使用 Linux 2.6 内核的 inotify 监控 Linux 文件系统事件,被监听目录下如果有文件发生修改,sersync 将通过内核自动捕获到事件,并将该文件利用 rsync 同步到多台远程服务器。sersync 仅仅同步发生增、删、改事件的单个文件或目录,不像rsync镜像同步那样需要比对双方服务器整个目录下数千万的文件,并且支持多线程同步,因此效率非常高。
sersync介绍
sersync主要用于服务器同步,web镜像等功能。基于boost1.41.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失败的文件重新同步。
本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
本项目socket与http插件扩展,满足您二次开发的需要。
PS:金山的同学开源出来的,不得不推荐鼓励下。
sersync2完全安装配置说明(一)
基本功能使用
当前版本的sersync依赖于rsync进行同步。如下图所示,在同步主服务器上开启sersync,将监控路径中的文件同步到目标服务器,因此需要在主服务器配置sersync,在同步目标服务器配置rsync。对于rsync配置,在google上可以找到很多资料,以下只是一些必要的配置和使用说明,仅供参考,请根据您的实际情况修改。
如图所示,需要在同步主服务器上配置sersync,在同步目标服务器配置rsync,并在目标服务器开启rsync守候进程,这样在主服务器产生的文 件,就会被sersync实时同步到多个目标服务器。在centos系统下默认已经安装了rsync,只需进行配置,并开启rsync守候进程即可。
配置同步目标服务器rsync
在多台目标服务器上配置如下:
上面配置文件,表明允许主服务器(假设ip为192.168.0.100)访问,rsync同步模块名为[tongbu1],将同步过来的文件放入path指定的目录/opt/tongbu1。如果有多台从服务器,则每一台都需要进行类似的rsync配置,上面的uid gid要换成您服务器的相应用户,主意rysnc要有对被同步目录的操作权限。配置好之后,使用如下命令,开启rsync守护进程:
在主服务器上安装配置sresync
在google code下载sersync的可执行文件版本,里面有配置文件与可执行文件,这里用sersync2.5进行说明,新版本配置形式类似。
1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。
过程如下:
2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效。
3.查看启动参数帮助
4.指定配置文件
对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。
5.指定默认的线程池的线程总数
例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。
6.不进行同步,只运行插件
例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。
7.多个参数可以配合使用
表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。
8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。
sersync2 完全安装配置说明(二) —-可选功能与xml高级配置
XML文件说明
sersync可选功能是通过xml配置文件来实现的,基本配置文件如下:
01 | <? xml version = "1.0" encoding = "ISO-8859-1" ?> |
02 | < head version = "2.5" > |
03 | < host hostip = "localhost" port = "8008" ></ host > |
04 | < filter start = "false" > |
05 | < exclude expression = "(.*)\.gz" ></ exclude > |
06 | < exclude expression = "^info/*" ></ exclude > |
07 | </ filter > |
08 | < inotify > |
09 | < delete start = "true" /> |
10 | < createFolder start = "true" /> |
11 | < createFile start = "true" /> |
12 | </ inotify > |
13 | < debug start = "false" /> |
14 | < sersync > |
15 | < localpath watch = "/opt/tongbu" > |
16 | < remote ip = "192.168.0.104" name = "tongbu1" /> |
17 | <!--<remote ip="192.168.8.39" name="tongbu"/>--> |
18 | <!--<remote ip="192.168.8.40" name="tongbu"/>--> |
19 | </ localpath > |
20 | < rsync > |
21 | < commonParams params = "-artuz" /> |
22 | < auth start = "false" users = "root" passwordfile = "/etc/rsync.pas" /> |
23 | < userDefinedPort start = "false" port = "874" /> <!-- port=874 --> |
24 | < timeout start = "false" time = "100" /> <!-- timeout=100 --> |
25 | < ssh start = "false" /> |
26 | </ rsync > |
27 | < failLog path = "/tmp/rsync_fail_log.sh" timeToExecute = "60" /> <!--default every 60min execute once--> |
28 | < crontab start = "false" schedule = "600" > <!--600mins--> |
29 | < crontabfilter start = "false" > |
30 | < exclude expression = "*.gz" ></ exclude > |
31 | < exclude expression = "info/*" ></ exclude > |
32 | </ crontabfilter > |
33 | </ crontab > |
34 | < plugin start = "false" name = "command" /> |
35 | </ sersync > |
下面做逐行的进行解释说明:
hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。
filter文件过滤功能
对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。
将start设置为 true,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以”.gz”结尾的文件与过滤监控目录下的info路径(监控路径/info /*),可以根据需要添加,但开启的时候,自己测试一下,正则表达式如果出现错误,控制台会有提示。相比较使用rsync 的exclude功能,被过滤的路径,不会加入监控,大大减少rsync的通讯量。
inotify监控参数设定(优化)
对于inotify监控参数可以进行设置,根据您项目的特点优化srsync。
对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事 件close_write,同样可以实现文件完整同步。
注意:强将createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控。
Debug开启
设置为true,开启debug模式,会在sersync正在运行的控制台,打印inotify事件与rsync同步命令。
XFS文件系统
对于xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作.
文件监控与远程同步设置
详见sersync2 完全安装配置说明(一) —-基本功能使用
Rsync参数配置
commonParams可以用户自定义rsync参数,默认是-artuz
auth start=”false” 设置为true的时候,使用rsync的认证模式传送,需要配置user与passwrodfile(–password-file=/etc/rsync.pas),来使用。userDefinedPort 当远程同步目标服务器的rsync端口不是默认端口的时候使用(–port=874)。timeout设置rsync的timeout时间(–timeout=100)。ssh 使用rsync -e ssh的方式进行传输。
失败日志脚步配置
对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。
Crontab定期整体同步功能
crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了crontab功 能,还可以进一步保证各个服务器文件一致,如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销。schedule这个参数是设置crontab的时间间隔,默认是600分钟
如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对 应与过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。
插件设置
当设置为true的时候,将文件同步到远程服务器后会调用name参数指定的插件。详见请看插件设置。
sersync2 完全安装配置说明(三) —-插件基本配置和使用
插件相关xml
01 | < sersync > |
02 | ...... |
03 | < plugin start = "false" name = "command" /> |
04 | </ sersync > |
05 | < plugin name = "command" > |
06 | < param prefix = "/bin/sh" suffix = "" ignoreError = "true" /> |
07 | < filter start = "false" > |
08 | < include expression = "(.*)\.php" /> |
09 | < include expression = "(.*)\.sh" /> |
10 | </ filter > |
11 | </ plugin > |
12 | < plugin name = "socket" > |
13 | < localpath watch = "/opt/tongbu" > |
14 | < deshost ip = "192.168.138.20" port = "8009" /> |
15 | </ localpath > |
16 | </ plugin > |
17 | < plugin name = "refreshCDN" > |
18 | < localpath watch = "/data0/htdocs/cms.xoyo.com/site/" > |
19 | < cdninfo domainname = "ccms.chinacache.com" port = "80" username = "xxxx" passwd = "xxxx" /> |
20 | < sendurl base = "http://pic.xoyo.com/cms" /> |
21 | < regexurl regex = "false" match = "cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" /> |
22 | </ localpath > |
23 | </ plugin > |
如上面的xml所示,其中plugin标签设置为true时候,在同步文件或路径到远程之后后,调用插件。通过name参数指定需要执行的插件。目前支持的有command refreshCDN socket http四种插件。http插件目前由于兼容性原因去除,以后会重新加入。
以下模块(command refreshCDN http socket)可以单独使用(发生文件改变不同步只调用插件),只需在命令行下使用-m 参数即可。如果需要作为插件与同步程序一起使用,见同步程序说明的插件配置。
command插件
当文件同步完成后,会调用command插件,如同步文件是test.php,则test.php文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行
如果suffix 设置了,则会放在inotify事件test.php之后
如果ignoreError为true,则会添加>/dev/null 2>&1
当然还可以设置command的filter,当filter为ture,include可以只对正则匹配到的文件,调用command。
“refreshCDN”,就在同步过程中将文件发送到目 的服务器后刷新cdn接口。如果不想使用,则将start属性设为false即可。如果需要使用其他插件,则查看其他plugin标签,将插件名称改为 xml中其它插件的名称即可。该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置。刷新CDN模块需要配置的xml文件如下。
其中 localpath watch 是需要监控的目录。
cdnifo标签制定了cdn接口的域名,端口号,以及用户名与密码。
sendurl标签是需要刷新的url的前缀。
regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,
举例:
如果产生文件事件为:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
经过上面的match正则匹配后,最后刷新的路径是:
http://pic.xoyo.com/cms/jx3/a/123.txt;
如果regex属性为false,最后刷新的路径是
http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
socket插件
socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息。
Http插件
http插件,可以向指定域名的主机post,inotify监控的事件。
单独运行插件
插件也可以单独使用,即不对远程目标机进行同步,直接调用插件:
只调用command插件
只调用refreshCDN插件
只调用socket插件
只调用http插件