本文環境為 CentOS 6.0,Server 端 192.168.182.130,Client 端 192.168.182.129。rsync 顧名思義就是 remote sync,是類 Unix 系統底下做資料鏡像備份的利器。

/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
通常需要 root 權限,也可寫進 /etc/rc.d/rc.local 讓它開機自啟。
rsync -vzrtopg --delete --progress --exclude "*.log" \ lansgg@192.168.182.130::lansggtest /root/backup \ --password-file=/etc/rsyncd.pass
參數說明:
-v 顯示細節;-z 傳輸時壓縮;-r 遞迴子目錄。-t 保留時間;-o 保留屬主;-p 保留權限;-g 保留屬組。-a 歸檔模式,等於 -rlptgoD。--delete 以伺服器為基準,伺服器刪掉的檔案客戶端也會刪。--progress 顯示進度;--exclude 排除指定類型。lansgg@192.168.182.130::lansggtest 指定伺服器與 rsyncd.conf 中的模組名。--password-file 指定客戶端密碼檔位置。[root@Centos ~]# yum install -y rsync

[root@Centos ~]# mkdir /root/test [root@Centos ~]# vim /etc/rsyncd.pass lansgg:lansgg123 [root@Centos ~]# chmod 600 /etc/rsyncd.pass
CentOS 的 rsync 套件預設不附設定檔,得自己寫:
[root@Centos ~]# vim /etc/rsyncd.conf uid=root # 傳輸時守護進程的 uid,預設 nobody gid=root # 傳輸時守護進程的 gid use chroot=no max connections=0 # 0 表示不限制 strict mode=yes # 檢查密碼檔權限 pid file=/var/run/rsyncd.pid lock file=/var/lock/rsync.lock log file=/var/log/rsyncd.log [lansggtest] # 模組名,可自訂 path=/root/test/ # 同步來源目錄 comment=lansgg test ignore errors read only=no # no 表示客戶端可上傳 write only=no # no 表示客戶端可下載 hosts allow=192.168.182.129 hosts deny=* list=false # 隱藏模組 auth users=lansgg secrets file=/etc/rsyncd.pass
[root@Centos ~]# rsync --daemon

在測試目錄建立幾個檔案:

[root@lv2 ~]# yum install rsync -y [root@lv2 ~]# vim /etc/rysncd.pass lansgg123 # 只寫密碼,不寫帳號 [root@lv2 ~]# mkdir /root/backup [root@lv2 ~]# rsync -vzrtopg --delete --progress --exclude "*.log" \ lansgg@192.168.182.130::lansggtest /root/backup \ --password-file=/etc/rsyncd.pass

可以看到 log 檔已被排除。把 rsync --daemon 寫進 rc.local,再把同步指令寫進 cron,就能做到定時備份——但這也說明了 rsync 的侷限:它無法即時同步。
inotify 是 Linux 內核特性,能監控檔案系統的讀寫、建立、刪除、卸載等事件,並即時通知應用程式,反應靈敏且比 cron 的輪詢有效率得多。使用前提是內核 2.6.13 以上,可用 uname -a 確認。

接下來是反向應用:Client 端有變動就推到多台 Server,很適合集群場景。三台 Server 端的 rsyncd.conf 設定同上。
Client 端安裝 inotify-tools:
[root@Centos ~]# yum install inotify-tools -y [root@Centos ~]# rpm -ql inotify-tools /usr/bin/inotifywait /usr/bin/inotifywatch
inotifywait 用來等待特定事件,可遞迴監控整個目錄樹;inotifywatch 則用於收集事件統計。
撰寫即時同步腳本:
# vi dataup.sh
#!/bin/bash
server1=192.168.182.130
server2=192.168.182.131
server3=192.168.182.132
src=/root/backup
dstmode=mode1
user=lansgg
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' \
-e modify,delete,create,attrib $src | while read files
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.pass $src $user@$server1::$dstmode
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.pass $src $user@$server2::$dstmode
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.pass $src $user@$server3::$dstmode
echo "$files was rsynced" >> /tmp/rsync.log 2>&1
done
inotifywait 常用參數:-m 持續監聽、-r 遞迴、-q 降低輸出、--timefmt 時間格式、--format 變動檔案細節。
sh inotify.sh & echo "sh inotify.sh &" >> /etc/rc.local
Client 端啟動並觀察日誌:
[root@node1 ~]# ./dataup.sh & [1] 10461 [root@node1 ~]# tail -f /tmp/rsync.log

執行 mkdir /root/backup/testtesttest 建立測試目錄後,Client 日誌會記錄事件:

到 Server 端確認,檔案已經全部同步完成:

補充:研究 inotify 時會順手遇到 incron(Inotify cron system),它把 inotify 與 cron 的排程概念結合起來,值得一試。