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

rsync 遠程容災備份實戰:守護進程模式 + 非交互認證 + 定時增量同步全流程

編輯:Linux 教學

rsync 憑藉增量傳輸、保留屬性、加密通道三大特性,成為 Linux 跨機房異步備份的事實標準。本文以雙節點 A(生產) / B(備份) 為例,落地生產級部署全鏈路。

環境基線

  • OS: RHEL 5 / CentOS 5 (Kernel 2.6.18)
  • A 端 (Server): 192.168.60.253,數據目錄 /webdata
  • B 端 (Client): 192.168.60.231,備份落盤 /ixdba.net
  • rsync 版本: 3.0.4 (源碼編譯安裝至 /usr/local)

Server 端:守護進程配置與啟動

# 1. 編譯安裝
tar zxvf rsync-3.0.4.tar.gz && cd rsync-3.0.4
./configure --prefix=/usr/local && make && make install

# 2. 創建配置 /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[ixdba]
path = /webdata
comment = ixdba business data
ignore errors
read only = true
list = false
uid = root
gid = root
auth users = backup
secrets file = /etc/server.pass

# 3. 創建認證文件 (權限 600)
echo "backup:Str0ngP@ssw0rd" > /etc/server.pass
chmod 600 /etc/server.pass

# 4. 啟動守護進程 (加入 /etc/rc.local 開機自啟)
/usr/local/bin/rsync --daemon
ps -ef | grep rsync  # 驗證進程存在

Client 端:非交互同步腳本與定時任務

# 1. 創建對應密碼文件 (僅含密碼,權限 600)
echo "Str0ngP@ssw0rd" > /etc/server.pass
chmod 600 /etc/server.pass

# 2. 同步命令解析
/usr/local/bin/rsync -vzrtopg --delete --progress \
  backup@192.168.60.253::ixdba /ixdba.net \
  --password-file=/etc/server.pass

# 參數詳解:
# -v 詳細輸出  -z 壓縮傳輸  -r 遞歸  -t 保留時間  -o 保留屬主  -p 保留權限  -g 保留屬組
# --delete 鏡像模式:以 Server 為準,Client 多出的文件將被刪除
# --progress 顯示進度
# backup@IP::module  認證用戶@Server::模塊名

生產級增強:crontab 定時 + 日誌輪轉 + 異常告警

# /etc/cron.d/rsync-backup
# 每小時增量同步,日誌獨立存放
0 * * * * root /usr/local/bin/rsync -vzrtopg --delete \
  backup@192.168.60.253::ixdba /ixdba.net \
  --password-file=/etc/server.pass \
  >> /var/log/rsync_sync.log 2>&1

# 日誌輪轉 /etc/logrotate.d/rsync
/var/log/rsync_sync.log {
    daily
    rotate 30
    compress
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/rsyncd.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

關鍵風險控制點

風險對策
--delete 導致誤刪Server 端啟用快照 (LVM/ZFS) 或備份前在 Client 做只讀快照
大文件傳輸中斷殘留臨時文件rsync 3.0+ 支持 --partial --append-verify 斷點續傳
軟鏈接/硬鏈接/稀疏文件處理加參數 -H -A -X -S 保留完整屬性
網絡抖動導致同步卡死加 --timeout=600 --contimeout=30 設置超時

驗證清單

  • Client 執行同步命令無需輸入密碼,日誌顯示 Number of files transferred: X
  • diff -r /webdata /ixdba.net/webdata 無差異 (排除臨時文件)
  • 模擬 Server 端刪除文件,Client 下輪同步後同步消失
  • 網絡中斷恢復後自動續傳,無數據損壞

rsync 容災核心在於自動化、冪等、可觀測,將同步納入監控體系 (Prometheus + node_exporter 采集 rsync_last_sync_timestamp),才算真正落地。

Copyright © Windows教程網 All Rights Reserved