Introduce: http://code.google.com/p/sersync/
- sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
- 相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
- 另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
- 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失败的文件重新同步。
- 本项目自带crontab功能,只需在 xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
- 本项目socket与http插件扩展,满足您二次开发的需要。
- Sersync is developed by c ++, it can filter lots of temporary files and duplicated inotify events generated by linux file system(for details see the appendix, the filter function is difficult for script program to implement),so it can save more time and network resources.
- Compare the above two projects, sersync is easy to use.You can use the binary executable file and the xml configuration file directly,both of them are in the bin directory.
- Compared to other script programs,sersync uses multiple threads to synchronize , especially used in synchronizing large files, it can ensure multiple servers to keep synchronization in real-time.
- The project has an error handling mechanism,add all the failed event to failure queue and try again, if still fails, sersync will retry every 10 hours until it is successfully synchronized .
- The project has crontab function.using the xml configuration file, you can according to your requirements, sync the whole monitor directory from time to time.
- The project has socket and http plugin extensions which can meet your secondary development needs.
Design Frame
Compile
如果使用linux 系统,一般情况下,不需要编译,直接使用bin目录下的可执行文件和xml配置文件即可。 src目录下放的是源码
在sersync 目录下执行make命令,就会将生成的2进制文件放入bin目录下。
src directory: the source files.
include directory: the boost head files(1.41.0).
lib directory: static libraries.
bin directory: binary executable file and xml configuration file.
Install
Config Rsync before install
注意在使用前,需要对每台服务器都配置并开启rsync守护进程。通常配置方法如下:
vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
【tongbu】
path=/opt/tongbu
comment = xoyo video files
ignore errors
read only = no
hosts allow = 192.168.8.40/26 192.168.138.94/24
hosts deny = *
Configuration parameters, please google.
Then open rsync daemon on each server:
rsync --daemon
Install sersync
由于大部分库都是静态编译的,所以在被监控服务器上,修改好配置文件后,直接执行./sersync2.1 即可。
tar zxvf sersync2.1.tar.gz
cd sersync
before using , write the xml configuration file
vi confxml.xml
synchronization funciton config
Just modify the sersync tab as follows:
<sersync>
<localpath watch="/opt/tongbu">
<remote ip="192.168.8.42" name="tongbu"/>
<remote ip="192.168.8.39" name="tongbu"/>
</localpath>
<crontab start="true" schedule="30"/>
<plugin name="refreshCDN" start="true"/>
</sersync>
plugin config
如上面的xml所示,其中plugin标签是在同步过程中,使用插件。当前使用的是刷新cdn插件”refreshCDN”,
<plugin name="refreshCDN" start="true"/>
该插件根据 chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置。刷新CDN插件需要配置的xml文件如下:
<plugin name="refreshCDN">
- <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="yourname" passwd="yourpasswd" />
<sendurl base="http://pic.xoyo.com/cms" />
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" />
</localpath>
</plugin>
其中 localpath watch 是需要监控的目录,将会覆盖sersync中的监控目录。
cdnifo标签指定了cdn接口的域名,端口号,以及用户名与密码。
regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配 inotify 返回的路径信息,并将正则匹配到的部分作为url一部分,
/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
http://pic.xoyo.com/cms/jx3/a/123.txt;
http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
socket 与 http 接口很简单,填写xml文件即可。
<plugin name="refreshCDN">
- <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="yourname" passwd="yourpasswd" />
<sendurl base="http://pic.xoyo.com/cms" />
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" />
</localpath>
</plugin>
<plugin name="refreshCDN" start="true"/>
for example,if inotify event is :
/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
after regular match, the path to send to cdn interface is :
http://pic.xoyo.com/cms/jx3/a/123.txt;
if regex attribute is set to false ,the path send to cdn interface is:
http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
socket and http plugin is very simple,just specify the info in the xml.
execute
synchronize or synchronize + plugin
./sersync2.1 -h
./sersync2.1 -r
开启守护进程模式,在后台运行(Open the daemon mode, running in the background)
./sersync2.1 -d
./sersync2.1 -o
./sersync2.1 -n
通常使用的方法是 : Commonly use following command to execute:
./sersync2.1 -d -r
run the plugin Only
可以不调用同步程序,当有inotify事件时候,不执行同步程序,只调用插件,形式如下:
./sersync2.1 -d -m refreshCDN
socket模块,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息:
./sersync2.1 -d -m socket
http模块接口,可以向指定域名的主机post,inotify监控的事件:
./sersync2.1 -d -m http
log file description
appendix
http://hi.baidu.com/johntech/blog/item/e4a31a3db1ee1ce755e723f4.html
为什么脚本监控效率低? 因为脚本监控,即使使用–exclude正则语法也无法过滤掉一些文件系统产生的临时文件和临时事件, 造成rsync反复执行,详细文章如下:
http://hi.baidu.com/johntech/blog/item/e4a31a3db1ee1ce755e723f4.html
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sersync2.5beta1
1.修改sersync2.4中,如果使用–password-file带密码同步,-r 没有调用 –password-file,对源与目的整体同步一遍bug
3.可以使用delete标签指定是否对本地的 delete事件进行监控。
4. rsync.fail.log.sh 会记录调用rsync错误号,便于以后改进。
5.改掉本地host ip 与远程 remote ip 相同时候,不进行同步的限制。现在可以将本地与远程都设为127.0.0.1进行本机同步了,无须把其中一个设置为localhost.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sersync2.4
修正重大 bug,当删除一次文件后,无法同步的错误,这个错误在低版本的sersync都存在,抱歉。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sersync2.3
修正同步文件名中存在’$’ ‘(‘ ‘)’等需要转义字符时发生错误的情况。
诸如 要过滤*.php 或者dirname1/*之类的文件,只需在配置文件中填写该正则表达即可。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++