centos6.5下部署sersync+rsync --daemon同步数据

rsync --daemon端配置

[root@rsync-daemon etc]# /etc/init.d/iptables stop
[root@rsync-daemon ~]# dos2unix rsyncd.conf 
dos2unix: converting file rsyncd.conf to UNIX format ...
[root@rsync-daemon ~]# cp rsyncd.conf /etc/
[root@rsync-daemon ~]# cd /etc/
[root@rsync-daemon etc]# useradd -M -s /sbin/nologin rsync
[root@rsync-daemon etc]# vi rsyncd.conf 
[root@rsync-daemon etc]# cat rsyncd.conf 
[root@rsync-daemon data]# cat /etc/rsyncd.conf
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[data]
path = /data/
ignore errors
read only = false
list = false
hosts allow = 10.0.0.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#rsync_config_______________end

[root@rsync-daemon etc]# mkdir /data
[root@rsync-daemon etc]# chown -R rsync.rsync /data
[root@rsync-daemon etc]# echo "rsync_backup:123456" >>/etc/rsync.password
[root@rsync-daemon etc]# chmod 600 /etc/rsync.password 
[root@rsync-daemon etc]# echo "rsync --daemon" >>/etc/rc.local
[root@rsync-daemon data]# rsync --daemon --config=/etc/rsyncd.conf
[root@rsync-daemon etc]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2821/rsync 
tcp 0 0 :::873 :::* LISTEN 2821/rsync

sersync数据推送端配置

[root@sersync tools]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz 
[root@sersync tools]# ll
total 716
drwxr-xr-x 2 root root 4096 Oct 26 2011 GNU-Linux-x86
-rw-r--r-- 1 root root 727290 Feb 26 18:34 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sersync tools]# cp -a GNU-Linux-x86 /usr/local/sersync
[root@sersync tools]# cd /usr/local/sersync/
[root@sersync sersync]# ll
total 1772
-rwxr-xr-x 1 root root 2214 Oct 26 2011 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 sersync2
[root@sersync sersync]# echo "PATH=/usr/local/sersync:$PATH" >/etc/profile.d/sersync.sh 
[root@sersync sersync]# source /etc/profile.d/sersync.sh
[root@sersync sersync]# echo "123456" >/etc/rsync.password
[root@sersync sersync]# chmod 600 /etc/rsync.password
[root@sersync sersync]# rsync -avzP /data/ rsync_backup@10.0.0.4::data --password-file=/etc/rsync.password        #<===首先测试rsync同步是否成功
[root@sersync sersync]# vi confxml.xml 
[root@sersync sersync]# cat 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"/>                                                                 #<===系统是否是xfs
<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="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>

<sersync>                                                                                #<===这里配置rsync --daemon端的ip地址,及模块的名称(带!是注释掉了的)
<localpath watch="/data">
<remote ip="10.0.0.4" name="data"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>                                                #<===调用的rsync的参数
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>                          #<===启用rsync及用户及指定密码文件
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->                                                             #<===设置超时时间
<ssh start="false"/> 
</rsync>
<failLog path="/usr/local/sersync/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/images"/>
</localpath>
</plugin>
</head>

[root@sersync sersync]# sersync2 -r -d -o /usr/local/sersync/confxml.xml    #<===启动sersync
[root@sersync sersync]# echo "sersync2 -r -d -o /usr/local/sersync/confxml.xml" >>/etc/rc.local    #<===加入开机自启动

以上是所有配置,经测试,同步成功,速度极快.......

小结:sersync优点(sersync可以过滤重复事件减轻负担、自带crontab功能、多线程调用rsync、失败重传。)

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

附:sersync配置文件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文件系统,如果是xfs文件系统,就开启为true
    <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>                       # sersync调用rsync socket网络套接字服务端的相关配置
        <localpath watch="/www">    # 本地(sersync)需要同步的目录或文件,同inotify+rsync一样,建议同步目录
            <remote ip="172.16.10.5" name="/tmp/www"/>  # 这里是rsync daemon端的服务器IP地址及模块名(模块名可以对应多个),rsync服务器端要以daemon模式先运行好rsync服务
            <!--remote ip="IPADDR" name="module"-->     # 除非下面开启了ssh start,此时name为远程shell方式运行时的目标目录
        </localpath>
        <rsync>                      # 指定rsync参数及选项
            <commonParams params="-az"/>              #默认参数是-zrtuz ,生产环境使用-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-->  # 将传输失败的文件写入此sh文件中,出现错误时重传
        <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>

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

sersync2命令相关参数(sersync2命令总是会先设置inotify相关的系统内核参数)

[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参数,则默认执行同步程序

更多知识及原理深入了解,查看好友博客 http://www.cnblogs.com/f-ck-need-u/p/7220193.html

原文地址:https://www.cnblogs.com/blog-tim/p/10440330.html