inotify-tools时实调用rsync同步文件

DelSegal 9年前

来自: 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

上面这个脚本,当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

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

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是会被同步的

更新使用方法,参见这里