zoukankan      html  css  js  c++  java
  • Rsync+sersync(inotify)实现数据实时双向同步

    Rsync+Sersync数据实时同步(双向)

    Author:Rich七哥

    服务介绍

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

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

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

    • 3因为服务异常导致的同步失败有记录,便于恢复,确保高可用!

    二、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推送到目标服务器

    节点声明

    #双向备份
    192.168.0.50(数据源节点、备份节点)
    192.168.0.51(数据源节点、备份节点)
    

    编译环境配置

    本教程是基于CentOs 7.5系统配置的,为了适合不同环境配置的节点安装,本教程将采用编译安装的方式

    [root@cosmo-0-50 bin]# cat /etc/redhat-release 
    CentOS Linux release 7.5.1804 (Core) 
    
    # 环境检查
    
    #查看gcc【编译器】版本
    gcc -v 
    #查看perl【解释器】版本
    perl -v
    
    # 提示无该命令则需要安装,如果有版本信息则直接执行下步操作
    #安装编译器gcc:
     
     #第一步:上传gcc_rpm.tar.gz到/usr/src 下
     #第二步:解压缩到/usr/local
      cd /usr/src
      tar -zxvf gcc_rpm.tar.gz -C /usr/local
      
     #第三步:rpm安装
      cd /usr/local/gcc_rpm
      rpm -Uvh  *.rpm  --nodeps  --force  
      
     #第四步:版本查看,确认是否安装成功
      gcc -v 和 g++ -v  
     
     
    #安装解释器perl[http://search.cpan.org/CPAN/authors/id/S/SH/SHAY]:
     
     #第一步:上传perl-5.26.1.tar.gz到/usr/src 下
     #第二步:解压缩到 /usr/local/perl
      cd /usr/src
      tar -zxvf perl-5.26.1.tar.gz  -C /usr/local
      
     #第三步:进入文件目录,设置config
      cd /opt/perl-5.26.1/
      ./Configure -des -Dprefix=/usr/local/perl
      
     #第四步:编译并检测
      make && make test
     【出现All tests successful:编译及检测成功】
     
     #第五步:安装
      make install
      
     #第六步:验证
      perl -v
    

    安装Rsync

    # 将rsync.tar包上传至安装节点/opt/cosmo/com/rsync目录下
    [root@cosmo-0-50 rsync]# ls
    rsync-3.0.9.tar.gz  
    
    #然后解压
    tar -zxvf rsync-3.0.9.tar.gz
    
    # 编译
    cd rsync-3.0.9
    ./configure --prefix=/opt/cosmo/rsync
    make  && make install && echo $?
    
    # 验证
    echo $?的值为0代表执行成功
    
    

    编辑Rsync配置文件

    192.168.0.50

    [root@cosmo-0-50 bin]# cat /opt/cosmo/com/rsync/bin/rsyncd.conf
    uid = root                          
    gid = root                          
    address =192.168.0.50               
    port =873                           
    hosts allow =192.168.0.0/24         
    use chroot = yes                    
    max connections =10                  
    pid file =/var/run/rsyncd.pid       
    lock file =/var/run/rsync.lock      
    log file =/var/log/rsyncd.log       
    motd file =/etc/rsyncd.motd
    
     
    [wwwroot]                           
    path =/opt/cosmo/test/                    
    comment = used for web-data root    
    read only = false                   
    list = yes                          
    auth users = rsyncuser              
    secrets file =/etc/rsync.passwd
    

    配置文件解析

    [root@cosmo-0-50 bin]# cat /opt/cosmo/com/rsync/bin/rsyncd.conf
    uid = root                         #运行进程的身份                     
    gid = root                         #运行进程的组                
    address =192.168.0.50              #监听IP          
    port =873                          #监听端口              
    hosts allow =192.168.0.0/24        #允许同步客户端的IP地址,可以是网段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0  
    use chroot = yes                   #是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes            
    max connections =10                 #最大连接数           
    pid file =/var/run/rsyncd.pid       #进程PID,自动生成  
    lock file =/var/run/rsync.lock      #指max connectios参数的锁文件
    log file =/var/log/rsyncd.log       #日志文件位置
    
     
    [wwwroot]                           #同步模块名称           
    path =/opt/cosmo/test/              #同步文件路径     
    comment = used for web-data root    #模块描述
    read only = false                   #设置服务端文件读写权限   
    list = yes                          #是否允许查看模块信息
    auth users = rsyncuser              #备份的用户,和系统用户无关
    secrets file =/etc/rsync.passwd     #存放用户的密码文件,格式是  用户名:密码
    

    配置密码文件

    [root@cosmo-0-50 bin]# cat /opt/cosmo/com/rsync/bin/rsync.passwd
    rsyncuser:password123
    #用户名:密码
    
    [root@cosmo-0-50 bin]#chmod 600 /etc/rsync.passwd  
    #目录权限必须是700或者600,否则的话身份验证会失效,设置rsync user的时候
    
    #创建sersync密码文件
    [root@cosmo-0-50 bin]# cat /opt/cosmo/com/rsync/bin/rsync2.passwd 
    password123
    

    启动rsync验证

    #启动命令
    [root@cosmo-0-50 bin]#/opt/cosmo/rsync/bin/rsync --daemon --config=/opt/cosmo/rsync/bin/rsyncd.conf
    
    # 查看进程
    ps -e|grep rsync
    
    #查看端口(此命令没有的话需安装net-tools)
    netstat -antup|grep 873
    
    # 同步测试
    rsync -avz  /opt/cosmo/test/ root@192.168.0.51:/opt/cosmo/test/
    
    # 设置开机自启动
      echo "/opt/cosmo/rsync/bin/rsync --daemon --config=/opt/cosmo/rsync/bin/rsyncd" >> /etc/rc.local
      
    
    • 51节点rsync安装和50步骤一致

    安装sersync服务

    # 将sersync的软件包上传至安装节点/opt/cosmo/com下
    # 解压
    tar xvf sersync2.5.4_51bit_binary_stable_final.tar.gz
    
    # 修改包名
    [root@cosmo-0-51 sersyncdir]# mv GNU-Linux-x86 sersyncdir
    [root@cosmo-0-51 sersyncdir]# cd sersyncdir
    [root@cosmo-0-51 sersyncdir]# ls
    sersync
    
    [root@cosmo-0-51 sersyncdir]# cd sersync/
    [root@cosmo-0-51 sersync]# ls
    confxml.xml  sersync2
    

    配置sersync服务

    # [root@cosmo-0-50 sersync]# cat confxml.xml 
    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
        <host hostip="localhost" port="8008"></host>
        <debug start="false"/>
        <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="false"/>
            <closeWrite start="true"/>
            <moveFrom start="true"/>
            <moveTo start="true"/>
            <attrib start="false"/>
            <modify start="false"/>
        </inotify>
    
        <sersync>
            <localpath watch="/opt/cosmo/test">
                <remote ip="192.168.0.51" name="wwwroot"/>
                <!--<remote ip="192.168.8.39" name="tongbu"/>-->
                <!--<remote ip="192.168.8.40" name="tongbu"/>-->
            </localpath>
            <rsync>
                <commonParams params="-artuz"/>
                <auth start="true" users="rsyncuser" passwordfile="/opt/cosmo/com/rsync/bin/rsync2.passwd"/>
                <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="/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>
    

    1572241634428

    sersync配置文件解析

    # 修改24--28行
    <sersync>
            <localpath watch="/opt/cosmo/test">    #本地同步目录
                <remote ip="192.168.0.51" name="wwwroot"/>   #rsync模块名称
    
    # 修改31--34行,认证部分【rsync密码认证】
    <rsync>
                <commonParams params="-artuz"/>
                <auth start="true" users="rsyncuser"   #修改用户名 passwordfile="/opt/cosmo/com/rsync/bin/rsync2.passwd"/>   # 修改密码文件路径
                <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-->  # **错误日志路径,用于记录和恢复同步失败的文件**
    

    启动sersync服务

    # 启动sersync服务
    [root@cosmo-0-50 sersync]# /opt/cosmo/com/sersyncdir/sersync/sersync2 -r -d -o /opt/cosmo/com/sersyncdir/sersync/confxml.xml 
    
    • 输出内容如下:

    1572242155159

    51节点的配置方法和50一致

    测试同步

    #在50节点的/opt/cosmo/test/进行文件修改或删除,在51节点进行观察是否同步成功
    watch ls -l
    

  • 相关阅读:
    PlayerPrefs存储Vector3等结构数据
    Kafka集群部署及測试
    火云开发课堂
    Thinking in Java:容器深入研究
    求int型数据在内存中存储时1的个数
    JAVA 几种多线程的简单实例 Thread Runnable
    Android利用Intent与其它应用交互
    kernel
    Azure DocumentDB 正式发布
    在公有云平台体验开源方案的自动部署
  • 原文地址:https://www.cnblogs.com/fusheng11711/p/11751930.html
Copyright © 2011-2022 走看看