1.安裝軟件
2臺server
一臺安裝rsync,另一臺安裝inotify
yum install -y rsync
下載inetify
http://inotify-tools.sourceforge.net
解壓 && configure && make && make install
2.配置rsync
開啓rsync
vim /etc/xinetd.d/rsync
disable = no
編寫配置文件
vim /etc/rsyncd.conf
uid=nobody
gid=nobody
user chroot=no
max connections=200
timeout=600
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[test]
path=/test
ignore errors
read only=no
list=no
hosts allow=192.168.1.0/255.255.255.0
auth users=test
secrets file=/root/rsyncd_passwd
創建密碼文件
echo 'test:test' >> /root/rsyncd_passwd
客戶端出已經可以同步了,設置客戶端密碼文件
echo 'test' > /root/rsyncd_passwd
rsync -av --delete [email protected]::test /test --password-file=/root/rsyncd_passwd
3.編寫inotify監控腳本
當本地有改動時,觸發rsync進行同步達到實時同步的效果(不適用於大數據量)
vim /root/rsync.sh
#!/bin/bash
src=/test
des=test
ip=192.168.70.173
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib $src | while read file
do
rsync -av --delete $src test@$ip::$des --password-file=/root/rsy
ncd_passwd
echo "$file was rsynced" >> rsync.log 2>&1
done
在inotify服務器上啓動文件中加入腳本
echo "sh /root/rsync.sh &" >> /etc/rc.local
注意保持權限正確,否則可能導致同步失敗。
inotifywait監控時間參數:
-m 事件監聽
-r 遞歸查詢目錄
-q 打印出監控事件
-e 指定監控的事件
--timefmt 指定時間顯示格式
%d日 %m月 %y年 %H時 %M分
--format 指定輸出信息的格式
%T事件 %w路徑名 %f文件名 %e事件