zoukankan      html  css  js  c++  java
  • linux sersync+rsync 实现跨服务器数据同步

       环境准备

     1、服务器A(主服务器)        IP:192.168.1.144

     2、服务器B(从服务器/备份服务器)  IP:192.168.1.145

     3、rsync默认TCP端口为873

     4、实现服务器A→服务B(A服务文件同步至B服务器中)

    服务器B

    #在服务器B上安装rsynccd /app/local
    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    tar zxf rsync-3.1.1.tar.gz
    cd rsync-3.1.1
    ./configure
    make && make install
    
    
    #设置rsync的配置文件
    vi /etc/rsyncd.conf
    
    #Rsync server
    uid=root
    gid=root
    #最大连接数
    max connections=36000
    #默认为true,修改为no,增加对目录文件软连接的备份 
    use chroot=no
    #超时时间[秒]
    timeout = 600
    #指定rsync的锁文件
    lock file =/var/run/rsync.lock
    #定义日志存放位置
    log file=/var/log/rsyncd.log
    #忽略无关错误
    ignore errors = yes
    #设置rsync服务端文件为读写权限
    read only = no
    #认证的用户名与系统帐户无关在认证文件做配置,如果没有这行则表明是匿名
    auth users = rsync
    #密码认证文件,格式(虚拟用户名:密码)
    secrets file = /etc/rsync.pass
    #这里是认证的模块名,在client端需要指定,可以设置多个模块和路径
    [data]          #模块名
    comment = data      #自定义注释
    path=/app/data      #同步到B服务的文件存放路径
    ##############################
    [file]
    comment = file
    path=/app/file
    ##############################
    [images]
    comment = images
    path=/app/images
    ##############################
    [video]
    comment = video
    path=/app/video

    #创建rsync认证文件 可以设置多个,每行一个用户名:密码,注意中间以“:”分割echo"rsync:rsync"> /etc/rsync.pass #设置文件所有者读取、写入权限 chmod 600 /etc/rsyncd.conf chmod 600 /etc/rsync.pass

    #本地hosts文件写入服务器A的IP和名称
    vi /etc/hosts

    #启动服务器B上的rsync服务#rsync --daemon -v
    rsync --daemon
    
    #监听端口873
    netstat -an | grep 873
    lsof -i tcp:873
    
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    rsync   31445 root    4u  IPv4 443872      0t0  TCP *:rsync (LISTEN)
    rsync   31445 root    5u  IPv6 443873      0t0  TCP *:rsync (LISTEN)
    
    
    #设置rsync为服务启动项(可选)echo"/usr/local/bin/rsync --daemon">> /etc/rc.local
    
    #要 Kill rsync 进程,不要用 kill -HUP {PID} 的方式重启进程,以下3种方式任选#ps -ef|grep rsync|grep -v grep|awk '{print $2}'|xargs kill -9#cat /var/run/rsyncd.pid | xargs kill -9
    pkill rsync
    #再次启动
    /usr/local/bin/rsync --daemon
    
    

    服务器A

    #安装rsync
    cd /app/local
    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    tar zxf rsync-3.1.1.tar.gz
    cd rsync-3.1.1
    ./configure
    make && make install
    
    #安装inotify-tools
    cd /app/local
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    tar zxf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure --prefix=/app/local/inotify 
    make && make install
    
    #安装sersync
    cd /app/local
    wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
    tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
    mv /app/local/GNU-Linux-x86/ /app/local/sersync
    cd /app/local/sersync
    #配置下密码文件,因为这个密码是要访问服务器B需要的密码和上面服务器B的密码必须一致
    echo "rsync" > /app/local/sersync/user.pass
    #修改权限
    chmod 600 /app/local/sersync/user.pass
    #修改confxml.conf(可编写多个配置文件,运行多个进程去管理同步文件)
    vi /app/local/sersync/confxml.xml
    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
     <host hostip="localhost" port="8008"></host>
     <debug start="true"/>
     <fileSystem xfs="false"/>
     <filter start="false">
     <exclude expression="(.*).php"></exclude>
     <exclude expression="^data/*"></exclude>
     </filter>
     <inotify>
     <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>
     <localpath watch="/app/data"> <!-- 这里填写服务器A要同步的文件夹路径-->
     <remote ip="192.168.1.144" name="data"/> <!-- 这里填写服务器B的IP地址和模块名,一定要与B服务器一致-->
     <!--<remote ip="192.168.28.39" name="tongbu"/>-->
     <!--<remote ip="192.168.28.40" name="tongbu"/>-->
     </localpath>
     <rsync>
     <commonParams params="-artuz"/>
     <auth start="true" users="rsync" passwordfile="/app/local/sersync/user.pass"/> <!-- rsync+密码文件 这里填写服务器B的认证信息-->
     <userDefinedPort start="false" port="874"/><!-- port=874 -->
     <timeout start="false" time="100"/><!-- timeout=100 -->
     <ssh start="false"/>
     </rsync>
     <failLog path="/tmp/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="/home/demo">
     <deshost ip="210.36.158.xxx" port="8009"/>
     </localpath>
     </plugin>
     <plugin name="refreshCDN">
     <localpath watch="/data0/htdocs/cdn.markdream.com/site/">
     <cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
     <sendurl base="http://cdn.markdream.com/cms"/>
     <regexurl regex="false" match="cdn.markdream.com/site([/a-zA-Z0-9]*).cdn.markdream.com/images"/>
     </localpath>
     </plugin>
    </head>
    
    #运行sersync
    nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/confxml.xml >/app/local/sersync/rsync.log 2>&1 &
    nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/img.xml >/app/local/sersync/img.log 2>&1 &
    
    -d:启用守护进程模式
    -r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    -n: 指定开启守护线程的数量,默认为10个
    -o:指定配置文件,默认使用confxml.xml文件

    =============================================================================================================

    以下为双同步,2台服务器互相同步文件

    1. 服务器A(主服务器)
    2. 服务器B(从服务器/备份服务器)
    3. rsync默认TCP端口为873

    服务器B

    #在服务器B上安装rsync
    cd /app/local
    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    tar zxf rsync-3.1.1.tar.gz
    cd rsync-3.1.1
    ./configure
    make && make install
    
    
    #设置rsync的配置文件
    vi /etc/rsyncd.conf
    
    #服务器B上的rsyncd.conf文件内容
    uid=root
    gid=root
    #最大连接数
    max connections=36000
    #默认为true,修改为no,增加对目录文件软连接的备份 
    use chroot=no
    #定义日志存放位置
    log file=/var/log/rsyncd.log
    #忽略无关错误
    ignore errors = yes
    #设置rsync服务端文件为读写权限
    read only = no 
    #认证的用户名与系统帐户无关在认证文件做配置,如果没有这行则表明是匿名
    auth users = rsync
    #密码认证文件,格式(虚拟用户名:密码)
    secrets file = /etc/rsync.pass
    #这里是认证的模块名,在client端需要指定,可以设置多个模块和路径
    [rsync]
    #自定义注释
    comment  = rsync
    #同步到B服务器的文件存放的路径
    path=/app/data/site/
    [img]
    comment  = img
    path=/app/data/site/img
    
    #创建rsync认证文件  可以设置多个,每行一个用户名:密码,注意中间以“:”分割
    echo "rsync:rsync" > /etc/rsync.pass
    
    #设置文件所有者读取、写入权限
    chmod 600 /etc/rsyncd.conf  
    chmod 600 /etc/rsync.pass  
    
    #启动服务器B上的rsync服务
    #rsync --daemon -v
    rsync --daemon
    
    #监听端口873
    netstat -an | grep 873
    lsof -i tcp:873
    
    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    rsync   31445 root    4u  IPv4 443872      0t0  TCP *:rsync (LISTEN)
    rsync   31445 root    5u  IPv6 443873      0t0  TCP *:rsync (LISTEN)
    
    
    #设置rsync为服务启动项(可选)
    echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local
    
    #要 Kill rsync 进程,不要用 kill -HUP {PID} 的方式重启进程,以下3种方式任选
    #ps -ef|grep rsync|grep -v grep|awk '{print $2}'|xargs kill -9
    #cat /var/run/rsyncd.pid | xargs kill -9
    pkill rsync
    #再次启动
    /usr/local/bin/rsync --daemon
    
    

    服务器A

    #安装rsync
    cd /app/local
    wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz
    tar zxf rsync-3.1.1.tar.gz
    cd rsync-3.1.1
    ./configure
    make && make install
    
    #安装inotify-tools
    cd /app/local
    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    tar zxf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure --prefix=/app/local/inotify 
    make && make install
    
    #安装sersync
    cd /app/local
    wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
    tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
    mv /app/local/GNU-Linux-x86/ /app/local/sersync
    cd /app/local/sersync
    #配置下密码文件,因为这个密码是要访问服务器B需要的密码和上面服务器B的密码必须一致
    echo "rsync" > /app/local/sersync/user.pass
    #修改权限
    chmod 600 /app/local/sersync/user.pass
    #修改confxml.conf
    vi /app/local/sersync/confxml.xml
    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
     <host hostip="localhost" port="8008"></host>
     <debug start="true"/>
     <fileSystem xfs="false"/>
     <filter start="false">
     <exclude expression="(.*).php"></exclude>
     <exclude expression="^data/*"></exclude>
     </filter>
     <inotify>
     <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>
     <localpath watch="/home/"> <!-- 这里填写服务器A要同步的文件夹路径-->
     <remote ip="8.8.8.8" name="rsync"/> <!-- 这里填写服务器B的IP地址和模块名-->
     <!--<remote ip="192.168.28.39" name="tongbu"/>-->
     <!--<remote ip="192.168.28.40" name="tongbu"/>-->
     </localpath>
     <rsync>
     <commonParams params="-artuz"/>
     <auth start="true" users="rsync" passwordfile="/app/local/sersync/user.pass"/> <!-- rsync+密码文件 这里填写服务器B的认证信息-->
     <userDefinedPort start="false" port="874"/><!-- port=874 -->
     <timeout start="false" time="100"/><!-- timeout=100 -->
     <ssh start="false"/>
     </rsync>
     <failLog path="/tmp/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="/home/demo">
     <deshost ip="210.36.158.xxx" port="8009"/>
     </localpath>
     </plugin>
     <plugin name="refreshCDN">
     <localpath watch="/data0/htdocs/cdn.markdream.com/site/">
     <cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
     <sendurl base="http://cdn.markdream.com/cms"/>
     <regexurl regex="false" match="cdn.markdream.com/site([/a-zA-Z0-9]*).cdn.markdream.com/images"/>
     </localpath>
     </plugin>
    </head>
    
    #运行sersync
    nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/confxml.xml >/app/local/sersync/rsync.log 2>&1 &
    nohup /app/local/sersync/sersync2 -r -d -o /app/local/sersync/img.xml >/app/local/sersync/img.log 2>&1 &
    
    -d:启用守护进程模式
    -r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    -n: 指定开启守护线程的数量,默认为10个
    -o:指定配置文件,默认使用confxml.xml文件
    192.168.1.144
  • 相关阅读:
    Java JVM启动参数
    使用Navicat连接MySQL8.0版本报1251错误
    安装MySQL和出现的问题解决
    跨域问题:解决跨域的三种方案
    Java8 新特性lambda表达式(一)初始
    搭建docker私有仓库
    crontab定时任务
    CentOS610 php环境安装
    Docker常用命令
    PHP调用python脚本执行时报错
  • 原文地址:https://www.cnblogs.com/Nanaya/p/13038900.html
Copyright © 2011-2022 走看看