单向同步软件

rsync一般是server到client

sersync一般是client到server,sersync可以做到实时监控

sersync+rsync一般是多台服务器之间

双向同步:

unison


sersync监控源系统文件变化,rsync实时同步,没有变化则不同步。

sersync是基于Inotify开发的,类似于Inotify-tools工具


配置:

被同步(目标)服务器:

yum -y install rsync    安装rsync

systemctl enable rsync    将rsync加入到服务启动项

vi  /etc/rsyncd.conf
uid=root                                                  gid/uid要是服务器的相应用户,对目标路径具有操作权限
gid
=root
max connections
=36000
use chroot=no
log file
=/var/log/rsyncd.log
pid file
=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock


CP-apache03】                                                            添加同步模块[CP-apache03]
path
=/opt/tongbu                                                           指定同步过来的文件的存放路径
comment
= xoyo video files
ignore errors
= yes
read only
= no
hosts allow
= 192.168.8.40/26 192.168.138.94/24                        指定源服务器的ip,可以是多个
hosts deny
= *

systemctl start rsync    启动rsync

    如果之前已启动rsync服务,则需要先killall rsync/kill pid,在如上启动

PS:并且rsync要有被同步目录的操作权限,注意权限


同步源服务器(数据源):

安装sersync2,在yum安装源没有sersync2,下载源码包安装

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

tar zxvf sersync2.5.tar.gz

cd sersync

vim confxml.xml

<sersync>

 
<localpath watch="/opt/tongbu">

   
<remote ip="192.168.8.42" name="tongbu"/>

   
<remote ip="192.168.8.39" name="tongbu"/>

 
</localpath>

 
<crontab start="true" schedule="30"/>

 
<plugin name="refreshCDN" start="true"/>

 
</sersync>

    只需要修改,添加相应的被同步服务器的ip。

systemctl enable sersync2.service    将sersync添加到系统服务启动项

systemctl start sersync2.service    启动sersync2服务


/opt/GNU-Linux-x86/sersync2 -r -o /opt/GNU-Linux-x86/confxml.xml

        配置完成,先做一次整体同步 (-r),(-o 指定配置文件confxml.xml)

/opt/GNU-Linux-x86/sersync2 -r -d -o /opt/GNU-Linux-x86/confxml.xml

        之后守护进程模式启动sersync2 (-d)


同步是否成功:

    查看被同步目标服务器的日志:tail /var/log/rsyncd.log


参数说明:

/opt/GNU-Linux-x86/sersync2 -r    进行一次整体同步

/opt/GNU-Linux-x86/sersync2 -d    进程守护模式,后台实时同步

/opt/GNU-Linux-x86/sersync2 -o    指定配置文件

/opt/GNU-Linux-x86/sersync2 -n num    指定默认的线程池的线程总数



sersync2配置文件confxml.xml详解
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="9090"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="true">
     <exclude expression="^((?!original\.).*)$"></exclude>    
                    #只备份源里面文件名是original开头的,后缀名不限定的文件,?!original 代表反向选择
     <!-- <exclude expression="abc"></exclude>
     <exclude expression="p_w_picpath"></exclude>
     <exclude expression="txt"></exclude>
     <exclude expression="xml"></exclude>
     <exclude expression="*"></exclude>
     -->
    </filter>
    <inotify>
     <delete start="false"/>                    #不监控文件或目录的删除
     <createFolder start="true"/>        #监控目录的创建
     <modify start="true"/>                    #监控文件的修改
     <createFile start="true"/>               #监控创建新文件
     <closeWrite start="false"/>
     <moveFrom start="false"/>
     <moveTo start="false"/>
     <attrib start="false"/>
    </inotify>

    <sersync>
     <localpath watch="/data/bookfm/doc/book">   #设置监控的目录
         <remote ip="192.168.100.250" name="book"/>  
                                                     
#指定目标主机的IP地址和模块名(和samba的类似,不是目录的名称)
                                                   
#有几台目标主机,配置这样几条,同时也要在目标上启用rsync守护进程
         <!--<remote ip="192.168.8.39" name="tongbu"/>-->
         <!--<remote ip="192.168.8.40" name="tongbu"/>-->
     </localpath>
     <rsync>
         <commonParams params="-au"/>   #指定rsync 运行时的参数,可根据自身需要更改
         <auth start="true" users="bookbackup" passwordfile="/usr/local/GNU-Linux-x86/rsyncd.secrets"/>
       #启用验证,指定用户名和密码文件(只用写一个密码即可)
       #用户名是目标服务器上rsync配置文件中指定的用户名,密码文件中的密码也目标服务器上rsync指定的密码文件指定的密码
         <userDefinedPort start="false" port="874"/><!-- port=874 -->
         <timeout start="false" time="100"/><!-- timeout=100 -->
         <ssh start="false"/>
     </rsync>
     <failLog path="/var/rsync/rsync_error.log" 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>
配置文件一般根据自己需要修改

更详细文档:http://www.osyunwei.com/archives/7447.html