inotify + rsync 实时同步

下载inotify

wget https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

安装:

[root@zhu-centos6 ~]# tar -zxvf inotify-tools-3.14.tar.gz ##解压文件

[root@zhu-centos6 ~]# cd inotify-tools-3.14 ##进入inotify里面

[root@zhu-centos6 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14 #定义他的安装目录

------------------------

查看上一个命令是否执行成功,若执行成功,返回0,若执行出错则返回其他数字。


-----------------------

[root@zhu-centos6 inotify-tools-3.14]# make && make install ##开始执行 安装,若执行不出错,即表示安装完成。

inotifywait 单独分析

[root@Centos6-Vm-A-247 ~]#/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib /data1

#执行上面命令,是让inotifywait监听/data1/目录,当监听到有发生close_write,delete,create,attrib等事件发生时,按%d/%m/%y %H:%M %w%f的格式输出。


inotifywait 参数说明

参数名称参数说明
-m,–monitor始终保持事件监听状态
-r,–recursive递归查询目录
-q,–quiet只打印监控事件的信息
–excludei排除文件或目录时,不区分大小写
-t,–timeout超时时间
–timefmt指定时间输出格式
–format指定时间输出格式
-e,–event后面指定删、增、改等事件

inotifywait events事件说明

事件名称事件说明
access读取文件或目录内容
modify修改文件或目录内容
attrib文件或目录的属性改变
close_write修改真实文件内容
close_nowrite
close
open文件或目录被打开
moved_to文件或目录移动到
moved_from文件或目录从移动
move移动文件或目录移动到监视目录
create在监视目录下创建文件或目录
delete删除监视目录下的文件或目录
delete_self
unmount卸载文件系统

-----------------实现实时同步的脚本------------------------------------

#!/bin/bash

#para

##目标主机的IP

hostip=192.168.2.185

##需要监控同步的目录

src=/data1

##目标主机的模块(/etc/rsyncd.conf里面的模块)

dst=zhuzhiwei

##rsync的别名用户

user=zzw

##rsync的密码文件存放位置

rsync_passfile=/etc/rsync.password

##inotify工具的位置

inotify_home=/usr/local/inotify-tools-3.14/

######judge(以下内容是检查上面的命令是否正确,可以不写)

if [ ! -e "$src" ] \

|| [ ! -e "${rsync_passfile}" ] \

|| [ ! -e "${inotify_home}/bin/inotifywait" ] \

|| [ ! -e "/usr/bin/rsync" ];

then

echo "Check File and Folder"

exit 9

fi

#############################

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \

| while read file

do

#rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@hostip::$dst >/dev/null 2>&1

cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$hostip::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

done

exit 0

####相关脚本可参考:http://www.cnblogs.com/jackyyou/p/5681126.html

---------------------------------------------------------------------------------

优化 Inotify

# 在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制

 

1
2
3
4
5
[root@web ~]# ll /proc/sys/fs/inotify/
总用量0
-rw-r--r--1 root root 09月923:36 max_queued_events
-rw-r--r--1 root root 09月923:36 max_user_instances
-rw-r--r--1 root root 09月923:36 max_user_watches

 

-----------------------------

max_user_watches #设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)

max_user_instances #设置每个用户可以运行的inotifywait或inotifywatch命令的进程数

max_queued_events #设置inotify实例事件(event)队列可容纳的事件数量

----------------------------

[root@web ~]# echo 50000000>/proc/sys/fs/inotify/max_user_watches -- 把他加入/etc/rc.local就可以实现每次重启都生效

[root@web ~]# echo 50000000>/proc/sys/fs/inotify/max_queued_events

更多实时同步工具:http://oldboy.blog.51cto.com/2561410/1216457

inotify+rsync实时数据同步实战操作讲解:

http://oldboy.blog.51cto.com/2561410/1111439


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章