主站 (Source) ──rsync daemon──► 從站1、從站2、... (Clients)
│ │
│ 定時任務 (cron/systemd timer) │ 被動接收
▼ ▼
/var/www/html (源碼目錄) /var/www/html (目標目錄)
yum -y install rsync xinetd (CentOS/RHEL) 或 apt install rsync (Debian/Ubuntu)。vim /etc/xinetd.d/rsync
# 修改 disable = no
systemctl enable --now xinetd
/etc/rsyncd.conf:
uid = root
gid = root
use chroot = yes
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[site_sync]
path = /var/www/html
comment = Web Site Cluster Sync
read only = yes
list = yes
hosts allow = 192.168.1.0/24 10.0.0.0/8
hosts deny = *
auth users = rsync_user
secrets file = /etc/rsyncd.secrets
echo "rsync_user:StrongPass123!" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
yum -y install rsync。echo "StrongPass123!" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
rsync -avz --delete --password-file=/etc/rsync.pass \
--exclude-from=/etc/rsync_exclude.lst \
rsync_user@MASTER_IP::site_sync /var/www/html/
--exclude-from 範例:
# /etc/rsync_exclude.lst
*.log
cache/
tmp/
.svn/
.git/
*.tmp
cat > /etc/cron.d/rsync_site_sync <<'EOF'
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/5 * * * * root /usr/bin/rsync -avz --delete --password-file=/etc/rsync.pass --exclude-from=/etc/rsync_exclude.lst rsync_user@MASTER_IP::site_sync /var/www/html/ >> /var/log/rsync_sync.log 2>&1
EOF
| 參數 | 作用 |
|---|---|
-a | 歸檔模式 = -rlptgoD(遞歸、保留連結/權限/時間/擁有者/設備) |
-v | 詳細輸出 |
-z | 傳輸壓縮(跨公網/低帶寬建議開啟) |
--delete | 刪除目標端源端不存在的檔案(鏡像同步) |
--exclude-from | 排除清單檔,每行一個模式 |
--bwlimit=KBps | 限制頻寬,避免佔滿業務帶寬 |
--partial | 保留斷傳檔案,斷點續傳 |
--checksum | 基於校驗和而非時間/大小判斷(慢但精準) |