Rsync+sersync实现文件同步

一、为什么要用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.6-1810

源服务器: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方式安装
yum install rsync #安装 rsync --daemon #启动rsync
离线安装,https://rsync.samba.org/ftp/rsync/src/  这边以3.1.3版本为例
wget https://rsync.samba.org/ftp/rsync/src/rsync-3.1.3.tar.gz #下载rsync安装包 
tar xzvf rsync-3.1.3.tar.gz#解压 
cd rsync-3.1.3 
./configure  
make && make install  #编译安装

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

4.1.5、创建用户认证文件

vim /etc/rsync.pass #配置文件,添加以下内容
rsync:rsync-!@#123  #格式,用户名:密码,可以设置多个,每行一个用户名:密码

4.1.6、设置文件权限

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

4.1.7、启动rsync

rsync --daemon #rsync deamon模式启动
killall rsync   #停止,杀掉进程
由于目标服务器重启以后rsync --daemon命令就失效了,所以需要进行开机自启,我选择的方式是直接加到定时任务中,rsync路径不一定在我下面的目录下,也有可能在/usr/bin下,也可直接使用解压后的rsync-3.1.3目录下的rsync 
crontab -e #最后一行添加
*/1 * * * * /usr/local/bin/rsync --daemon

五、在源服务器A配置操作

5.1、安装rsync客户端

5.1.1、关闭SELINUX

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

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

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

5.1.3、安装rsync客户端

yum方式安装
yum install rsync #安装 rsync --daemon #启动rsync
离线安装,https://rsync.samba.org/ftp/rsync/src/  这边以3.1.3版本为例
wget https://rsync.samba.org/ftp/rsync/src/rsync-3.1.3.tar.gz #下载rsync安装包 
tar xzvf rsync-3.1.3.tar.gz#解压 
cd rsync-3.1.3 
./configure  
make && make install  #编译安装

5.1.4、创建认证密码文件

vim /etc/rsyncd/rsync.passwd  #编辑文件,添加以下内容
rsync-!@#123 #密码
:wq! #保存退出
chmod 600 /etc/rsyncd/rsync.passwd  #设置文件权限,只设置文件所有者具有读取、写入权限即可

5.1.5、测试源机器到目标机器之间的数据同步

mkdir /data/test #在源服务器上创建测试文件夹,然后在源服务器运行下面2行命令
rsync -avH --port=873 --progress --delete  /data/test rsync@B::rsync_data --password-file=/etc/rsyncd/rsync.passwd

运行完成后,在目标服务器B上查看,在/data/test目录下有test文件夹,说明数据同步成功。

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

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

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

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

查看系统默认参数值:
[root@rsync ~]# sysctl -a | grep max_queued_events 
fs.inotify.max_queued_events = 16384
[root@rsync ~]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
[root@rsync ~]# sysctl -a | grep max_user_watches 
fs.inotify.max_user_watches = 8192
修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
[root@rsync ~]# vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq! #保存退出

参数说明:**
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/wwwroot/ -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/wwwroot/为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值

5.2.3、安装sersync

wget https://sersync.googlecode.com/files/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  #移动目录到/usr/local/sersync

5.2.4、配置sersync

cd  /usr/local/sersync #进入sersync安装目录
mkdir /usr/local/sersync/logs
touch /usr/local/sersync/logs/rsync_fail_log.sh
cp confxml.xml confxml.xml-bak  #备份原文件
vim confxml.xml  #编辑,修改下面的代码
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="Aip" port="58008"></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="/data/test">
            <remote ip="B" name="rsync_data"/>
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync" passwordfile="/etc/rsyncd/rsync.passwd"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/logs/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="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>
:wq!  #保存退出

参数说明 :

localpath watch="/data/test ":#源服务器同步目录
name="rsync_data ": #目标服务器rsync同步目录模块名称
users="rsync": #目标服务器rsync同步用户名
passwordfile="/etc/rsyncd/rsync.passwd ": #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip="B":  #目标服务器ip,每行一个
failLog path="/usr/local/sersync/logs/rsync_fail_log.sh "  #脚本运行失败日志记录
start="true"  #设置为true,每隔600分钟执行一次全盘同步

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

#启动守护
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
echo "PATH=$PATH:/usr/local/sersync/" >> /etc/profile
source /etc/profile
#开机自启
vim /etc/rc.d/rc.local  #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本
#240419需要注意的是/etc/rc.d/rc.local文件默认是不执行的,所以需要给他授权
chmod +x /etc/rc.d/rc.local
#配置多实例
配置多实例只需对应不同的配置文件即可
/usr/local/sersync/sersync2  -d –r -o /usr/local/sersync/config-a.xml
/usr/local/sersync/sersync2  -d –r -o /usr/local/sersync/config-b.xml
/usr/local/sersync/sersync2  -d –r -o /usr/local/sersync/config-c.xml 

#关闭:通过ps aux |grep sersync查询进程号然后关闭
#启动:执行/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/xxx.xml,如果是新增的,则需要放入开机自动中!

5.2.6、添加脚本监控sersync是否正常运行

vim /script/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 -d -r -o $confxml &
else
exit 0;
fi
chmod +x /script/check_sersync.sh #添加脚本执行权限
crontab -e #编辑,在最后添加下面一行
*/5 * * * * sh /script/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本
crontab -l  #查看一下定时任务列表

5.2.7、测试sersync实时触发rsync同步脚本是否正常运行

在源机器的/data/test目录下创建文件夹或者文件,然后到目标机器上的目录下查看文件是否正常同步

六、常见报错

6.1、测试数据同步报错 @ERROR: auth failed on module rsync_data

1、检查源机器和目录机器的rsync的版本是否一致,
2、目标服务器 secrets file 权限必须是600

6.2、Rsync 启动报错 failed to create pid file /var/run/rsyncd.pid: File exists

failed to create pid file /var/run/rsyncd.pid: File exists
解决方法  rm –rf /var/run/rsyncd.pid 再重新启动rsync服务

6.3、ERROR: password file must not be other-accessible

解决方法,文件权限问题 chmod 600 /etc/rsyncd/rsync.passwd

6.4、@ERROR: chdir failed

@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]

排查目录服务器日志/var/log/rsyncd.log得知,是目录服务器配置的目录不存在,需要创建
  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值