Inotify&Sersync文件监视工具配置

一、Inotify介绍:
一共安装2个工具(命令),即inotifywait和inotifywatch
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用;
inotifywatch:收集被监视的文件系统使用度统计数据,指文件系统事件发生的次数统计

1、inotify配置
[root@nfs01 ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@nfs01 ~]#yum install inotify-tools -y
[root@nfs01 scripts]# vim inotify.sh
#!/bin/sh
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data |while read file
do
cd /data &&
rsync -avz ./ --delete backup@172.16.1.41::backup --password-file=/etc/rsync.password
>/dev/null
done
[root@nfs01 ~]# sh /server/scripts/inotify.sh & #后台执行
[root@nfs01 scripts]# tail -1 /etc/rc.local #开机自启动
/bin/sh /server/scripts/inotify.sh &

2、inotifywait常用选项
-m --monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d --daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r --recursive Watch directories recursively.
-e --event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
-q --quiet Print less (only print events)

3、关键参数说明
在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制
max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量

二、sersync配置
[root@nfs01 tools]# unzip sersync_installdir_64bit.zip
[root@nfs01 tools]# mv sersync_installdir_64bit/sersync /application/
[root@nfs01 ~]# vim /application/sersync/conf/confxml.xml #编辑配置文件

[root@nfs01 /]# vimdiff /application/confxml.xml /application/bkconfxml.xml #文件比对
[root@nfs01 ~]# tail -1 /etc/rc.local
/application/sersync/bin/sersync -r -d -o /application/sersync/conf/confxml.xml
[root@nfs01 ~]# /application/sersync/bin/sersync -help
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________

原文地址:https://www.cnblogs.com/xwupiaomiao/p/8194758.html