博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rsync+inotify
阅读量:4338 次
发布时间:2019-06-07

本文共 4914 字,大约阅读时间需要 16 分钟。

前提条件:已配置好rsync客户端和rsync --daemon服务端,数据可以正常同步,linux内核必须是2.6.13以上,如图:

[root@A-linux ~]# rsync -avzP /tmp/ rsync_backup@10.0.0.4::data --password-file=/etc/rsync.password  #<===首先检查同步是否正常sending incremental file list./hosts166 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)ssh-hBbdsD1500/ssh-hBbdsD1500/agent.1500sent 250 bytes received 38 bytes 576.00 bytes/sectotal size is 166 speedup is 0.58[root@C-linux data]# ll   #<===服务端查看同步结果总用量 8-rw-r--r-- 1 rsync rsync 166 1月 31 10:36 hostsdrwx------ 2 rsync rsync 4096 1月 31 13:27 ssh-hBbdsD1500

接着,在客户端安装inotify软件(这里是源码编译安装inotify-tools-3.14.tar.gz)

[root@A-linux tools]# tar xf inotify-tools-3.14.tar.gz [root@A-linux tools]# ./configure --prefix=/usr/local/inotify-tools-3.14 [root@A-linux tools]# make && make install [root@A-linux tools]# ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify[root@A-linux tools]# cd ../

inotify几个主要的内核参数(可根据实际调整参数)

[root@A-linux inotify]# ll -l /proc/sys/fs/inotify/            #<===以下文件中的默认值需要调大total 0-rw-r--r-- 1 root root 0 Sep 27 17:57 max_queued_events        #<===监控时件最大数量,需调整此文件默认大小-rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_instances       #<===用户的实例-rw-r--r-- 1 root root 0 Sep 27 17:57 max_user_watches         #<===用户实例可监控的最大目录及文件数量[root@A-linux inotify ~]# echo 327679 > /proc/sys/fs/inotify/max_queued_events  [root@A-linux inotify ~]# echo 30000000 > /proc/sys/fs/inotify/max_user_watches

inotify软件包主要的2个命令

[root@A-linux tools]# cd /usr/local/inotify[root@A-linux inotify]# tree ./bin./bin├── inotifywait          #<===等待文件发生变化,是inotify核心命令└── inotifywatch         #<===用于收集文件系统的统计数据,例如发生了多少次inotify事件,某文件被访问了多少次等等,一般用不上

inotifywait 命令的参数

-m  :表示始终监控,否则应该是监控到了一次就退出监控了-r  :递归监控,监控目录中的任何文件,包括子目录。递归监控可能会超出max_user_watches的值,需要适当调整该值@
:如果是对目录进行递归监控,则该选项用于排除递归目录中不被监控的文件。file是相对路径还是绝对路径由监控目录是相对还是绝对来决定-q :--quiet的意思,静默监控,这样就不会输出一些无关的信息-e :指定监控的事件。一般监控的就 delete、create、attrib、modify、close_write--exclude
:通过模式匹配来指定不被监控的文件,区分大小写--excludei
:通过模式匹配来指定不被监控的文件,不区分大小写--timefmt :监控到事件触发后,输出的时间格式,可指定可不指定该选项,一般设置为[--timefmt '%Y/%m/%d %H:%M:%S']--format :用户自定义的输出格式,如[--format '%w%f %e%T'] %w :产生事件的监控路径,不一定就是发生事件的具体文件,例如递归监控一个目录,该目录下的某文件产生事件,将输出该目录而非其内具体的文件 %f :如果监控的是一个目录,则输出产生事件的具体文件名。其他所有情况都输出空字符串 %e :产生的事件名称 %T :以"--timefmt"定义的时间格式输出当前时间,要求同时定义"--timefmt"

inotifywait -e 指定可监控的事件

access  :文件被访问modify  :文件被写入,内容被修改attrib  :元数据被修改。包括权限、时间戳、扩展属性等等close_write  :打开的文件被关闭,是为了写文件而打开文件,之后被关闭的事件close_nowrite :read only模式下文件被关闭,即只能是为了读取而打开文件,读取结束后关闭文件的事件close :是close_write和close_nowrite的结合,无论是何种方式打开文件,只要关闭都属于该事件open  :文件被打开moved_to  :向监控目录下移入了文件或目录,也可以是监控目录内部的移动moved_from :将监控目录下文件或目录移动到其他地方,也可以是在监控目录内部的移动move :是moved_to和moved_from的结合moved_self :被监控的文件或目录发生了移动,移动结束后将不再监控此文件或目录create :在被监控的目录中创建了文件或目录delete :删除了被监控目录中的某文件或目录delete_self :被监控的文件或目录被删除,删除之后不再监控此文件或目录umount :挂载在被监控目录上的文件系统被umount,umount后不再监控此目录isdir :监控目录相关操作

测试inotify监控实例(监控A-linux主机中/data目录下子目录及文件的变化)

监控单个事件[root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data     #<==仅仅监控目录中被删除文件或目录同时监控多个事件[root@A-linux inotify]# /usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write /data

使用inotify脚本实时监控目录内子目录和文件的变化

[root@A-linux ~]# cat inotify.sh #!/bin/bash############################################################ description: inotify+rsync best practice ## author : 骏马金龙 ## blog : http://www.cnblogs.com/f-ck-need-u/ ############################################################watch_dir=/datapush_to=10.0.0.4# First to do is initial syncrsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.password/usr/local/inotify/bin/inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \--exclude=".*.swp" >>/etc/inotifywait.log &while true;doif [ -s "/etc/inotifywait.log" ];thengrep -i -E "delete|moved_from" /etc/inotifywait.log >> /etc/inotify_away.logrsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.passwordif [ $? -ne 0 ];thenecho "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |\mail -s "inotify+Rsync error has occurred" root@localhostficat /dev/null > /etc/inotifywait.logrsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir rsync_backup@$push_to::data --password-file=/etc/rsync.passwordelsesleep 1fidone

脚本可以放进 /etc/rc.local 中开机执行,或者放在后台执行

[root@A-linux ~]# echo '/bin/sh /root/inotify.sh &' >>/etc/rc.local [root@A-linux ~]# tail -1 /etc/rc.local /bin/sh /root/inotify.sh &

小结:

(1)当同步的目录数据量不大时,建议使用 rsync+inotify(2)当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用 rsync+sersync(3)生产环境直接用 rsync+sersync,而不使用 rsync+inotify

博客参考 

 

转载于:https://www.cnblogs.com/blog-tim/p/10369710.html

你可能感兴趣的文章
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>