公司有个文件同步服务器,以前使用rsync同步文件,而且小文件居多,当源文件增多以后,在执行rsync命令时,前期的校验时间很长,如果只修改了很少的文件,而校验时间则占到整个同步时间的80%以上,这样很体验不好,以前了解过金山开源的sersync,本来以为这个可以解决rsync前期校验慢的问题,又看了一遍介绍才发现这个底层也是基于rsync实现的,sersync的方便之处是把inotify和rsync结合在一起了,在需要实时同步的环境下,可以不用自己在脚本中调用inotify接口,利用sersync直接实时同步文件,刚好线上也有需要实时同步的环境,所以就拿来试试。该软件的使用还是很方便的。关于该软件的官方介绍如下:

    sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
    1 sersync是使用c++编写,在结合rsync同步的时候,节省了运行时耗和网络资源。
    2 相比较上面两个项目,sersync配置起来很简单,已编译好的软件包中就两个文件。
    3 相比较其他脚本实现过程,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
    4 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
    5 项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求执行。
    6 本项目socket与http插件扩展,满足您二次开发的需要。

    sersync部署在源服务器上,在目标服务器上部署好普通的rsync服务后,在源服务器上启动sersync即可,sersync通过解析xml配置文件来获取相关rsync时的一些策略。

实验环境:

    10.1.6.153 ubuntu12.04 目标服务器

    10.1.6.79  ubuntu12.04 源文件服务器

配置目标服务器:

安装

apt-get install rsync


修改配置文件

#/etc/default/rsync
RSYNC_ENABLE=true
RSYNC_OPTS='--address=10.1.6.153'
RSYNC_NICE=''

#/etc/rsyncd.conf
uid=root
gid=root
max connections=0
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock

[sersync]
path = /opt/sersync
uid = www-data
gid = www-data
list = yes
ignore errors = yes
read only = no
hosts allow =  10.1.6.79



配置源服务器

下载安装

wget 'http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz'
tar zcvf sersync2.5.4_64bit_binary_stable_final.tar.gz


修改配置文件

先看解压后的文件怎样使用

#./sersync2 -h
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序


修改配置文件confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<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/sersync">
	    <remote ip="10.1.6.153" name="sersync"/>
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/>
	    <auth start="false" users="www-data" passwordfile="/etc/rsyncd.s"/>
	    <userDefinedPort start="true" port="873"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

</head>

执行初始化

#可以在第一次同步之前把所有文件都先整体同步一次
./sersync2 -r
#放入后台运行
./sersync2 -d