rsync+sersync实现文件同步

一、目的

        A服务器:11.11.11.11 源服务器

       B服务器:22.22.22.22 目标服务器,既同步备份的目标

       将A服务器的文件同步到B服务器上

二、rsync环境部署

       1.关闭selinux,在/etc/sysconfig/selinux 这个文件中,设置SELINUX=disable

       2.开通防火墙的873端口   -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

       3.安装rsync   yum install -y rsync

       4.配置rsync,配置文件位于/etc/rsync.conf。两个服务器都要安装

log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid  #pid文件的存放位置
lock file = /var/run/rsync.lock  #支持max connections参数的锁文件
secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件。这个文件也可以不要,但是安全性上可能会有些问题。
motd file = /etc/rsyncd.Motd  #rsync启动时欢迎信息页面文件位置(文件内容自定义)
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
max connections = 200 #最大连接数
timeout = 600  #设置超时时间
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份


[web] #自定义名称
path = /home/web #rsync服务端数据目录路径
comment = web #模块名称与[web]自定义名称相同
port=873  #默认端口
read only = no  #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
auth users = web_user #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开,也可不设置
hosts allow = 22.22.22.22  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开,也可不设置

如果有多个文档目录需要同步,只要接着创建[web]模块就好了。rsync不会帮助用户主动创建文件夹,所以同步目录需要用户自己提前创建好。

        5.创建同步的用户与密码文件,记得要设置文件权限600

           在B服务器上: echo "user:password" >>/etc/rsync.passwd        chmod 600 /etc/rsync.passwd

           在A服务器上: echo "password" >>/etc/rsync.passwd        chmod 600 /etc/rsync.passwd

           如果不想要密码,这步可以省略。

        6.启动rsync     rsync  --daemon --config=/etc/rsyncd.conf

        7.配置开机自启动    echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

 三、sersync环境配置

         在A服务器上下载安装sersync。

         1.创建一个文件夹存放sersync文件    mkdir /usr/local/sersync

         2.从sersync官网下载压缩包,解压文件   

            cd /usr/local/sersync   

            wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

            tar -zxvf  sersync2.5.4_64bit_binary_stable_final.tar.gz

         3.配置sersync,配置文件为confxml.xml文件。路径/usr/locla/sersync/GNU-Linux-x86/confxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   # 设置本地IP和端口
   <host hostip="localhost" port="8008"></host>
   # 开启DUBUG模式  
   <debug start="false"/>
   # 开启xfs文件系统
   <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="true"/>
       <closeWrite start="true"/>
       <moveFrom start="true"/>
       <moveTo start="true"/>
       <attrib start="true"/>
       <modify start="true"/>
</inotify>
   <sersync>
   # 本地同步的目录路径
       <localpath watch="/home/web">
   # 远程IP和rsync模块名  
           <remote ip="22.22.22.22" name="web"/>  
           <!--<remote ip="192.168.8.39" name="tongbu"/>-->
           <!--<remote ip="192.168.8.40" name="tongbu"/>-->
       </localpath>
       <rsync>
   # rsync指令参数
           <commonParams params="-auvzP"/>
   # rsync同步认证 没有密码就设置为“false”
           <auth start="true" users="user" passwordfile="/etc/rsync.passwd"/>
   # 设置rsync远程服务端口,远程非默认端口则需打开自定义
           <userDefinedPort start="false" port="874"/><!-- port=874 -->
   # 设置超时时间
           <timeout start="true" time="100"/><!-- timeout=100 -->
   # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
           <ssh start="false"/>
       </rsync>
    # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
       <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    # 设置rsync+crontab定时传输,默认关闭
       <crontab start="false" schedule="600"><!--600mins-->
           <crontabfilter start="false">
               <exclude expression="*.php"></exclude>
               <exclude expression="info/*"></exclude>
           </crontabfilter>
       </crontab>
   # 设置sersync传输后调用name指定的插件脚本,默认关闭
       <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>

          基本上只要修改标注的三个位置就可以使用了,其他位置可以根据自己的需求来更改。不改使用默认配置也是可以的。

       4.启动sersync   /usr/local/sersync/GNU-Linux-x86/sersync2 -r -d -o /usr/local/sersync/GNU-Linux-x86/confxml.xml

          如果要重启sersync程序  killall sersync2 && /usr/local/sersync/GNU-Linux-x86/sersync2 -r -d -o /usr/local/sersync/GNU-Linux-x86/confxml.xml

       5.设置开启自启动   echo "/usr/local/sersync/GNU-Linux-x86/sersync2 -r -d -o /usr/local/sersync/GNU-Linux-x86/confxml.xml" >> /etc/rc.local

四、测试检验

      在A服务器的同步目录/home/web下,创建删除文件,看看B服务器是否同步增删了文件。如果成功,说明同步已经配置完成了。

      完成上述四步之后,只是完成了一个从A到B的单向同步过程,在B服务器上的增改,都是不会影响到A服务器的。如果需要双向同步,也很简单方便,只要在B服务器上也部署一个sersync服务即可。步骤同三。但是密码文件记得需要修改一下。

原文地址:https://www.cnblogs.com/zhuxiangru/p/10716904.html