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

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

更新使用方法,参见这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值