sersync优点

1.sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
2.sersync配置很简单,其中bin目录下已经有静态编译好的2进制文件,配合bin目录下的xml配置文件直接使用即可。
3.sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
4.sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
5.sersync自带crontab功能,只需在xml配置文件中开启,即可按要求隔一段时间整体同步一次。无需再额外配置crontab功能。
6.sersync可以二次开发。

sersync项目地址:https://code.google.com/archive/p/sersync/,在此网站中有下载、安装、使用等等详细的中文介绍。
sersync下载地址:https://code.google.com/archive/p/sersync/downloads。

(1)当同步的目录数据量不大时,建议使用rsync+inotify

(2)当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync


sersync工具包无需任何安装,解压即可使用。

tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
cp -a GNU-Linux-x86 /usr/local/sersync
echo "PATH=$PATH:/usr/local/sersync" > /etc/profile.d/sersync.sh
source /etc/profile.d/sersync.sh

sersync2.5.4_64bit_binary_stable_final.tar.gz



sersync目录/usr/local/sersync只有两个文件:一个是二进制程序文件,一个是xml格式的配置文件

[root@xuexi ~]# ls /usr/local/sersync/
confxml.xml  sersync2


其中confxml.xml是配置文件,文件内容很容易理解。以下是示例文件内容说明。

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>           # 是否开启调试模式,下面所有出现false和true的地方都分别表示关闭和开启的开关
    <fileSystem xfs="false"/>        # 监控的是否是xfs文件系统
    <filter start="false">           # 是否启用监控的筛选功能,筛选的文件将不被监控
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>                         # 监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
        <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>                       # rsync命令的配置段
        <localpath watch="/www">    # 同步的目录或文件,同inotify+rsync一样,建议同步目录
            <remote ip="172.16.10.5" name="/tmp/www"/>  # 目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync
            <!--remote ip="IPADDR" name="module"-->     # 除非下面开启了ssh start,此时name为远程shell方式运行时的目标目录
        </localpath>
        <rsync>                      # 指定rsync选项
            <commonParams params="-az"/>
            <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"/>      # 是否使用远程shell模式而非rsync daemon运行rsync命令
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  # 错误重传
        <crontab start="false" schedule="600"><!--600mins-->    # 是否开启crontab功能
            <crontabfilter start="false">       # crontab定时传输的筛选功能
                <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/images"/>
        </localpath>
    </plugin>
</head>

以上配置文件采用的是远程shell方式的rsync连接方式,所以无需在目标主机上启动rsync --daemon。设置好配置文件后,只需执行sersync2命令即可。该命令的用法如下:


[root@xuexi sersync]# sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_____________________________________________________________
参数-d:启用守护进程模式,让sersync2运行在后台
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍,
      :即首先让远端目录和本地一致,以后再同步则通过监控实现增量同步
参数-n:指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

_____________________________________________________________

由此可见,sersync2命令总是会先设置inotify相关的系统内核参数。

所以,只需执行以下简单的命令即可。

[root@xuexi ~]# sersync2 -r -d
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r      rsync all the local files to the remote servers before the sersync work
option: -d      run as a daemon
daemon thread num: 10
parse xml config file
host ip : localhost     host port: 8008
daemon start,sersync run behind the console
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /www && rsync -az -R --delete ./  -e ssh 172.16.10.5:/tmp/www >/dev/null 2>&1
run the sersync:
watch path is: /www

上面

execute command: cd /www && rsync -az -R --delete ./  -e ssh 172.16.10.5:/tmp/www >/dev/null 2>&1

即为rsync命令的运行参数,由于rsync执行前会cd到监控目录中,且rsync通过"-R"选项是以/www为根的相对路径进行同步的,所以监控目录自身不会被拷贝到远端,因此在confxml.xml中设置目标目录为/tmp/www,这样本地/www下的文件将同步到目标主机的/tmp/www目录下,否则将会同步到目标主机的/tmp目录下。

对于sersync多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置文件运行即可。

例如:

[root@xuexi ~]# sersync2 -r -d -o /etc/sersync.d/nginx.xml


设置sersync监控开机自动执行

vi /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml




添加脚本监控sersync是否正常运行

vi  /home/crontab/check_sersync.sh 

#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi
chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限

vi /etc/crontab #编辑,在最后添加下面一行

*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本
service crond reload  #重新加载服务


至此,Linux下Rsync+sersync实现数据实时同步完成。









<?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="/data/backup">
            <remote ip="10.10.0.16" name="back"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
            <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="true" schedule="60"><!--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/images"/>
        </localpath>
    </plugin>
</head>