【解决多台linux服务器间的文件实时同步备份问题】

1.1 inotify相关介绍

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同步,这样刚好解决了同步数据的实时性问题。

1.2 rsync+inotify同步逻辑图

在这里插入图片描述

1.3 环境部署

1、下载所需的安装包:

先把两个包放在/usr/src/下。
a、rsync下载路径:http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz/
b、inotify下载路径:http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

2、第一台服务器(192.168.188.90)配置(不分主次)

a、安装rsync:
[root@nginx ~]# cd /usr/src/ 
[root@nginx src]# tar zxvf rsync-3.0.9.tar.gz 
[root@nginx src]# cd rsync-3.0.9 
[root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync 
[root@nginx rsync-3.0.9]# make 
[root@nginx rsync-3.0.9]# make install
b、创建密码认证文件:
[root@nginx rsync-3.0.9]# cd /usr/local/rsync/
[root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync1.passwd
c、给密码文件赋予600权限:
[root@nginx rsync]# chmod 600 rsync1.passwd
d、安装inotify:
[root@nginx rsync]# cd /usr/src/ 
[root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz 
[root@nginx src]# cd inotify-tools-3.14 
[root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify 
[root@nginx inotify-tools-3.14]# make 
[root@nginx inotify-tools-3.14]# make install
e、创建监控脚本:
#!/bin/bash 
host=192.168.188.89
src=/root/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/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync1.passwd $src $user@$host::$des 
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 
done

注意:host为第二台服务器IP,src为要监控的路径,web是认证模块名称,最后把监控脚本命名为rsync1.sh放在要监控的路径下。此处应是/root/test/
f、给监控脚本赋予764权限:

[root@nginx tmp]# chmod 764 rsync1.sh
     g、创建rsync配置文件:
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 = /root/test/
comment = web file
ignore errors 
read only = no 
write only = no 
hosts allow = 192.168.188.89 
hosts deny = * 
list = false
uid = root 
gid = root 
auth users = webuser 
secrets file = /usr/local/rsync/rsync1.passwd

该配置文件是用来接收另一台服务器的文件。其中web是server服务端(90服务器)的认证模块名称,需要与90服务器里的一致。把配置文件命名为rsync1.conf,放到/usr/local/rsync/目录里

 h、启动该配置文件
[root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf
需要开机启动的话:
[root@nginx-backup rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync1.conf" >> /etc/rc.local

3、第二台服务器(192.168.188.89)配置(不分主次):

 a、安装rsync:
[root@nginx ~]# cd /usr/src/ 
[root@nginx src]# tar zxvf rsync-3.0.9.tar.gz 
[root@nginx src]# cd rsync-3.0.9 
[root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync 
[root@nginx rsync-3.0.9]# make 
[root@nginx rsync-3.0.9]# make install
b、创建密码认证文件:
[root@nginx rsync-3.0.9]# cd /usr/local/rsync/
[root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync2.passwd
c、给密码文件赋予600权限:
[root@nginx rsync]# chmod 600 rsync2.passwd 
d、安装inotify:
[root@nginx rsync]# cd /usr/src/ 
[root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz 
[root@nginx src]# cd inotify-tools-3.14 
[root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify 
[root@nginx inotify-tools-3.14]# make 
[root@nginx inotify-tools-3.14]# make install
e、创建监控脚本:
#!/bin/bash 
host=192.168.188.90
src=/root/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/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync2.passwd $src $user@$host::$des 
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 
done

注意:host为第二台服务器IP,src为要监控的路径,web是认证模块名称,最后把监控脚本命名为rsync2.sh放在要监控的路径下。此处应是/root/test/

 f、给监控脚本赋予764权限:
[root@nginx tmp]# chmod 764 rsync2.sh
     g、创建rsync配置文件:
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 = /root/test/
comment = web file
ignore errors 
read only = no 
write only = no 
hosts allow = 192.168.188.90 
hosts deny = * 
list = false
uid = root 
gid = root 
auth users = webuser 
secrets file = /usr/local/rsync/rsync2.passwd

该配置文件是用来接收另一台服务器的文件。其中web是server服务端(90服务器)的认证模块名称,需要与90服务器里的一致。把配置文件命名为rsync2.conf,放到/usr/local/rsync/目录里

 h、启动该配置文件
[root@nginx-backup rsync]# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf

需要开机启动的话:

[root@nginx-backup rsync]# echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync2.conf" >> /etc/rc.local

4、最后启动两台服务器的监控脚本

a、在90服务器: 
[root@nginx tmp]# sh /root/test/rsync1.sh &
需要开机启动的话:
[root@nginx tmp]# echo "/root/test/rsync1.sh" >> /etc/rc.local
b、在89服务器:
[root@nginx tmp]# sh /root/test/rsync2.sh &

需要开机启动的话:

[root@nginx tmp]# echo "/root/test/rsync2.sh" >> /etc/rc.local 

测试:

任意在其中一台服务器/root/test/路径下,新增一个文件,你会发现另一台服务器也同步了该文件。
同步之后,两台服务器下都会有rsync1.sh和rsync2.sh监控脚本,注意勿删!

如果需要3台或3台以上的服务器之间进行同步,则需要更改shell脚本,方法类似。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值