傳統 Rsync + Inotify-tools 架構在海量小檔案場景下效能低下:Inotify 只通知「目錄有變更」,Rsync 需全量掃描對比,耗時極長。Sersync 基於 Inotify 開發,能記錄具體變更的檔案/目錄名稱,配合 Rsync 僅同步增量,效能提升數量級。本文記錄 CentOS 5.x 雙向三節點(1 源 + 2 目標)生產級部署全流程。
| 架構 | 變更通知粒度 | Rsync 同步範圍 | 適用場景 |
|---|---|---|---|
| Rsync + Inotify-tools | 目錄級 | 全量掃描對比 | 資料量小(< 50 GB)、檔案數少 |
| Rsync + Sersync | 檔案/目錄級 | 僅同步變更項 | 資料量大(> 100 GB)、百萬級檔案 |
| 角色 | IP | 功能 |
|---|---|---|
| 源伺服器 | 192.168.21.129 | 運行 Sersync 監控 /home/www.jb51.net,推送至目標端 |
| 目標伺服器 1 | 192.168.21.127 | 運行 Rsync Server (port 873) |
| 目標伺服器 2 | 192.168.21.128 | 運行 Rsync Server (port 873) |
vi /etc/selinux/config → SELINUX=disabled → reboot 或 setenforce 0 臨時生效。vi /etc/sysconfig/iptables-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT22 埠規則之後、REJECT 規則之前,否則 iptables 啟動失敗。/etc/init.d/iptables restartyum install -y rsync xinetdvi /etc/xinetd.d/rsync → disable = no/etc/init.d/xinetd startlog file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
motd file = /etc/rsyncd.motd
[home_www.jb51.net]
path = /home/www.jb51.net/
comment = home_www.jb51.net
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = home_www.jb51.net_user
hosts allow = 192.168.21.129
hosts deny = 192.168.21.254echo "home_www.jb51.net_user:123456" > /etc/rsync.passchmod 600 /etc/rsyncd.conf /etc/rsync.passservice xinetd restart && netstat -antlp | grep 873
yum install -y rsync xinetd(若無 rsync 則裝雙套件)。/etc/passwd.txt(僅含密碼):echo "123456" > /etc/passwd.txt && chmod 600 /etc/passwd.txtmkdir -p /home/www.jb51.net/ceshirsync -avH --port=873 --progress --delete /home/www.jb51.net/ home_www.jb51.net_user@192.168.21.127::home_www.jb51.net --password-file=/etc/passwd.txtrsync ... @192.168.21.128::home_www.jb51.net ...sersync(Google Code 存檔或 GitHub fork),解壓至 /usr/local/sersync。confxml.xml:... 、... 等。./sersync2 -r -d -o confxml.xml(-r 先全量同步、-d 守護進程、-o 指定配置)。use chroot = no:支援同步符號連結指向的實際檔案。max connections = 200:應大於並發同步數。timeout = 600:大檔傳輸防超時。crontab 區段可設定定時全量校驗(如每天 03:00)。600,否則 Rsync 拒絕讀取。audit2allow -w -a 分析、建立模組或直接設 Permissive。tar 打包傳輸解壓,再啟動 Sersync 增量同步。rsync: failed to connect:檢查防火牆、xinetd 是否運行、rsyncd.conf 語法、hosts allow IP。Rsync + Sersync 是「輕量級、無資料庫、秒級延遲」的實時同步最佳實踐,適合中小型檔案同步、多機房熱備、靜態資源分發等場景。