sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
  1. sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
  2. 相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。
  3. 另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
  4. 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每10个小时对同步失败的文件重新同步。
  5. 本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。
  6. 本项目socket与http插件扩展,满足您二次开发的需要。

具体配置很简单在同步主服务器上开启sersync,在同步目标服务器配置rsync即可。

同步主服务器配置:此处假设主服务器ip为10.0.0.1,同步目标服务器ip为10.0.0.2、10.0.0.3

#wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz

#tar -zvxf sersync2.5_64bit_binary_stable_final.tar.gz

#cd  GNU-Linux-x86

#ls

confxml.xml
sersync2

#vi confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>#hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。
    <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="/data/java/admin/img">#允许远程同步的与本地路径
            <remote ip="10.0.0.2" name="rsync_img"/>#允许远程同步的ip;rsync_img是同步项目的名称。这里说明以下如果相同步多个路径在此处添加,我做过测试是不生效的。我最后是通过制定xml文件的方法做的;
            <remote ip="10.0.0.3" name="rsync_img"/>#允许远程同步的其他服务器ip
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <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"/>
        </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>

    <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/p_w_picpaths"/>
        </localpath>
    </plugin>
</head> 

编辑后保存配置文件

在同步目标服务器配置rsync

#rpm -qa rsync #检测是否安装rsync
rsync-2.6.8-3.1

#vi /etc/rsyncd.conf

uid = root   #此处uid必须是root否则无法同步
gid = nobody
max connections = 200  #最大连接数
timeout = 600
use chroot = no
read only = no  #此处必须为“no”否则会造成同步无法写入

pid file=/var/run/rsyncd.pid

host allow =10.0.0.1 #允许同步的主服务器的IP地址
host allow =192.168.0.1 #允许访问其他服务器地址


[rsync_img]
path = /data/img #本地同步的路径
comment = rsync_img #注释

[rsync_test] #同步的其他项目
path = /data/test
comment = rsync_test

保存

到主服务器

#cd  GNU-Linux-x86

#./sersync2 -r -d

至此sersync配置完毕

 

 

重启sersync:

#ps -ef |grep sersync  &查看sersync进程

例如显示:root     15808     1  0 18:49 ?        00:00:00 /root/shell/sersync2 -r -d

那么则执行

#ps aux |grep '/root/shell/sersync2'|grep -v grep|awk '{print $2}'|awk '{printf("%s ",$1)}'|xargs kill -9

然后重新运行sersync  

#/root/shell/sersync2 -r -d

 

 

补充:

1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。
./sersync -d

2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
./sersync -r

3.查看启动参数帮助
./sersync --help

4.指定配置文件
./sersync -o XXXX.xml

5.指定默认的线程池的线程总数
./sersync -n num

6.不进行同步,只运行插件
./sersync -m pluginName

7.多个参数可以配合使用
./sersync -n 8 -o abc.xml -r -d

8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。
./sersync -r -d

 注意:文章中主服务器与同步服务器的模块名字必须一致。

以上内容摘自:http://dadloveu.blog.51cto.com/715500/354990