linux之rsync+inotify实现服务器之间文件的同步

2019-06-26  本文已影响0人  燃燃的爸爸

1、rsync

与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

2、inotify

Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

一、服务端

ip地址:192.168.0.88
环境:centos6.8
1、安装rsync

该版本的已安装rsync,如果没有,可使用yum安装(也可以使用源码安装,这里就不多做介绍了):

yum install rsync xinetd
#配置xinetd: vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no

启动xinetd服务:
service xinetd start</pre>

2、建立密码认证文件

[root@Server ]# echo "rsync-pwd" >/etc/rsync.passwd #其中rsync-pwd可以自己设置密码,rsync.passwd名字也可以自己设置

[root@Server]# chmod 600 /etc/rsync.passwd #无论是为了安全,还是为了避免出现以下错误,密码文件都需要给600权限</pre>

3、安装inotify

[root@Server]# cd /usr/src/
[root@Server src]# wget https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@Server src]# tar zxvf inotify-tools-3.14.tar.gz
[root@Server src]# cd inotify-tools-3.14
[root@Server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@Server inotify-tools-3.14]# make
[root@Server inotify-tools-3.14]# make install

4、创建rsync复制脚本

此项功能主要是将server端的目录/tmp里的内容,如果修改了(无论是添加、修改、删除文件)能够通过inotify监控到,并通过rsync实时的同步给client的/tmp里,下面是通过shell脚本实现的。

#!/bin/bash 
host=192.168.12.15
src=/tmp/ 
des=web 
user=webuser 
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ 
| while read files do 
       /usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src 
       $user@$host::$des 
echo "${files} was rsynced" >>/var/log/rsync.log 2>&1 
done

其中host是client的ip,src是server端要实时监控的目录,des是认证的模块名,需要与client一致,user是建立密码文件里的认证用户。
把这个脚本命名为rsync.sh,并给予执行权限

[root@Server tmp]# chmod 764 /root/rsync.sh

然后运行这个脚本

[root@Server tmp]# sh /root/rsync.sh &

请记住,只有在备份服务器client端的rsync安装并启动rsync之后,在启动rsync.sh脚本,否则有时候会满屏出现:

rsync: failed to connect to 192.168.1.22: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]

我们还可以把rsync.sh脚本加入到开机启动项里
[root@Server tmp]# echo "/root/rsync.sh" >> /etc/rc.local

二、客户端服务器

ip地址:192.168.12.15
环境:centos6.8

1、安装rsync(备份服务器只安装rsync)

安装rsync和xinetd,并创建目录: yum install rsync xinetd

#配置xinetd: vi /etc/xinetd.d/rsync
#disable = yes修改为
disable = no

启动xinetd服务:
service xinetd start</pre>

2、建立用户与密码认证文件

[root@Client]# echo "webuser:rsync-pwd" > /etc/rsync.passwd #请记住,在server端建立的密码文件,只有密码,没有用户名;而在备份服务端client里建立的密码文件,用户名与密码都有。

[root@Client]# chmod 600 /etc/rsync.passwd #需要给密码文件600权限

3、建立rsync配置文件

vim /etc/rsyncd.conf
uid = root 
gid = root 
use chroot = no 
max connections = 10 strict modes = yes 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log 
[web] 
path = /tmp/ comment = web file ignore errors 
read only = no write only = no 
hosts allow = 192.168.10.220 hosts deny = * list = false uid = root 
gid = root 
auth users = webuser 
secrets file = /etc/rsync.passwd

其中web是server服务端里的认证模块名称,需要与主服务器里的一致,以上的配置我的自己服务器里的配置,以供参考。

4、启动rsync

service xinetd start
chkconfig xinetd on   #设置开机自启动

三、开启测试
服务端执行脚本rsync.sh,在/tmp下执行对文件的增删改查
在客户端服务器会有一致的操作

image.png

该文章主要内容来自转载!

上一篇 下一篇

猜你喜欢

热点阅读