• Rsync+sersync 实现数据实时同步


    Rsync+sersync 实现数据实时同步
    原创a1779078902 最后发布于2019-07-19 17:13:19 阅读数 256 收藏
    展开
     一、为什么要用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在遍历查找对比文件时,速度很快),因此效率很高。

    三 项目实践
    3.1 准备两台机器

    rsync服务器(备份端,目标机器) 192.168.139.136  [root@nfs-server /]# uname -r
    3.10.0-862.el7.x86_64
    sersync服务器(数据源,源机器) 192.168.139.135 [root@backup /]# uname -r
    3.10.0-862.el7.x86_64
    3.2 在两台机器上都安装rsync

     [root@rsync-server /]# yum install -y rsync

     [root@backup /]# yum install -y rsync
    四 .配置rsync服务器上面的rsync
    4.1 编辑配置文件rsyncd.conf

    [root@backup /] vi /etc/rsyncd.conf
    uid = rsync #设置rsync运行权限为rsync
    gid = rsync #设置rsync运行权限为rsync
    use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
    max connections = 200 #最大连接数
    timeout = 300 #设置超时时间
    pid file = /var/run/rsyncd.pid #pid文件的存放位置
    lock file = /var/run/rsyncd.lock #支持max connections参数的锁文件
    log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    read only = false #设置rsync服务端文件为读写权限
    list = false #不显示rsync服务端资源列表
    hosts allow = 192.168.139.0/24 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    hosts deny = 0.0.0.0/32 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    auth users = rsyncbak #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
    secrets file = /etc/rsync.password #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
    [backup] #自定义模块名称
    path = /backup #rsync服务端数据目录路径
    4.2 建立相关测试数据目录和用户

    [root@rsync-server /]#useradd -s /no/login -M rsync
    [root@rsync-server /]#id rsync
    uid=1001(rsync) gid=1001(rsync) groups=1001(rsync)
    [root@rsync-server /]#mkdir /backup
    [root@rsync-server /]#chown -R rsync.rsync /backup
    4.3 创建远程验证用户

    [root@rsync-server /]#vi /etc/rsyncd.password
    rsyncbak:1
     4.4 启动服务

      [root@rsync-server /]#systemctl start rsyncd
      [root@rsync-server /]#systemctl enable rsyncd
      检查
    [root@nfs-server /]# ps aux |grep rsync
    root 3813 0.0 0.0 114740 1172 ? Ss 09:11 0:00 /usr/bin/rsync --daemon --no-detach
    root 4033 0.0 0.0 112704 960 pts/0 S+ 15:57 0:00 grep --color=auto rsync
    五. 搭建sersync  
    5.1 在sersync服务器上安装sersync

     [root@backup /]cd /usr/local/src
     [root@backup /]wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
     [root@backup /]tar xzvf sersync2.5.4_64bit_binary_stable_final.tar.gz
     [root@backup /usr/local/src]cd GNU-Linux-x86/
     [root@backup /]cp confxml.xml{,.ori}
    5.2 配置sersync

    [root@backup GNU-Linux-x86]# 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 事件监听#
    <inotify>
    <delete start="true"/>
    <createFolder start="true"/>
    <createFile start="true"/>
    <closeWrite start="true"/>
    <moveFrom start="true"/>
    <moveTo start="true"/>
    <attrib start="false"/>
    <modify start="false"/>
    </inotify>

    <sersync>
    <localpath watch="/data"> #本地监控目录#
    <remote ip="192.168.139.136" name="backup"/> #远程服务器ip地址和模块#
    <!--<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="rsyncbak" passwordfile="/etc/rsync.password"/> #rsync 远程连接验证的用户名和密码#
    <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>
    5.3 创建本地目录用来测试数据并启动服务

     [root@backup /]mkdir /data
    启动服务
     [root@backup /]/usr/local/src/GNU-Linux-x86/sersync2 -d
    检查
     [root@backup /]ps aux |grep sersync2
    root 2562 0.0 0.0 125108 800 ? Ssl 14:30 0:00 /usr/local/src/GNU-Linux-x86/sersync2 -d
    root 2731 0.0 0.0 125108 836 ? Ssl 14:45 0:00 /usr/local/src/GNU-Linux-x86/sersync2 -d -o /usr/local/src/GNU-Linux-x86/confxml.xml.bbs
    root 2959 0.0 0.0 112704 960 pts/0 R+ 21:34 0:00 grep --color=auto sersync2
    六.测试rsync是否搭建成功
    6.1 在sersync服务器上操作:

    确保两台服务器上测试目录一样
    [root@backup ~]# ll /data
    total 0

    [root@rsync-server /]# ll /backup
    total 0
    6.2 测试backup服务器与rsync服务器通信

    [root@backup ~]# touch /data/2.txt
    [root@backup ~]# rsync -avz /data/ rsyncbak@192.168.139.136::backup
    Password:
    sending incremental file list
    1.txt

    sent 130 bytes received 43 bytes 69.20 bytes/sec
    total size is 0 speedup is 0.00

    #在rsync服务器上检查,可以看到我们的2.txt传送过来了#
    [root@rsync-server /]# ll /backup
    total 0
    -rw-r--r-- 1 rsync rsync 0 Jul 16 22:53 2.txt
    七.测试sersync是否搭建成功
    7.1 在backup服务器上创建 删除 或者修改文件

    [root@backup ~]# touch /data/3.txt
    [root@backup ~]# ll /data
    total 0
    -rw-r--r-- 1 root root 0 Jul 16 21:38 1.txt
    -rw-r--r-- 1 root root 0 Jul 16 23:07 2.txt
    -rw-r--r-- 1 root root 0 Jul 16 23:07 3.txt
    7.2在rsync服务器上查看是否成功

    //可以看到我们的3.txt已经创建成功
    [root@nfs-server /]# ll /backup
    total 0
    -rw-r--r-- 1 rsync rsync 0 Jul 16 23:07 2.txt
    -rw-r--r-- 1 rsync rsync 0 Jul 16 23:07 3.txt
     

    1.rsync 排错 
     1.1错误:
     [root@backup ~]# rsync -avz /data/ rsyncbak@192.168.139.136::backup
      Password: 
      sending incremental file list
       1.txt
       rsync: chgrp ".1.txt.rnUDvB" (in backup) failed: Operation not permitted (1)

        sent 108 bytes  received 124 bytes  154.67 bytes/sec
         total size is 0  speedup is 0.00
        rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]  
     1.2 解决方法:如果推送的时候出现以下错误,可能是配置文件中需要加入
               fake super = yes

     
    ————————————————
    版权声明:本文为CSDN博主「a1779078902」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/a1779078902/java/article/details/96480004

  • 相关阅读:
    JS控制的几种页面跳转方式和传值
    文件管理
    文件:文件和文件夹
    上传文件
    购物车的例子
    使用ajax登录格式
    ajax 另外两种返回类型(json xml)
    省级三级联动
    thinkPHP--SQL连贯操作
    thinkPHP-空操作
  • 原文地址:https://www.cnblogs.com/mouseleo/p/12587009.html
Copyright © 2020-2023  润新知