前言:
rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。
一、基本環境
系統:CentOS 2.6.32-220.el6.x86_64
軟件包版本:rsync-3.0.6-12.el6.x86_64
inotify-tools-3.14
下載鏈接:百度 inotify-tools-3.14
服務端(server):172.16.1.1
客服端(client1):172.16.1.2
(client2):172.16.1.3
二、客戶端配置(172.16.1.2、172.16.1.3)
1. client1 172.16.1.2配置
安裝rsync
#yum install -y rsync xinetd
在/etc/目錄下建立rsyncd.conf配置文件進行編輯
#vim /etc/rsyncd.conf
uid = nobody //rsyncd 守護進程運行系統用戶全局配置,也可在具體的塊中獨立配置,
gid = nobody //rsyncd 守護進程運行系統用戶全局配置,也可在具體的塊中獨立配置,
use chroot = no //允許 chroot,提升安全性,客戶端連接模塊,首先chroot到模塊path參數指定的目錄下 #chroot為yes時必須使用root權限,且不能備份path路徑外的鏈接文件
max connections = 10 //最大連接數
strict modes = yes //是否檢查口令文件的權限
pid file = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //支持max connections參數的鎖文件
log file = /var/log/rsyncd.log //日志文件位置,啟動rsync后自動產生這個文件,無需提前創建
[lixuan] //自定義名稱
path = /data/lixuan/ #本地自定義路徑
comment = client file //模塊名稱與自定義名稱相同
ignore errors //忽略錯誤
read only = no //設置rsync服務端文件為讀寫權限
write only = no //設置rsync服務端文件為讀寫權限
hosts allow = 172.16.1.1 #服務端IP地址
hosts deny = * //止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
list = false //不顯示rsync服務端資源列表
uid = root //設置rsync運行權限為root
gid = root //設置rsync運行權限為root
auth users = root //模塊驗證用戶名稱,可使用空格或者逗號隔開多個用戶名
secrets file = /etc/client1.pass #自動同步密碼文件
新建/etc/client1.pass 文件
#echo "root:123456" >>/etc/client1.pass
#chmod -R 600 /etc/client1.pass #這個 必須是 600權限 要不就會提示 找不到文件
啟動rsync服務
#/etc/init.d/xinetd start
#rsync --daemon --config=/etc/rsync.conf
2. client2 172.16.1.3配置
安裝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 -R 600 /etc/client2.pass #這個 必須是 600權限 要不就會提示 找不到文件
啟動rsync服務
#/etc/init.d/xinetd start
#rsync --daemon --config=/etc/rsync.conf
三、服務端配置(172.16.1.1)
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 -R 600 /etc/server.pass #這個 必須是 600權限 要不就會提示 找不到文件
啟動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.1.2
client2=172.16.1.3
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 -R 600 /etc/client.pass #這個 必須是 600權限 要不就會提示 找不到文件
賦予腳本執行權限
#chmod 755 /data/sh/inotifyrsync.sh
后台執行腳本
#sh /data/sh/inotifyrsync.sh &
將此腳本加入開機自啟動文件中
#echo "/data/sh/inotifyrsync.sh &" >> /etc/rc.local
#rsync --daemon --config=/etc/rsync.conf
四、測試rsync+inotify數據實時同步
在服務端(172.16.1.1)的/data/lixuan/目錄中添加刪除目錄或文件,然后進入客戶端(172.16.1.1、172.16.1.2)的/data/lixuan/目錄中查看是否和服務端數據實時保持一致。