rsync+sersync文件实时同步

rsync+sersync文件实时同步

参考:
https://www.linuxidc.com/Linux/2017-10/147900.htm
https://yq.aliyun.com/articles/601394
https://blog.51cto.com/13673885/2103325?cid=704552

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

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

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

二、Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?

1、Rsync+Inotify-tools

(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

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

2、Rsync+sersync

(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

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

小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

环境
10.22.60.33        master        centos 7.5.1804        源服务器
10.22.60.34        slave        centos 7.5.1804        目标服务器


#####################   目标服务器操作       ########################
一、在目标服务器上安装Rsync服务器(10.22.60.34)
1、关闭 selinux 

$ vi /etc/selinux/config
  SELINUX=disabled   # 修改为:

$ setenforce 0     # 立即生效


2、关闭防火墙

$ systemctl stop firewalld.service


    
3、安装rsync服务端软件

$ yum install rsync xinetd     # 安装
$ vi /etc/rc.d/rc.local        # #设置开机启动
  /usr/bin/rsync --daemon --config=/etc/rsyncd.conf        # 添加开机启动
    
$ systemctl start xinetd     #启动xinetd
    

4、创建rsyncd.conf配置文件

    $ vi /etc/rsyncd.conf
        uid = root
        gid = root
        use chroot = yes
        max connections = 0
        log file = /var/log/rsyncd.log
        pid file = /var/run/rsyncd.pid 
        lock file = /var/run/rsync.lock 
        secrets file = /etc/rsync.pass  
        motd file = /etc/rsyncd.Motd

        [data] 
            path = /data/NFS/data
            comment = A directory in which data is stored
            ignore errors = yes
            read only = no
            hosts allow = 10.22.60.0/24

        [logs]
            path = /data/NFS/logs
            comment = The directory where the logs are stored
            ignore errors = yes
            read only = no
            hosts allow = 10.22.60.0/24

        [etc]
            path = /data/NFS/etc
            comment = The directory where the configuration is stored
            ignore errors = yes
            read only = no
            hosts allow = 10.22.60.0/24


5、创建用户认证文件

    $ vi /etc/rsync.pass        # 配置文件,添加以下内容,添加允许传输用户和密码
    sunline:sunline                # 格式,用户名:密码,可以设置多个,每行一个用户名:密码


6、设置文件权限

    $ chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限
    $ chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限


7、启动rsync和xinetd

    $ /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    $ systemctl start xinetd 


#####################   源服务器操作       ########################
1、关闭 selinux 

    $ vi /etc/selinux/config
    SELINUX=disabled   # 修改为:

    $ setenforce 0     # 立即生效


2、关闭防火墙

    $ systemctl stop firewalld.service


    3、安装Rsync客户端端软件

    $ yum install rsync xinetd
    $ vi /etc/rc.local            ## 配置开机启动
        /usr/bin/rsync --daemon
    $ vi /etc/reyncd.conf
        log file = /var/log/rsyncd.log
        pid file = /var/run/rsyncd.pid
        lock file = /var/run/rsync.lock
        motd file = /etc/rsyncd.Motd

        [Sync]
        comment = Sync
        uid = root
        gid = root
        port= 873

        $ chmod +x /etc/rc.d/rc.local  #否则重启不执行

        $ systemctl start xinetd  #启动(CentOS中是以xinetd来管理rsync服务的)


        
        
4、创建认证密码文件

    $ vi /etc/passwd.txt              #编辑文件,添加以下内容,该密码应与目标服务器中的/etc/rsync.pass中的密码一致        
        sunline
 
    $ chmod 600 /etc/passwd.txt     #设置文件权限,只设置文件所有者具有读取、写入权限即可    


        

        
        
5、测试数据同步
    测试源服务器10.22.60.33到目标服务器10.22.60.34,之间的数据同步

    $ mkdir -p /data/NFS/data/sync_test    
    $ rsync -avH --port=873 --progress --delete  /data/NFS/data  root@10.22.60.34::data--password-file=/etc/passwd.txt            
        sending incremental file list
        data/
        data/sync_test/

        sent 91 bytes  received 20 bytes  222.00 bytes/sec
        total size is 0  speedup is 0.00

    运行完成后,在目标服务器10.22.60.34上查看,在/data/NFS/data目录下有sync_test文件夹,说明数据同步成功
    传输到目标服务器的目录和文件均放到/etc/rsyncd.conf配置的服务器目录路径,如果源目录改变了,
    那么传输时两个目录将进行目录匹配,会有增删动作,因此需要注意。    
    

    
二、安装sersync工具,实时触发rsync进行同步

1、查看服务器内核是否支持inotify

    $ ll /proc/sys/fs/inotify    # 列出文件目录,出现下面的内容,说明服务器内核支持inotify
        total 0
        -rw-r--r-- 1 root root 0 Mar 20 13:21 max_queued_events
        -rw-r--r-- 1 root root 0 Mar 20 11:14 max_user_instances
        -rw-r--r-- 1 root root 0 Mar 20 13:21 max_user_watches


            
    备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:#uname -a查看内核
    CentOS 7.0内核为3.10.0,默认已经支持inotify    

2、修改inotify默认参数(inotify默认内核参数值太小)    修改参数:

    $ sysctl -wfs.inotify.max_queued_events="99999999"
    $ sysctl -w fs.inotify.max_user_watches="99999999"
    $ sysctl -wfs.inotify.max_user_instances="65535"

    $ vi /etc/sysctl.conf #添加以下代码
        fs.inotify.max_queued_events=99999999
        fs.inotify.max_user_watches=99999999
        fs.inotify.max_user_instances=65535


        
        
3、安装sersync
    sersync下载地址:https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz        
    (可以使用迅雷下载,直接下载会有墙)
    

    $ tar zxvfsersync2.5.4_64bit_binary_stable_final.tar.gz      #解压
    $ mv GNU-Linux-x86  /usr/local/sersync                      #移动目录到/usr/local/sersync       

 
        
        
4、创建rsync

    $ cd  /usr/local/sersync #进入sersync安装目录
    $ cp confxml.xml  confxml.xml-bak          #备份原文件
    $ cp confxml.xml  data_configxml.xml    # 复制用于同步data目录的文件
    $ cp confxml.xml  etc_configxml.xml        # 复制用于同步etc目录的配置文件
    $ cp confxml.xml  logs_configxml.xml        # 复制用于同步logs的配置文件


    
    
5、修改配置文件

                                ------  data 配置  ------
                                
    $ vi data_configxml.xml
       -----  24行   -----
 24         <localpath watch="/data/NFS/data">                    # 本地地址同步路径
 25             <remote ip="10.22.60.34" name="data"/>            # 目标服务器地址信息
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-artuz"/>
 31            <!-- <auth start="true" users="data" passwordfile="/etc/passwd.txt"/> -->
 32             <auth start="true" users="root" passwordfile="/etc/passwd.txt"/>        ## 启用身份验证"true",密码文件路径"/etc/passwd.txt"
 33             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 34             <timeout start="false" time="100"/><!-- timeout=100 -->
 35             <ssh start="false"/>
 36         </rsync>


        

                                ------  logs 配置  ------
                                
    $ vi logs_configxml.xml
    <localpath watch="/data/NFS/logs">
        <remote ip="10.22.60.34" name="logs"/>
        <!--<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="data" passwordfile="/etc/passwd.txt"/> -->
        <auth start="true" users="root" passwordfile="/etc/passwd.txt"/>
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="false" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>
    </rsync>

        
        

                                ------  etc 配置  ------    

    $ vi etc_configxml.xml
    <localpath watch="/data/NFS/etc">
        <remote ip="10.22.60.34" name="etc"/>
        <!--<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="data" passwordfile="/etc/passwd.txt"/> -->
        <auth start="true" users="root" passwordfile="/etc/passwd.txt"/>
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="false" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>
    </rsync>


6、启动服务

    $ /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/data_configxml.xml
    $ /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/logs_configxml.xml
    $ /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/etc_configxml.xml

    在源服务器上/data/NFS/data、/data/NFS/logs、/data/NFS/etc目录下创建文件或目录
    在目标服务器上查看,是否同步过去


7、设置sersync监控开机自动执行

    $ vi /etc/rc.d/rc.local  #编辑,在最后添加    
        /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/data_configxml.xml
        /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/logs_configxml.xml
        /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/etc_configxml.xml


    

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值