sersync

sersync

利用inotify和rsync技术实现对服务器数据实时同步的解决方案

优点

1.使用c++编写,对linux系统文件产生的临时文件和重复文件操作进行过滤,在结合rsync同步的时候,会减少运行时消耗本地及网络资源,因此它速度更快
2.配置简单
3.使用多线程同步
4.自带出错处理机制,通过失败队列对出错的文件进行重新同步
5.自带crontab功能,只需要在xml配置文件中开启
6.自带socket和http的协议拓展

安装及配置

1.下载
    https://github.com/wsgzao/sersync

2.安装
    tar zxvf /tmp/sersync2.5.4_64bit_binary_stable_final.tar.gz

3.配置
    mv /tmp/GNU-Linux-x86 /usr/local/sersync
    cd /usr/local/sersync

    创建配置文件
        mkdir conf

    创建对应模块配置文件
        cp ../confxml.xml test_confxml.xml

配置文件说明
    1.针对插件的保留字段,和同步没关系
        <host hostip="localhost" port="8008"></host>

    2.debug开关,在运行的控制台上打印inotify事件,和rsync同步命令
        <debug start="false"/>

    3.对于xfs文件系统用户,需要将这个开关开启
        <fileSystem xfs="false"/>

    4.过滤同步文件功能
        <filter start="false">
            <exclude expression="(.*)\.svn"></exclude>
            <exclude expression="(.*)\.gz"></exclude>
            <exclude expression="^info/*"></exclude>
            <exclude expression="^static/*"></exclude>
        </filter>

    5.inotify监控参数设定
        <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>

    6.同步的模块
        <localpath watch="/test">                       待同步的目录
                <remote ip="10.0.0.1" name="test"/>     rsncd.conf中的模块名
        </localpath>

    7.rsync相关配置
        <rsync>
            <commonParams params="-aruz"/>                                                          rsync同步时的参数
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>            密码文件配置
            <userDefinedPort start="false" port="874"/>                                             端口配置
            <timeout start="true" time="100"/>                                                      超时时间配置
            <ssh start="false"/>
        </rsync>

    8.定时失败文件同步(每隔60分钟再同步一次)
        <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/>

    9.定期全量同步,加过滤
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>

    10.插件使用开启
        <plugin start="false" name="command"/>

插件介绍

1.command插件
    <plugin name="command">
    <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongb    u/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
    </plugin>

当文件同步完成后,会调用command插件,如同步的文件是test.sh,则test.sh文件在改动后,调用rsync同步到远程服务器后,调用command插件,执行:/bin/sh test.sh suffix>/dev/null 2>&1
如果suffix设置了,则会放在inotify事件test.sh之后,如果ignoreError为true,则会添加>/dev/null 2>&1,也可以设置command的filter

2.sokect插件
    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
    开启该模块,则向指定ip与端口发送inotify所产生的路径信息

3.refreshCDN插件
    <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">                                                    需要监控的目录
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passw    d="xxxx"/>             定义cdn的域名,端口,用户名号,密码
            <sendurl base="http://pic.xoyo.com/cms"/>                                                           需要刷新的url的前缀
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/    images"/>              属性为true的时候,使用match属性的正则语句匹配inotify返回的路径信息,将正则匹配到的部分作为url的一部分
        </localpath>
    </plugin>
    当文件更新之后,将文件发送到目的服务器,刷新cdn接口
坚持原创技术分享,您的支持将鼓励我继续创作!