sersync类似于inotify,同样用于监控,但它克服了inotify的几个缺点。
前文说过,inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件(例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件),从而导致重复调用rsync命令。而且vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的。
这里说明一下优化inotify+rsync的脚本,也能克服了上面两个问题。但sersync也能克服这样的问题,且实现方式更简单,另外它还有多线程的优点。
sersync项目地址:https://code.google.com/archive/p/sersync/,在此网站中有下载、安装、使用等等详细的中文介绍。
sersync下载地址:https://code.google.com/archive/p/sersync/downloads。
简言之,sersync可以过滤重复事件减轻负担、自带crontab功能、多线程调用rsync、失败重传。
建议:
实际上,在前面inotify+rsync的最佳实现中的改进脚本,除了多线程功能,已经和sersync的核心功能接近了,即使是同步大量数据,性能也能接近sersync。
sersync工具包无需任何安装,解压即可使用。
[root@xuexi ~]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz [root@xuexi ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@xuexi ~]# cp -a GNU-Linux-x86 /usr/local/sersync [root@xuexi ~]# echo "PATH=$PATH:/usr/local/sersync" > /etc/profile.d/sersync.sh [root@xuexi ~]# source /etc/profile.d/sersync.shsersync目录/usr/local/sersync只有两个文件:一个是二进制程序文件,一个是xml格式的配置文件。
[root@xuexi ~]# ls /usr/local/sersync/ confxml.xml sersync2其中confxml.xml是配置文件,文件内容很容易理解。以下是示例文件内容说明。
<?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"> <host hostip="localhost" port="8008"></host> <debug start="false"/> # 是否开启调试模式,下面所有出现false和true的地方都分别表示关闭和开启的开关 <fileSystem xfs="false"/> # 监控的是否是xfs文件系统 <filter start="false"> # 是否启用监控的筛选功能,筛选的文件将不被监控 <exclude expression="(.*)\.svn"></exclude> <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> # rsync命令的配置段 <localpath watch="/www"> # 同步的目录或文件,同inotify+rsync一样,建议同步目录 <remote ip="172.16.10.5" name="/tmp/www"/> # 目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync <!--remote ip="IPADDR" name="module"--> # 除非下面开启了ssh start,此时name为远程shell方式运行时的目标目录 </localpath> <rsync> # 指定rsync选项 <commonParams params="-az"/> <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> # 是否使用远程shell模式而非rsync daemon运行rsync命令 </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> # 错误重传 <crontab start="false" schedule="600"><!--600mins--> # 是否开启crontab功能 <crontabfilter start="false"> # crontab定时传输的筛选功能 <exclude expression="*.php"></exclude> <exclude expression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </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>以上配置文件采用的是远程shell方式的rsync连接方式,所以无需在目标主机上启动rsync daemon。设置好配置文件后,只需执行sersync2命令即可。该命令的用法如下:
[root@xuexi 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:启用守护进程模式,让sersync2运行在后台 参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍, :即首先让远端目录和本地一致,以后再同步则通过监控实现增量同步 参数-n:指定开启守护线程的数量,默认为10个 参数-o:指定配置文件,默认使用confxml.xml文件 参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块 参数-m:单独启用其他模块,使用 -m socket 开启socket模块 参数-m:单独启用其他模块,使用 -m http 开启http模块 不加-m参数,则默认执行同步程序由此可见,sersync2命令总是会先设置inotify相关的系统内核参数。
所以,只需执行以下简单的命令即可。
[root@xuexi ~]# sersync2 -r -d 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 option: -r rsync all the local files to the remote servers before the sersync work option: -d run as a daemon daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 daemon start,sersync run behind the console config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /www && rsync -az -R --delete ./ -e ssh 172.16.10.5:/tmp/www >/dev/null 2>&1 run the sersync: watch path is: /www第20行高亮标注的即为rsync命令的运行参数,由于rsync执行前会cd到监控目录中,且rsync通过“-R”选项是以/www为根的相对路径进行同步的,所以监控目录自身不会被拷贝到远端,因此在confxml.xml中设置目标目录为/tmp/www,这样本地/www下的文件将同步到目标主机的/tmp/www目录下,否则将会同步到目标主机的/tmp目录下。
对于sersync多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置文件运行即可。
例如:
[root@xuexi ~]# sersync2 -r -d -o /etc/sersync.d/nginx.xml