環境:
192.168.1.35 CentosX64 6.5
192.168.1.37 CentosX64 6.5
實現功能:
現在我們需要監控IP爲35機器上的某一目錄,然後同步到IP爲37的機器上
軟件安裝:
我們需要在35上安裝rsync服務,在37上安裝rsync客戶端和inotify-tools
rsync服務安裝參考http://blog.csdn.net/chenghuikai/article/details/60767613
inotify-tools安裝:直接yum安裝即可
編寫inotify腳本:
#!/bin/bash
host01=192.168.1.160 #inotify-slave的ip地址
src=/backup/ #本地監控的目錄
dst=backup #inotify-slave的rsync服務的模塊名
user=rsync_backup #inotify-slave的rsync服務的虛擬用戶
rsync_passfile=/etc/rsync.password #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify-3.14 #inotify的安裝目錄
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
然後後臺運行腳本:
sh inotify.sh &
現在可以測試了