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

  1. 安装前的准备(各同步服务器都配置)

     # 因为这会降低服务器异常报警的级别,所以记得添加报警机制
     # SELINUX=disabled(完全不验证)
     vim /etc/sysconfig/selinux
    
     SELINUX=permissive
    
     ESC
     :wq
    
     getenforce
     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.x 192.168.0.x
         hosts deny = *
    
     [tongbu2]
         path = /www/file2
         comment = xoyo video files
         ignore errors = yes
         read only = no
         hosts allow = 192.168.0.x 192.168.0.x
         hosts deny = *
    
     ESC
     :wq
    
  4. 配置密码文件

     vim /etc/rsyncd.pswd
    
     root:**************
    
     ESC
     :wq
    
     chmod 600 /etc/rsyncd.pswd
    
     vim /etc/sersyncd.pswd
    
     **************
    
     ESC
     :wq
    
  5. 创建同步目录(各服务器都创建)

     useradd www
     mkdir /www
     mkdir /www/file1
     mkdir /www/file2
     chown -R www:www /www/file1
     chown -R www:www /www/file2
    
  6. 启动rsync(各服务器都启动)

     rsync --daemon
    
  7. 查看占用的端口号

     netstat –apn|grep rsync
    
  8. 开启端口

     yum install firewalld -y
     systemctl enable firewalld
     systemctl start firewalld
     firewall-cmd --zone=public --permanent --add-port=873/tcp
     firewall-cmd --reload
    
  9. 创建单元文件(如果是用yum装的的rsync,则跳过该步骤)

     # 进入单元文件目录,创建rsync单元文件
     cd /etc/systemd/system
     vim rsyncd.service
    
     [Unit]
     Description=Start rsync on boot.
     After=default.target network.target
    
     [Service]
     User=root
     Group=root
     Type=forking
     ExecStart=/usr/bin/rsync --daemon
     PrivateTmp=false
     Restart=always
    
     [Install]
     WantedBy=multi-user.target
    
     ESC
     :wq
    
     # 修改文件权限为只有root用户可以编辑该文件
     chown -R root:root /etc/systemd/system/rsyncd.service
     chmod -R 644 /etc/systemd/system/rsyncd.service
     systemctl daemon-reload
    
  10. 启动并设置开机启动

     systemctl enable rsyncd
     systemctl start rsyncd
    
  11. 安装sersync(各服务器都装)

     # 源码
     https://code.google.com/archive/p/sersync/
     # 百度云
     https://pan.baidu.com/s/1qrwTM40aJ8ckmT84PpiNjg
     # 64位最新
     https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gz
     # 32位最新
     https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5_32bit_binary_stable_final.tar.gz
     # 下载安装
     wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sertongbu2.5.4_64bit_binary_stable_final.tar.gz
     tar -zxvf sertongbu2.5.4_64bit_binary_stable_final.tar.gz
     mv GNU-Linux-x86/ /usr/local/sersync
    
  12. 配置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="tongbu1"/>
             <remote ip="192.168.0.3" name="tongbu1"/>
             <remote ip="192.168.0.4" name="tongbu1"/>
         </localpath>
         <rsync>
             <commonParams params="-artuz"/>
             <!--如果设置了密码,则密码文件的路径、内容必须正确 start="true"-->
             <auth start="true" users="root" passwordfile="/etc/sersyncd.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>
         <!--因为这里start="false" 所以下面plugin的配置无需关心-->
         <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"/>
             <remote ip="192.168.0.4" name="tongbu2"/>
         </localpath>
         <rsync>
             <commonParams params="-artuz"/>
             <!--如果设置了密码,则密码文件的路径、内容必须正确 start="true"-->
             <auth start="true" users="root" passwordfile="/etc/sersyncd.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>
         <!--因为这里start="false" 所以下面plugin的配置无需关心-->
         <plugin start="false" name="command"/>
     </sersync>
    
     ESC
     :wq
    
  13. 启动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
    
  14. 测试

     # 在任意一台服务器执行
     su www
     cd /www/file1
     echo test > test.txt
     ls
     # 然后在其他每台服务器执行
     cd /www/file1
     ls
     # 如果结果中包含
     test.txt
     # 并且文件内容为'test'
     # 即表示安装成功
    
  15. 配置开机启动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
    
  16. 关于日志中部分提示的说明

     # 文件同步时会有如下提示
     # connect from UNKNOWN (192.168.0.3)
     # rsync to tongbu1/ from unknown (192.168.0.3)
     # connect from UNKNOWN (192.168.0.4)
     # rsync to tongbu1/ from unknown (192.168.0.4)
    
     # 虽然提示连接到未知ip,实际上只是因为没有加
     # 虚拟域名映射,完全不会影响rsync功能的使用。
     # 如果一定要处理的话,可以修改/etc/hosts文件
     # 这样unknown就会变成你设置的虚拟域名了
     vim /etc/hosts
    
     192.168.0.2 file2
     192.168.0.3 file3
     192.168.0.4 file4
    
     ESC
     :wq
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-01-28 15:00:07

results matching ""

    No results matching ""