Centos7学习笔记(十二)- inotify+rsync与sersync
1、inotify-tools的安装
yum安装,安装好epel源后,yum -y install inotify-tools
源码安装:
wget https://github.com/inotify-tools/inotify-tools/releases/download/3.20.2.2/inotify-tools-3.20.2.2.tar.gz tar zxvf inotify-tools-3.20.2.2.tar.gz cd inotify-tools-3.20.2.2/ ./configure --prefix=/usr/local/inotify-tools-3.20 make && make install ln -s /usr/local/inotify-tools-3.20/ /usr/local/inotify echo 'export PATH="$PATH:/usr/local/inotify/bin"' >> /etc/bashrc . /etc/bashrc
2、inotifywait命令的解释
inotifywait命令的选项:
-m:表示始终监控,否则应该是监控到了一次就退出监控了
-r:递归监控,监控目录中的任何文件,包括子目录。递归监控可能会超出max_user_watches的值,需要适当调整该值
@<file>:如果是对目录进行递归监控,则该选项用于排除递归目录中不被监控的文件。file是相对路径还是绝对路径由监控目录是相对还是绝对来决定
-q:--quiet的意思,静默监控,这样就不会输出一些无关的信息
-e:指定监控的事件。一般监控的就delete、create、attrib、modify、close_write
--exclude <pattern> :通过模式匹配来指定不被监控的文件,区分大小写
--excludei <pattern>:通过模式匹配来指定不被监控的文件,不区分大小写
--timefmt:监控到事件触发后,输出的时间格式,可指定可不指定该选项,一般设置为[--timefmt '%Y/%m/%d %H:%M:%S']
--format:用户自定义的输出格式,如[--format '%w%f %e%T']
%w:产生事件的监控路径,不一定就是发生事件的具体文件,例如递归监控一个目录,该目录下的某文件产生事件,将输出该目录而非其内具体的文件
%f:如果监控的是一个目录,则输出产生事件的具体文件名。其他所有情况都输出空字符串
%e:产生的事件名称,记录时,事件之间(比如echo内容进入文件,会产生close_write、close两个事件)默认用逗号分隔
%T:以"--timefmt"定义的时间格式输出当前时间,要求同时定义"--timefmt",不加%T,那么定义--timefmt就没意义了。
(%Xe:对应的是%e,其中X是指定的任意字符(空格都行),来分隔连续记录的事件名称)
[root@nfs01 /home/study]#inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%y-%m-%d %H:%M:%S' --format '%w%f:%?e:%T' /nfsdata//nfsdata/aaa:CLOSE_WRITE?CLOSE:20-09-02 20:38:43
[root@nfs01 /home/study]#inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%y-%m-%d %H:%M:%S' --format '%w%f:% e:%T' /nfsdata/
/nfsdata/aab.txt:CLOSE_WRITE CLOSE:20-09-02 20:46:01
<br/>
inotifywait -e可监控的事件:
<br/>
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 :监控目录相关操作
https://www.cnblogs.com/f-ck-need-u/p/7220193.html
<br/>
一般在脚本中,inotifywait用如下参数选项就够了
inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' --exclude=".*.swp" 被监控目录
<br/>
inotify的优化:
/proc/sys/fs/inotify/max_queued_events,inotify实例事件队列可容纳的事件数量。默认16384,根据性能,将该值改大。50w以上。
/proc/sys/fs/inotify/max_user_instances,inotifywait或者inotifywatch命令,每用户最大可启用的进程数,默认128,。可适当改大,比如512
/proc/sys/fs/inotify/max_user_watches,inotifywait或者inotifywatch命令可以监视的文件数量(指单进程),默认8192。可适当改大,比如30000。
重启会失效,若想永久生效,需写入/etc/rc.local做开机处理。
<br/>
3、sersync
a)下载安装sersync:
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
cp -a GNU-Linux-x86 /usr/local/sersync
echo "PATH=$PATH:/usr/local/sersync" > /etc/profile.d/sersync.sh
source /etc/profile.d/sersync.sh
b)sersync配置文件confxml.xml文件内容详解
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> #《=====sersync监控的主机,及其使用的端口
<debug start="false"/> #《=====是否开启调试模式
<fileSystem xfs="false"/> #《=====文件系统是否是xfs,对CentOS7来讲,一般都是xfs,因此需修改为true
<filter start="false"> #《===== 是否启用监控的筛选功能,筛选的文件将不被监控
<exclude expression="(.*).svn"></exclude> #《===== 监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
<exclude expression="(.*).gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> #《=====监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/opt/tongbu"> #《====== 同步的目录或文件,同inotify+rsync一样,建议同步目录,sersync监控的目录
<remote ip="127.0.0.1" name="tongbu1"/> #《======== 推送的目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync服务
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/> #《=========定义rsync推送时所用的参数
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #《=======定义rsync推送所需定义的用户名和密码文件
<userDefinedPort start="false" port="874"/><!-- port=874 --> #《========是否修改默认873端口为自定义的端口如874
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/> #《=======这里如果改为true,rsync是以ssh shell方式推送,上面remote段中的name,就是指远端目标目录而非模块名。
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #《========rsync推送失败后,失败日志记录文件
<crontab start="false" schedule="600"><!--600mins--> #《=============是否开启crontab及定时执行时间,默认600分钟
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/> #《=========是否开启plugin功能
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*).php"/>
<include expression="(.*).sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
注意:强将creatFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;所以除非特殊需要,请开启; 默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不对删除事件进行监控;
<br/>
c)运行sersync时,是需要加参数的,以下是参数说明:
[root@nfs01 /usr/local/sersync]#sersync2 -h
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参数,则默认执行同步程序
多目录监控时,可以把基础配置文件复制多份并修改,然后用参数-o独立指定配置文件运行,也可以在confxml.xml文件中,定义多localpath段。<br/>
一般都将sersync2用-d参数后台运行。
<br/>
d)重启sersync
用killall 进程方式或者自行写脚本实现start、stop、restart。
<br/>
