最新消息:

CentOS7 下使用 rsync+sersync 配置文件自动同步

Linux google 629浏览 0评论

一、为什么要用Rsync+Sersync架构

Sersync是基于Inotify开发的,类似于Inotify-tools的工具
Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。

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

2.1、Rsync+Inotify-tools
Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。
2.2、Rsync+sersync
sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
2.3、小结
当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

三、配置机器说明
操作系统:CentOS 7.5
源服务器:A
目标服务器:B
目的:把源服务器上/data/test目录实时同步到目标服务器的/data/test下

四、目标服务器B配置操作

4.1、安装rsync服务端
4.1.1、关闭SELINUX

vim /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce 0 #立即生效

4.1.2、关闭防火墙或者开启防火强tcp873端口(rsync默认端口)

#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
#开启防火墙873端口
iptables -A INPUT -p tcp --dport 873 -j ACCEPT

4.1.3、安装rsync服务端

 yum -y install rsync
 systemctl start rsyncd.service #启动rsync服务
 systemctl enable rsyncd.service #将rsync服务 加入开机自启

4.1.4、配置文件调整

#修改/etc/rsyncd.conf配置文件
#Rsync server
##设置rsync运行权限为root
uid = root
##设置rsync运行权限为root
gid = root
#默认端口
port = 873
# 安全相关,增加对目录文件软连接的备份
use chroot = no
# 并发连接数
max connections = 2000
# 超时时间(秒)
timeout = 600
# 指定rsync的pid目录
pid file = /var/run/rsyncd.pid
# 指定rsync的锁文件【重要】
lock file = /var/run/rsync.lock
# 指定rsync的日志目录 
log file = /var/log/rsyncd.log
# 忽略一些I/O错误
ignore errors
# 设置rsync服务端文件为读写权限
read only = false
# 不显示rsync服务端资源列表
list = false
#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts allow = A的ip
# 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = 0.0.0.0/32 
# 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
auth users = rsync
#用户认证配置文件,里面保存用户名称和密码
secrets file = /etc/rsync.passwd

# 自定义模块名称,可以向下添加多个模块,就是同步多个
[rsync_data]
comment = data_rsync
# 将同步过来的文件分别放入对应的path指定的目录
path = /data/test
#贴一个没有注释的自己平时复制用
vi /etc/rsyncd.conf

log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.secret
motd file = /etc/rsyncd.motd
[rsync_data]
path = /data/test
comment = test
uid = root
gid = root
incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 192.168.1.103
hosts deny = *

4.1.5、创建用户认证文件

vim /etc/rsync.pass #配置文件,添加以下内容
rsync:rsync-!@#123  #格式,用户名:密码,可以设置多个,每行一个用户名:密码
设置文件权限
chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限 
chmod 600 /etc/rsync.pass   #设置文件所有者读取、写入权限
启动rsync
rsync --daemon #rsync deamon模式启动
killall rsync   #停止,杀掉进程
cd /usr/local/src
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
Or
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
cd /usr/local/sersync

设置如下代码:(如果需要同步多目录,就再建一个如confxmldata.xml,port端口8008不能冲突(测试好像不改也没关系),改成其他端口。localpath watch 改下,failLog path这个脚本文件也改下,每个xml文件中只保留自己的localpath watch )

<?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="/home/xyz">
            <remote ip="192.168.1.189" name="test"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="root" passwordfile="/etc/rsyncd.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>
参数说明:
如果A服务器新建、删除目录都会触发同步到B服务器。但是A服务器删除文件,不会触发到B服务器。
localpath watch=”/home/xyz“
name=”test”: #目标服务器rsync同步目录模块名称
users=”root”: #目标服务器rsync同步用户名
passwordfile=”/etc/rsyncd.passwd”: #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip=”192.168.1.189”: #对端服务器ip
failLog path=”/tmp/rsync_fail_log.sh” #脚本运行失败日志记录
start=”true” #设置为true,每隔600分钟执行一次全盘同步
delete start #只做增量备份,可以吧这个设置为flase。但是如果两边要一样就设置为true

rsync命令格式:
rsync -参数 文件 目标位置
rsync -参数 文件 user@host:目录
rsync -参数 user@host:目录/文件 目标位置
rsync -参数 文件 user@host::目标位置
rsync -参数 user@host::目录/文件 目标位置
:和::的区别
可以理解为,一个“:”默认是走ssh协议,而两个“::”则是走rsync自己的传输协议。

-a 递归方式传输文件,并保持所有文件属性
-c --checksum   对文件大小进行校验
-r 子目录递归传输
-v 显示详细过程
-l 保留原有文件的软连接
-L 同步软链接时会把源文件一起同步
-p 保持文件的权限属性
-o 保持文件的属主关系
-g 保持文件的属组关系
-D 保持设备文件信息
-t 保持文件的时间属性
-P 更详细的显示同步过程,比-v详细
-u 如果目标端中的文件比源端的更新,则不同步
-z 传输时压缩

转载请注明:LinuxHub | DevOps,SRE,运维架构师,运维开发,Python,Golang程序员,开源爱好者 » CentOS7 下使用 rsync+sersync 配置文件自动同步

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址