zoukankan      html  css  js  c++  java
  • rsync+sersync实现数据实时同步

    一、为什么要用rsync+sersync架构?

    1、sersync是基于inotify开发的,类似于inotify-tools的工具


    2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录

    二、rsync+inotify-tools与rsync+sersync架构的区别?

    1、rsync+inotify-tools

     a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

     b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低   

    2、rsync+sersync

     a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;

     b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。

    同步过程:

    1.  在同步服务器上开启sersync服务,sersync负责监控配置路径中的文件系统事件变化;

    2.  调用rsync命令把更新的文件同步到目标服务器;

    3.  需要在主服务器配置sersync,在同步目标服务器配置rsync server(注意:是rsync服务)

     同步过程和原理

    1.  用户实时的往sersync服务器上写入更新文件数据;

    2.  此时需要在同步主服务器上配置sersync服务;

    3.  在另一台服务器开启rsync守护进程服务,以同步拉取来自sersync服务器上的数据;

    通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器

    三、安装配置过程

    sersync是需要备份的服务器,备份目录/data,IP:192.168.5.80

    rsync服务器作为备份服务器,存储目录/backup,IP:192.168.5.82

    3.1 在192.168.5.82上对rsync服务进行安装配置

    # yum install rsync -y
    # useradd rsync -s /sbin/nologin -M
    # mkdir /backup
    # chown -R rsync /backup
    # echo "rsync_backup:123456" > /etc/rsync.password
    # chmod 600 /etc/rsync.password
    
    # cat /etc/rsyncd.conf 
    uid=rsync
    gid=rsync
    port=873
    use chroot=no
    max connections= 10
    ignore errors
    pid file=/var/run/rsyncd.pid
    lock file=/var/run/rsync.lock
    log file=/var/log/rsyncd.log
    list = false
    auth users=rsync_backup            # 不是系统用户,不需要提前创建 只是用来同步数据
    secrets file = /etc/rsync.password
    
    [backup]
    path=/backup
    read only=false
    hosts allow = 192.168.5.0/24
    hosts deny = 0.0.0.0/32
    
    # systemctl start rsyncd
    # systemctl enable rsyncd

    3.2 在192.168.5.80上对sersync服务进行安装配置

    # yum install rsync -y
    上传软件包,可通过此地址下载
    # tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
    # mv /usr/local/GNU-Linux-x86 /usr/local/sersync
    # echo '123456' >/etc/rsync.password
    # chmod 600 /etc/rsync.password 
    # vim /usr/local/sersync/confxml.xml 
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
        <host hostip="localhost" port="8008"></host>   # 设置本地IP和端口
        <debug start="false"/>  # 是否开启调试模式,下面所有出现false和true的地方都分别表示关闭和开启的开关
        <fileSystem xfs="false"/>  # 监控的是否是xfs文件系统
        <filter start="false">    # 是否启用监控的筛选功能,筛选的文件将不被监控,默认关闭
        <exclude expression="(.*).svn"></exclude>
        <exclude expression="(.*).gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
        </filter>
        <inotify>    # 设置监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
        <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>    # rsync命令的配置段
        <localpath watch="/data">  # 本地监控同步的目录或文件,同inotify+rsync一样,建议同步目录
            <remote ip="192.168.5.82" name="backup"/>  # 目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>   # 指定rsync选项
            <commonParams params="-artuz"/>  
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>   # 指定rsync密码认证选项
            <userDefinedPort start="false" port="874"/><!-- port=874 -->  # 设置rsync远程服务端口
            <timeout start="false" time="100"/><!-- timeout=100 -->  # 设置超时时间
            <ssh start="false"/>   # 是否使用远程shell模式而非rsync daemon运行rsync命令
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  # 错误重传。sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">  # 是否开启crontab功能,默认关闭
            <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/images"/>
        </localpath>
        </plugin>
    </head>
    
    # 启动服务,启动正常后文件直接同步
    # echo '/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml' >> /etc/rc.local 
    # chmod +x /etc/rc.local 
    # /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    option: -d     run as a daemon
    option: -r     rsync all the local files to the remote servers before the sersync work
    option: -o     config xml name:  /usr/local/sersync/confxml.xml
    daemon thread num: 10
    parse xml config file
    host ip : localhost    host port: 8008
    daemon start,sersync run behind the console 
    use rsync password-file :
    user is    rsync_backup
    passwordfile is     /etc/rsync.password
    config xml parse success
    please set /etc/rsyncd.conf max connections=0 Manually
    sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
    Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
    please according your cpu ,use -n param to adjust the cpu rate
    ------------------------------------------
    rsync the directory recursivly to the remote servers once
    working please wait...
    execute command: cd /data && rsync -artuz -R --delete ./ rsync_backup@192.168.5.82::backup --password-file=/etc/rsync.password >/dev/null 2>&1 
    run the sersync: 
    watch path is: /data

    3.3 本地监控脚本

    # cat /usr/local/sersync/check_sersync.sh 
    #!/bin/sh
    sersync="/usr/local/sersync/sersync2"
    confxml="/usr/local/sersync/confxml.xml"
    status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
    if [ $status -eq 0 ];
    then
    $sersync -dro $confxml &
    else
    exit 0;
    fi
    
    # echo "*/5 * * * * /bin/bash /usr/local/sersync/check_sersync.sh 2>&1 >/dev/null" > /var/spool/cron/root
  • 相关阅读:
    CF505E-Mr. Kitayuta vs. Bamboos【贪心,二分】
    51nod2626-未来常数【树上启发式合并,线段树】
    P5371-[SNOI2019]纸牌【矩阵乘法】
    [HAOI2018]奇怪的背包
    [POI2015]MYJ
    CF1132F Clear the String
    [机房测试]矿石
    react报错:Can‘t perform a React state update on an unmounted component
    react 路由 404页面配置
    9.14JavaWeb之@WebServlet注解
  • 原文地址:https://www.cnblogs.com/cyleon/p/11219602.html
Copyright © 2011-2022 走看看