Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 服務器間文件同步實戰:Rsync 守護進程模式配置與定時同步策略

編輯:Linux 教學

Rsync 是 Linux 系統級增量同步工具,支援守護進程模式(Server/Client)實現高效、認證、帶寬控制的跨服務器文件同步。本文詳細記錄雙向/單向同步架構、xinetd 托管配置、認證授權、排除規則與 Crontab 定時策略。

架構角色定義

  • Server A (源端):運行 rsync --daemon,提供只讀/讀寫模組,數據權威源。
  • Server B (目標端):作為 Client,定時 rsync -avz 從 A 拉取數據。

Server A:Rsync 守護進程配置

1. 安裝與 xinetd 托管 (CentOS 6/7 兼容)

yum install -y rsync xinetd

# 編輯 xinetd 服務定義
cat > /etc/xinetd.d/rsync <<'EOF'
service rsync
{
    disable = no
    flags           = IPv4
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure += USERID
}
EOF

systemctl restart xinetd
systemctl enable xinetd
# 或 service xinetd restart; chkconfig xinetd on

2. 核心配置文件 /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
transfer logging = yes
timeout = 300
port = 873

[www]                   # 模組名,客戶端用 ::www 訪問
    path = /home/www    # 同步源目錄
    comment = Web root sync
    read only = yes     # 只讀,源端通常不接受推送
    list = yes          # 允許客戶端列出模組
    auth users = rsync_user  # 虛擬用戶,不需系統賬號
    secrets file = /etc/rsyncd.passwd
    hosts allow = 10.0.0.0/8, 192.168.0.0/16  # 允許網段
    hosts deny = *
    exclude = .git/ node_modules/ *.log tmp/ cache/  # 排除規則
    # 進階: 讀寫模組需 write only = no, 配合 incoming/outgoing chroot

3. 虛擬用戶認證文件

# 格式: 用戶名:密碼 (明文,僅 rsync 守護進程使用)
echo "rsync_user:StrongRandomPassword123!" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd

4. 防火牆放行 873 埠

firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
# 或 iptables -I INPUT -p tcp --dport 873 -j ACCEPT

Server B:客戶端同步腳本

1. 密碼文件 (僅客戶端需密碼,權限 600)

echo "StrongRandomPassword123!" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd

2. 同步命令詳解

rsync -avz --delete --progress \
    --password-file=/etc/rsyncd.passwd \
    rsync_user@192.168.1.100::www \
    /home/www/
參數含義
-a歸檔模式 = -rlptgoD (遞歸、保留鏈接、權限、時間、組、擁有者、設備)
-v詳細輸出
-z傳輸壓縮 (LAN 可省略,WAN 建議開啟)
--delete刪除目標端源端不存在的文件 (鏡像同步,慎用)
--progress顯示單文件進度
--password-file指定密碼文件,避免交互輸入
--bwlimit=10240限制帶寬 10MB/s,避免佔滿業務帶寬
--exclude-from=/etc/rsync_exclude.txt外部排除文件列表

3. 同步腳本 /root/sync_www.sh

#!/bin/bash
set -euo pipefail

LOG="/var/log/rsync_www.log"
SRC="rsync_user@192.168.1.100::www"
DEST="/home/www"
PASS="/etc/rsyncd.passwd"
BWLIMIT="20480"  # 20MB/s

echo "$(date '+%F %T') ==== Sync START ====" >> "$LOG"
rsync -avz --delete --progress \
    --bwlimit="$BWLIMIT" \
    --password-file="$PASS" \
    "$SRC" "$DEST" >> "$LOG" 2>&1
RET=$?
echo "$(date '+%F %T') ==== Sync END (exit $RET) ====" >> "$LOG"
exit $RET
chmod +x /root/sync_www.sh

定時同步策略 (Crontab)

# 編輯 root crontab
crontab -e

# 每 15 分鐘同步一次 (高頻業務)
*/15 * * * * /root/sync_www.sh

# 或每小時整點 (常規業務)
0 * * * * /root/sync_www.sh

# 每天凌晨 2:30 低峰全量校驗
30 2 * * * /root/sync_www.sh --checksum

驗證與排錯

# 測試連通性
rsync --list-only rsync_user@192.168.1.100::www --password-file=/etc/rsyncd.passwd

# 手動執行腳本觀察日誌
/root/sync_www.sh
tail -f /var/log/rsync_www.log

# 檢查守護進程狀態
netstat -ntulp | grep 873
ps aux | grep rsync

進階:雙向同步與衝突處理

Rsync 本質單向,雙向同步需雙向運行或使用 Unison(雙向同步、圖形/文本界面、衝突檢測、指紋緩存)。生產環境建議:

  • 單向權威源 + 只讀備份:Rsync 守護進程模式(本文方案)。
  • 多活雙向:Unison + 定時任務,或分佈式文件系統。
  • 對象存儲同步:rclone / aws s3 sync / ossutil。
Copyright © Windows教程網 All Rights Reserved