rsync 可以實現觸發式的檔案同步,但是通過 crontab 守護程序方式進行觸發,同步的資料和實際資料會有差異,而 inotify 可以監控檔案系統的各種變化,當檔案有任何變動時,就觸發 rsync 同步,這樣剛好解決了同步資料的實時性問題。
一、基本環境
系統:CentOS 2.6.32-220.el6.x86_64
站羣軟件包版本:rsync-3.0.6-12.el6.x86_64
inotify-tools-3.14
下載連結:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
服務端(server):172.16.32.204
客服端(client1):172.16.32.205
(client2):172.16.32.206
二、客户端配置(172.16.32.205 、 172.163.32.206)
1. client1 172.16.32.205 配置
安裝 rsync
#yum install -y rsync xinetd
在/etc/目錄下建立 rsyncd.conf 配置檔案進行編輯
#vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[lixuan]
path = /data/lixuan/ #本地自定義路徑
comment = client file
ignore errors
read only = no
write only = no
hosts allow = 172.16.32.204 #服務端 IP 地址
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /etc/client1.pass #自動同步密碼檔案
新建/etc/client1.pass 檔案
#echo “root:123456” >>/etc/client1.pass
#chmod 600 /etc/client1.pass
啓動 rsync 服務
#/etc/init.d/xinetd start
2. client2 172.16.32.206 配置
安裝 rsync
#yum install -y rsync xinetd
在/etc/目錄下建立 rsyncd.conf 配置檔案進行編輯
#vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[lixuan]
path = /data/lixuan/ #本地自定義路徑
comment = client file
ignore errors
read only = no
write only = no
hosts allow = 172.16.32.204 #服務端 IP 地址
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /etc/client2.pass #自動同步密碼檔案
儲存退出!
新建/etc/client2.pass 檔案
#echo “root:123456” >>/etc/client2.pass
chmod 600 /etc/client2.pass
啓動 rsync 服務
#/etc/init.d/xinetd start
三、服務端配置(172.16.32.204)
1. 安裝 rsync
#yum install -y rsync xinetd
在/etc/目錄下建立 rsyncd.conf 配置檔案進行編輯
#vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 100
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/server.pass
[lixuan]
path = /data/lixuan/
auth users = root
list = no
read only = no
secrets file = /etc/servers.pass
comment = server directory
儲存退出!
新建/etc/server.pass 檔案
#echo “root:123456” >>/etc/server.pass
#chmod 600 /etc/server.pass
啓動 rsync 服務
#/etc/init.d/xinetd start
2. 安裝 inotify
驗證核心是否支援 inotify
#uname -r
2.6.32-220.el6.x86_64
#ll /proc/sys/fs/inotify
total 0
-rw-r–r– 1 root root 0 Jun 11 10:15 max_queued_events #表示呼叫 inotify_init 時分配給 inotify instance 中可排隊的 event 的數目的最大值
-rw-r–r– 1 root root 0 Jun 11 10:15 max_user_instances #表示每一個 real user ID 可建立的 inotify instatnces 的數量上限
-rw-r–r– 1 root root 0 Jun 11 10:15 max_user_watches #表示每個 inotify instatnces 可監控的最大目錄數量
配置服務端內容釋出指令碼
#vim /data/sh/inotifyrsync.sh
#!/bin/bash
client1=172.16.32.205
client2=172.16.32.206
src=/data/lixuan/
dst=lixuan
user=root
/usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f%e’ -e close_write,modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg –delete –progress –password-file=/etc/client.pass $src $user@$client1::$dst
/usr/bin/rsync -vzrtopg –delete –progress –password-file=/etc/client.pass $src $user@$client2::$dst
echo “${files} was rsynced” >>/tmp/rsync.log 2>&1
done
儲存退出!
新建/etc/client.pass 檔案
#echo “123456” >>/etc/client.pass
#chmod 600 /etc/client.pass
賦予指令碼執行許可權
#chmod 755 /data/sh/inotifyrsync.sh
後台執行指令碼
#sh /data/sh/inotifyrsync.sh &
將此指令碼加入開機自啓動檔案中
#echo “/data/sh/inotifyrsync.sh &” >> /etc/rc.local
四、測試 rsync+inotify 資料實時同步
在服務端(172.16.32.204)的/data/lixuan/目錄中新增刪除目錄或檔案,然後進入客户端(172.16.32.205 、 172.16.32.206)的/data/lixuan/目錄中檢視是否和服務端資料實時保持一致。