Rsync+inotify时间服务器之间文件实时同步

Rsync+inotify时间服务器之间文件实时同步

Rsync+inotify时间服务器之间文件实时同步

Rsync+inotify时间服务器之间文件实时同步

rsync介绍

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

inotify介绍

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

环境介绍

1、服务器相关信息

  Server1:
  OS: CentOS6.7
  IP: 192.168.190.129  
  Dir: /home/ops/test  
  Server2:
  OS: CentOS7
  IP: 192.168.190.131  
  Dir: /home/ops/test

2、安装rsync服务,在CentOS6下面.yum安装的是rsync3.0.6.为了保持和CentOS7一致版本.所以源码安装

[root@localhost local]# cd /home/ops/
[root@localhost ops]# wget https://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
[root@localhost ops]# cd rsync-3.0.9
[root@localhost rsync-3.0.9]# ./configure --prefix=/usr/local/rsysnc
[root@localhost rsync-3.0.9]# make && make install

3、创建密码文件

[root@localhost rsync-3.0.9]# cd /usr/local/rsysnc/
[root@localhost rsysnc]# echo "rsync-password" > /usr/local/rsysnc/rsync.passwd
[root@localhost rsysnc]# chmod 600 rsync.passwd

4、安装inotify 服务

[root@localhost rsysnc]# cd /home/ops/
[root@localhost ops]# ls
rsync-3.0.9  rsync-3.0.9.tar.gz
[root@localhost ops]# pwd
/home/ops
[root@localhost ops]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@localhost ops]# tar xf inotify-tools-3.14.tar.gz
[root@localhost ops]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@localhost inotify-tools-3.14]# make && make install

客户端rsync配置,在CentOS7上面配置

1、安装rsync

[root@localhost ops]# yum install rsync

2、设置密码

[root@localhost ops]# echo "webuser:rsync-password" > /etc/rsyncdpasswd.conf
[root@localhost ops]# chmod 600 /etc/rsyncdpasswd.conf
[root@localhost ops]#

3、修改配置文件

[root@localhost ops]# cat /etc/rsyncd.conf
uid = root
gid = root
user 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 = /home/ops/test  #后面不要加"/"
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.190.129
hosts deny = *
list = false
uid = root
gid = root
auth users = webuser
secrets file = /etc/rsyncdpasswd.conf
[root@localhost ops]#

4、启动rsync服务

[root@localhost ops]# systemctl start rsync

5、 在server端中启动脚本,CentOS6上启动

[root@localhost ops]# cat rsync.sh
#!/bin/bash
host=192.168.190.131
src=/home/ops/test ## 注意不加"/"
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/local/rsync/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
[root@localhost ops]#

[root@localhost ops]# nohub bash  rsync.sh &
[1] 15931
[root@localhost ops]#

测试

1、 在server端test目录下面有如下文件我们新添加一个文件和一个目录看是否能同步到客户端

[root@localhost ops]# ls test/
111  123  1234  rsync.log
[root@localhost ops]#

2、 创建文件测试

[root@localhost ops]# ls test/
111  123  1234  rsync.log
[root@localhost ops]# mkdir test/testrsync
[root@localhost ops]# touch test/touchrsync
[root@localhost ops]# ls test/
111  123  1234  rsync.log  testrsync  touchrsync
[root@localhost ops]#

3、客户端查看

[root@localhost test]# ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'
192.168.190.131
[root@localhost test]# ls
111  123  1234  rsync.log  testrsync  touchrsync
[root@localhost test]#