inotify-tools时实调用rsync同步文件
2015-06-08 18:05
375 查看
下载inotify-tools
http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gzinotify-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是会被同步的
更新使用方法,参见这里
相关文章推荐
- rsync ssh 数据同步分析
- rsync详解之exclude排除文件
- rsync结合find技巧分享
- rsync 数据同步使用详解
- 用rsync实现windows与linux文件同步的方法
- rsync命令排除文件和文件夹(exclude-from)
- Windows rsync服务器备份配置实例
- window系统的Rsync同步实战分析第1/2页
- 使用RSYNC在服务器间镜像或者备份目录和文件
- 利用rsync自动备份 完全配置方法 增加了ip限制
- Linux 用cp和rsync同步文件时跳过指定目录的方法
- rsync 同步错误 cwrsync rsync error rsync error: some files/attrs were not transferred 解决方法
- 用rsync对网站进行镜像备份实现步骤
- rsync中文手册之使用rsync实现网站镜像和备份linux第1/3页
- 使用rsync同步网路备份第1/2页
- 使用rsync来实现文件同步
- rsync相对于其他同步工具的优缺点介绍
- Rsync 服务器文件同步的搭建以及使用
- RSync实现文件同步备份配置详解
- Shell脚本实现监控rsync数据是否传输完