rsync搭配inotify实现服务器之间数据实时同步

简介

rsync

如果你man一下sync的话,就会发现:sync-flush file system buffers,它是一个把缓冲区中的数据同步到文件系统中的一个命令;而rsync其实就是remote rsync,它是一个远程同步工具,兼具cp和scp的功能,rsync命令的使用几乎和scp是一样一样的。rsync是通过超级守护进程xinetd进行触发同步的。

优点:与cp和scp相比来说,rsync更快,更安全,支持增量备份。rsync在数据同步的过程中,不像cp一样全部都拷贝,而是先去比对特征码,只有不一样的才会去拷贝,如果一样了,就不需再做多余操作。通过使用rsync+crontab可以解决对实时性要求不是太高的场景。
缺点:在这个大数据年代,如果你的数据量非常大,你每做一次任务计划,rsync都会先去遍历目标目录,把所有数据做一次特征码比对,然后进行差量传输,这个过程会是很漫长的,对于那些要求实时性更新比较高的企业来说,无疑是一场噩梦。但是如果能够出现一个工具能够实时的去监控我们的文件系统,只有在数据改变时才会触发它去同步,那该有多好啊!为了满足广大需求者的心声rsync+inotify组合就出现了

inotify

Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

rsync+inotify之推荐理由

服务器性能:rsync+crontab会定时去检查是否有文件更新,这势必会造成服务器性能下降;而rsync+inotify组合是触发式更新,只有在数据文件有变化时,才会去更新,因此相对前者而言,是提高了服务器性能
数据实时性:rsync+crontab是周期性任务计划,不能保证数据的实时性;rsync+inotify组合是触发式更新,只要有数据变化,就立刻同步更新

搭建

环境配置

服务器 系统 IP 软件
服务端 CnetOS7.6 172.18.19.47 rsync
客户端 CentOS7.6 172.18.19.46 rsync、inotify

服务端配置

安装

1
yum -y install rsync

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# cat /etc/rsyncd.conf |grep -Ev '^$|^#'
uid = root
gid = root
use chroot = yes
max connections = 5
pid file = /var/run/rsyncd.pid
lockfile = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
timeout = 900

[nginx_ssl]
path = /data/nginx/ssl
ignore errors = yes
read only = no
write only = no
hosts allow = 172.18.19.46
hosts deny = *
list = yes
uid = root
gid = root
auth users = sg01
secrets file = /etc/sg01.passwd

[nginx_conf]
path = /data/nginx/conf
ignore errors = yes
read only = no
write only = no
hosts allow = 172.18.19.46
hosts deny = *
list = yes
uid = root
gid = root
auth users = sg01
secrets file = /etc/sg01.passwd

[work_html_sg]
path = /data/work/html_sg
ignore errors = yes
read only = no
write only = no
hosts allow = 172.18.19.46
hosts deny = *
list = yes
uid = root
gid = root
auth users = sg01
secrets file = /etc/sg01.passwd

[work_service_jar]
path = /data/work/service_jar
ignore errors = yes
read only = no
write only = no
hosts allow = 172.18.19.46
hosts deny = *
list = yes
uid = root
gid = root
auth users = sg01
secrets file = /etc/sg01.passwd

[work_shell]
path = /data/work/shell
ignore errors = yes
read only = no
write only = no
hosts allow = 172.18.19.46
hosts deny = *
list = yes
uid = root
gid = root
auth users = sg01
secrets file = /etc/sg01.passwd

配置rsync认证文件/etc/web.passwd

1
2
3
# echo "sg01:123456" > /etc/sg01.passwd
# cat /etc/sg01.passwd
sg01:123456

在实际过程中用户可以自定义,并提高密码复杂度。
修改/etc/sg01.passwd的权限为600

1
2
3
# chmod 600 /etc/sg01.passwd
# ll /etc/sg01.passwd
-rw------- 1 root root 12 Mar 27 15:25 /etc/sg01.passwd

启动

1
2
3
4
5
6
7
8
9
10
11
# systemctl start rsyncd    #启动服务
# ps -ef | grep rsync #检查进程
root 28008 1 0 14:13 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 28115 4478 0 14:15 pts/0 00:00:00 grep --color=auto rsync
# netstat -tnpl #检查873端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 21265/sshd
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 28008/rsync
tcp6 0 0 :::873 :::* LISTEN 28008/rsync
tcp6 0 0 :::3306 :::* LISTEN 15971/mysqld

客户端配置

安装rsync、inotify

1
yum install rsync inotify-tools -y

设置rsync客户端的密码文件,客户端只需要设置rsync同步的密码即可,不用设置用户名。

1
2
3
# echo 123456 > /etc/sg01.passwd
# cat /etc/sg01.passwd
123456

修改/etc/sg01.passwd的权限为600

1
2
3
# chmod 600 /etc/sg01.passwd
# ll /etc/sg01.passwd
-rw------- 1 root root 7 Mar 27 15:26 /etc/sg01.passwd

在客户端上测试能否同步文件

1
2
3
4
rsync --password-file=/etc/sg01.passwd /etc/fstab sg01@172.18.19.47::work_shell
#将客户端文件上传到服务端
rsync --password-file=/etc/sg01.passwd sg01@172.18.19.47::work_shell/fstab /data/backup/
#将服务端的文件下载到客户端

可以查看服务端work_shell模块即目录/data/work/shell和本地/data/backup目录中是否有文件fstab。
客户端同步脚本内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# cat /data/scripts/autorsync.sh 
#!/bin/bash
#Author:Francis

list=(1 2 3 4 5)

array_src=(1 2 3 4 5)
array_src[1]=/data/nginx/ssl/
array_src[2]=/data/nginx/conf/
array_src[3]=/data/work/html_sg/
array_src[4]=/data/work/service_jar/
array_src[5]=/data/work/shell/

array_des=(1 2 3 4 5)
array_des[1]=nginx_ssl
array_des[2]=nginx_conf
array_des[3]=work_html_sg
array_des[4]=work_service_jar
array_des[5]=work_shell

host=172.18.19.47
user=sg01

:<<'
for i in ${list[*]};do
/usr/bin/rsync -vzrtopg --delete --progress ${array_src[$i]} $user@$host::${array_des[$i]} --password-file=/etc/sg01.passwd
done
'

for i in ${list[*]};do
{
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib ${array_src[$i]} | while read file DATE TIME DIR;
do
/usr/bin/rsync -vzrtopg --delete --progress ${array_src[$i]} $user@$host::${array_des[$i]} --password-file=/etc/sg01.passwd
#echo " ${file} was rsynced" >>/tmp/autorsync.log 2>&1
done
} &
done
wait

将脚本以后台方式启动

1
nohup sh autorsync.sh &

脚本使用多进程方式,批量停止

1
ps -ef|grep autorsync|grep -v grep | awk '{print $2}'| xargs kill -9

测试

在客户端目录/data/nginx/ssl/、/data/nginx/conf/、/data/work/html_sg/、/data/work/service_jar/、/data/work/shell/中进行文件修改、创建、删除等操作,来查看服务端对应的目录文件是否同步成功。

本文标题:rsync搭配inotify实现服务器之间数据实时同步

文章作者:Francis

原始链接:http://www.cnops.com/posts/68c5ac4c.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。