inotify-tools時實調用rsync同步文件

DelSegal 8年前發布 | 7K 次閱讀 Linux

來自: http://blog.csdn.net//jiao_fuyou/article/details/46416021


下載inotify-tools

http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

inotify-tools時實調用rsync同步文件

#!/bin/sh 
host1=172.16.18.116
host2=172.16.18.226
src=/home/jfy/tmptmp 
des=/home/jfy 
inotifywait=/usr/local/inotify-tools/bin/inotifywait

/usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file do echo -e "=== rsync ${host1} ...\n" rsync -avtgpro --delete '-e ssh -p 2014' ${src} root@${host1}:${des} echo -e "\n" echo -e "=== rsync ${host2} ...\n" rsync -avtgpro --delete ${src} root@${host2}:${des} echo "---------------------------------------------------------------------------------------" done</pre>

上面這個腳本,當vi一個被監控的文件后wq時,一下子會產生幾千條event,導致rsync被執行幾千次,這是由于文件很大,一直保存過程中就一直會有modify事件,實際上attrib,create,modify可以用close_write來代替,不管是新建,修改,attrib都會有close_write事件。

下面換一種寫法,只要inotifywait返回就執行rsnync,不管具體事件。

#!/bin/sh 
host1=172.16.18.116
host2=172.16.18.226
src=/home/jfy/tmptmp
des=/home/jfy
inotifywait=/usr/local/inotify-tools/bin/inotifywait

while [ 1 -eq 1 ] do echo -e "wait inotify ..." /usr/local/inotify-tools/bin/inotifywait -rq --timefmt '%d/%m/%y %H:%M' --format '%T %e %w%f' -e modify,delete,create,attrib ${src} > /dev/null echo -e "=== rsync ${host1} ...\n" rsync -avtgpro --delete '-e ssh -p 2014' ${src} root@${host1}:${des} echo -e "\n" echo -e "=== rsync ${host2} ...\n" rsync -avtgpro --delete ${src} root@${host2}:${des} echo "---------------------------------------------------------------------------------------" done</pre>

inotifywait還可以從file讀入要監控和要排除的文件或目錄:

/usr/local/inotify-tools/bin/inotifywait -rq --fromfile ../conf/inotify-file-list --timefmt '%d/%m/%y %H:%M' --format '%T %e %w%f' -e modify,delete,create,attrib

還有一些文件名匹配的選項可用

#!/bin/sh 
host1=172.16.18.116
host2=172.16.18.226
src=/home/jfy/tmptmp
des=/home/jfy
INOTIFY_INCLUDE="--fromfile /usr1/app/conf/inotify_include.list"
RSYNC_EXCLUDE="--include-from=/usr1/app/conf/rsync_include.list --exclude-from=/usr1/app/conf/rsync_exclude.list"

while [ 1 -eq 1 ] do echo -e "wait inotify ..." /usr/local/inotify-tools/bin/inotifywait -rq -e modify,delete,create,attrib --exclude "(.log|.swp|.inc|.svn|.rar|.tar.gz|.gz|.txt|.zip|.bak)" $INOTIFY_INCLUDE echo -e "=== rsync ${host1} ...\n" rsync -avtgpro --delete -e 'ssh -p 2014' $RSYNC_EXCLUDE ${src} root@${host1}:${des} echo -e "\n" echo -e "=== rsync ${host2} ...\n" rsync -avtgpro --delete $RSYNC_EXCLUDE ${src} root@${host2}:${des} echo "---------------------------------------------------------------------------------------" done</pre>

inotify_include.list,@為排除文件

/home/jfy/tmptmp
@/home/jfy/tmptmp/wollar.sql
@/home/jfy/tmptmp/ttt

–include-from可以指定–exclude-from中的一些特殊文件允許同步

rsync_exclude.list

tmptmp/56.sql
tmptmp/114.sql

rsync_include.list

tmptmp/114.sql

上面這兩個文件的結果就是114.sql是會被同步的

更新使用方法,參見這里

</div>

 本文由用戶 DelSegal 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!