前言
之前的文章又写过rsync+inotify数据实时同步,在文章最后我说过了要更新出来rsync+sersync,今天来仔细分析分析一下他们两者的区别
rsync+sersync简述
- sersync是
基于Inotify开发
的,类似于Inotify-tools的工具 - sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。
Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?
rsync+inotify-tools
-
Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),
并没有把具体
是哪个文件或者哪个目录发生了变化记录下来; -
rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对
整个目录进行同步
,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。
rsync+sersync
- sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)
具体
某一个文件或某一个目录的名字; - rsync在同步的时候,
只同步发生变化
的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
总结:当同步的目录数据量不大时,中小型公司建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。
实验环境
名称 | IP | 操作系统 |
---|---|---|
源服务器 | 192.168.10.1 | Centos7.3 |
目标服务器 | 192.168.10.5 | Centos7.3 |
目的:实现源服务器/var/www/html
的内容备份同步到目标服务器/rsync
目标服务器部署rsync
关闭防火墙或者开启873端口
[root@rsync ~]# systemctl stop firewalld
[root@rsync ~]# setenforce 0
rsync默认已经集成了Linux7.x图形化环境,不过我们也可以使用yum源下载个最新版也是没问题的
[root@rsync ~]# yum -y install rsync
[root@rsync ~]# vim /etc/rc.d/rc.local #设置开机自启动
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
[root@rsync ~]# chmod +x /etc/rc.d/rc.local
[root@rsync ~]# systemctl start rsyncd
创建rsyncd.conf配置文件
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
secretsfile = /etc/rsyncd.pass
motd file = /etc/rsyncd.motd
[rsync]
path = /rsync
comment = rsync
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
maxconnections = 200
timeout = 600
auth users = rsync
hosts allow = 192.168.10.0/24
# hosts deny =
配置文件详细说明
log file =/var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile =/var/run/rsyncd.pid #pid文件的存放位置
lock file =/var/run/rsync.lock #支持max connections参数的锁文件
secretsfile = /etc/rsyncd.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file =/etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
[rsync] #共享模块名称,若需要同步到多个目录,可以写多个
path = /var/www/html/#rsync服务端数据目录路径
comment = rsync#模块名称与[md]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873 #默认端口
use chroot= no #默认为true,修改为no,增加对目录文件软连接的备份
read only =no #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
maxconnections = 200 #最大连接数
timeout =600 #设置超时时间
auth users= rsync #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow= 192.168.10.0/24 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny= #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
创建用户文件
格式,用户名:密码,可以设置多个,每行一个用户名:密码
[root@rsync rsync]# cat /etc/rsyncd.pass
rsync:123.com
[root@rsync ~]# chmod 600 /etc/rsyncd.conf
[root@rsync ~]# chmod 600 /etc/rsyncd.pass
[root@rsync ~]#systemctl restart rsyncd #重新启动
配置源服务器rsync客户端
同上,先关闭防火墙或开放端口,然后下载rsync
在配置文件添加需要发布同步的目录
[root@sersync ~]# systemctl stop firewalld
[root@sersync ~]# setenforce 0
[root@serysyc ~]# whereis rsync
rsync: /usr/bin/rsync /etc/rsync.pass /usr/share/man/man1/rsync.1.gz
[root@serysyc ~]# vim /etc/rc.d/rc.local
/usr/bin/rsync --daemon
[root@serysyc ~]# vim /etc/rsyncd.conf
log file = /var/log/rsync.log
pidfile = /var/run/rsync.pid
lock file = /var/run/rsync.lock
motd file = /etc/rsync.motd
[rsync] # 应该与目标服务器的共享模块名称对应,有多个可以添加多个分别对应自己模块名来同步多目录多数据
comment = rsync
uid = root
gid = root
port = 873
hosts allow = 192.168.10.0/24
path = /var/www/html
read only = no
auth users = rsync
secrets file = /etc/passwd.txt
[root@serysyc ~]# chmod +x /etc/rc.d/rc.local
[root@serysyc ~]# systemctl start rsyncd
创建认证密码文件
编辑文件,添加以下内容,该密码应与目标服务器中的/etc/rsyncd.pass中的密码一致
[root@serysyc ~]# vim /etc/passwd.txt
123.com
[root@serysyc ~]# chmod 600 /etc/passwd.txt
测试从源服务器向目标服务器同步数据
[root@serysyc ~]# yum -y install httpd
[root@serysyc ~]# cd /var/www/html/
[root@serysyc html]# echo '123' > index.html
[root@serysyc ~]# rsync -avH --delete /var/www/html/ rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt
目标服务器查看同步的数据
[root@rsync rsync]# ll
总用量 4
-rw-r--r--. 1 root root 4 6月 14 20:11 index.html
[root@rsync rsync]# cat index.html
123
源服务器安装sersync工具,实时触发rsync进行同步
sersync软件包:
链接:https://pan.baidu.com/s/1VI_Gm-FbQPe3cFyZtJeIuA
提取码:4sku
上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/root目录下
[root@serysyc ~]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@serysyc ~]# mv GNU-Linux-x86/ /usr/local/sersync
[root@serysyc ~]# cd /usr/local/sersync/
[root@serysyc sersync]# cp confxml.xml confxml.xml.bak
[root@serysyc sersync]# vim confxml.xml
参数说明:
localpath watch="/var/www/html":#源服务器同步目录
192.168.10.5:#目标服务器IP地址
name=“rsync”: #目标服务器rsync同步目录共享模块名称
users=“rsync”: #目标服务器rsync同步用户名
passwordfile="/etc/passwd.pass": #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip=“192.168.10.5”: #目标服务器ip,每行一个
failLogpath="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录
start=“true” #设置为true,每隔600分钟执行一次全盘同步
🆗,现在检测实时同步
在源服务器/var/www/html目录写入数据
[root@serysyc html]# echo '123' > 2.txt
[root@serysyc html]# echo '123.com' > 3.txt
[root@serysyc html]# echo '123.com' > 4.txt
[root@serysyc ~]# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
## 执行后完成此命令后,会一直处在持续监控状态,达到实时同步目的
....
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /var/www/html && rsync -artuz -R --delete ./ rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt >/dev/null 2>&1
run the sersync:
watch path is: /var/www/html
在目标服务器查看同步情况
[root@rsync rsync]# ll
总用量 20
-rw-r--r--. 1 988 1001 7 6月 14 23:39 1.txt
-rw-r--r--. 1 root root 4 6月 14 21:57 2.txt
-rw-r--r--. 1 root root 8 6月 14 22:00 3.txt
-rw-r--r--. 1 root root 7 6月 14 22:27 4.txt
-rw-r--r--. 1 988 1001 4 6月 14 20:11 index.html
设置sersync监控开机自动执行
结合crontab计划任务,定时检查sersync2是否运行,如果没有,则执行脚本重新执行/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
来持续监控源服务器
vim /etc/rc.d/rc.local #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行此服务
chmod +x /etc/rc.d/rc.local
添加脚本监控sersync正常运行
#!/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
做计划任务,每一小时检查一次
* */1 * * * /root/check_sersync.sh
遇到的报错及解决方法
报错一:
如果前面测试连接时就报错
如下:
[root@serysyc ~]# rsync -avH --delete /var/www/html/ rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt
sending incremental file list
rsync: failed to set times on "." (in rsync): Operation not permitted (1)
rsync: delete_file: unlink(aaaa) failed: Permission denied (13)
./
1.txt
rsync: mkstemp ".1.txt.g3qgm2" (in rsync) failed: Permission denied (13)
sent 132 bytes received 30 bytes 324.00 bytes/sec
total size is 12 speedup is 0.07
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
解决办法
修改配置文件/etc/rsyncd.conf
uid = root
gid = root
因为同步的数据在源服务器可能是root管理,同步过来还得是root,再者就是把源数据的所属者改成和目标服务器的一样,在同步数据。
报错二:
如下
[root@sersync ~]# rsync -avzH --delete /var/www/html rsync@192.168.10.5::rsync --password-file=/etc/passwd.txt
rsync: failed to connect to 192.168.10.5 (192.168.10.5): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
解决办法:
有经验的人一看报错信息就知道是rsyncd服务没有起来
重启rsyncd
systemctl start rsyncd
确保873端口开启
[root@sersync ~]# telnet 192.168.10.5 873
Trying 192.168.10.5...
Connected to 192.168.10.5.
Escape character is '^]'.
@RSYNCD: 31.0
[root@sersync ~]# netstat -anpt |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3725/rsync
tcp 0 0 192.168.10.1:52144 192.168.10.5:873 TIME_WAIT -
tcp6 0 0 :::873 :::* LISTEN 3725/rsync
还有如果都没有问题,检查你的防火墙
,是否关闭或开放873端口