服务器文件双向、多向同步rsync+sersync

  1. 关闭selinux

     getenforce
    
     vim /etc/sysconfig/selinux
    
     SELINUX=disabled
    
     ESC
     :wq
    
     setenforce 0
     getenforce
    
  2. 安装rsync

     yum install rsync -y
    
  3. 配置rsyncd.conf

     # uid、gid决定被同步过来的文件所属用户所属组
     # []中是一个自己指定的同步计划的名字(字母数字下划线)
     # path是被同步文件的存放位置
     # hosts allow这里注意,每一台服务器都只写其它服务器IP
     # hosts allow、hosts deny格式
         # *(所有IP)
         # 192.168.0.2/24(IP段)
         # 192.168.0.2 192.168.0.3(指定多个IP)
    
     vim /etc/rsyncd.conf
    
     uid = www
     gid = www
     use chroot = yes
     max connections = 0
     log file = /tmp/rsyncd.log
     motd file = /tmp/rsyncd.motd
     pid file = /var/run/rsyncd.pid
     lock file = /var/run/rsyncd.lock
     secrets file = /etc/rsyncd.pswd
    
     [tongbu1]
         path = /www/file1
         comment = xoyo video files
         ignore errors = yes
         read only = no
         hosts allow = 192.168.0.2 192.168.0.3
         hosts deny = *
    
     [tongbu2]
         path = /www/file2
         comment = xoyo video files
         ignore errors = yes
         read only = no
         hosts allow = 192.168.0.2 192.168.0.3
         hosts deny = *
    
     ESC
     :wq
    
  4. 配置密码文件

     vim /etc/rsyncd.pswd
    
     root:**************
    
     ESC
     :wq
    
     chmod 600 /etc/rsyncd.pswd
    
  5. 创建同步目录

     useradd www
     mkdir /www
     mkdir /www/file1
     mkdir /www/file2
     chown -R www:www /www/file1
     chown -R www:www /www/file2
    
  6. 启动并设置开机启动

     systemctl daemon-reload
     systemctl enable rsyncd
     systemctl start rsyncd
    
  7. 开启防火墙相关端口

     yum install firewalld -y
     systemctl enable firewalld
     systemctl start firewalld
     firewall-cmd --zone=public --permanent --add-port=873/tcp
     firewall-cmd --zone=public --permanent --add-port=874/tcp
     firewall-cmd --reload
    
  8. 安装sersync源码地址

     tar -zxvf sersync2.5.4_64.tar.gz
     mv GNU-Linux-x86/ /usr/local/sersync
    
  9. 配置sersync(各服务器都执行)

     cd /usr/local/sersync
     cp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu1.xml
     cp /usr/local/sersync/confxml.xml /usr/local/sersync/tongbu2.xml
    
     vim /usr/local/sersync/tongbu1.xml
    
     <sersync>
         <localpath watch="/www/file1">
             <!--这里注意,每一台服务器都只写其它服务器IP-->
             <!--<remote ip="192.168.0.2" name="tongbu2"/>-->
             <remote ip="192.168.0.3" name="tongbu1"/>
         </localpath>
         <rsync>
             <commonParams params="-artuz"/>
             <auth start="true" users="root" passwordfile="/etc/rsyncd.pswd"/>
             <userDefinedPort start="false" port="874"/>
             <timeout start="false" time="100"/>
             <ssh start="false"/>
         </rsync>
         <!--60分钟进行一次同步失败文件重新同步-->
         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
         <!--600分钟进行一次全目录同步-->
         <crontab start="true" schedule="600">
             <crontabfilter start="false">
                 <exclude expression="*.php"></exclude>
                 <exclude expression="info/*"></exclude>
             </crontabfilter>
         </crontab>
         <plugin start="false" name="command"/>
     </sersync>
    
     ESC
     :wq
    
     vim /usr/local/sersync/tongbu2.xml
    
     <sersync>
         <localpath watch="/www/file2">
             <!--这里注意,每一台服务器都只写其它服务器IP-->
             <remote ip="192.168.0.2" name="tongbu2"/>
             <!--<remote ip="192.168.0.3" name="tongbu2"/>-->
         </localpath>
         <rsync>
             <commonParams params="-artuz"/>
             <auth start="true" users="root" passwordfile="/etc/rsyncd.pswd"/>
             <userDefinedPort start="false" port="874"/>
             <timeout start="false" time="100"/>
             <ssh start="false"/>
         </rsync>
         <!--60分钟进行一次同步失败文件重新同步-->
         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
         <!--600分钟进行一次全目录同步-->
         <crontab start="true" schedule="600">
             <crontabfilter start="false">
                 <exclude expression="*.php"></exclude>
                 <exclude expression="info/*"></exclude>
             </crontabfilter>
         </crontab>
         <plugin start="false" name="command"/>
     </sersync>
    
     ESC
     :wq
    
  10. 启动sersync(各服务器都执行)

     /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu1.xml
     /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu2.xml
    
  11. 配置开机启动rsync、sersync

     # 开机启动rsync
     systemctl enable rsyncd
     systemctl start rsyncd
    
     # 开机启动sersync
     vim /etc/rc.local
    
     /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu1.xml
     /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/tongbu2.xml
    
     ESC
     :wq
    
     chmod +x /etc/rc.d/rc.local
    
  12. 测试

     # 在任意一台服务器执行
     su www
     cd /www/file1
     echo test > test.txt
     ls
     # 然后在其他每台服务器执行
     cd /www/file1
     ls
     # 如果结果中包含
     test.txt
     # 并且文件内容为'test'
     # 即表示安装成功
    
  13. 关于日志中部分提示的说明

     # 文件同步时会有如下提示
     # connect from UNKNOWN (192.168.0.x)
     # rsync to tongbu1/ from unknown (192.168.0.x)
    
     # 虽然提示连接到未知ip,实际上只是因为没有加
     # 虚拟域名映射,完全不会影响rsync功能的使用。
     # 如果一定要处理的话,可以修改/etc/hosts文件
     # 这样unknown就会变成你设置的虚拟域名了
     vim /etc/hosts
    
     192.168.0.x xxx
    
     ESC
     :wq
    

results matching ""

    No results matching ""