您的位置:首页 > 其它

企业实时同步方案----Rsync+Inotify-Tools 推荐

2014-06-28 03:43 561 查看
在博文RHCE系列之备份工具----镜像备份Rsync中,我带大家了解 rsync 的诸多特性以及它所支持的四种模式。作为一个镜像备份工具,可以说 rsync 做的很出色。
可是,随着应用系统规模的不断扩大,我们对数据的安全性和可靠性方面的需求也越来越高!Rsync 在高端业务系统中的不足也逐渐暴露了出来。
首先,rsync 在同步数据时,需要扫描所有文件后才进行比对,然后再进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的一个操作,并且往往发生变化的是其中很小的一部分,那么这将是非常低效的方式。
其次,rsync 不能实时的去监测和同步数据。虽然我们使用crontab的方式,加上 rsync 自身以守护进程启动的方式实现触发同步,但是两次触发动作一定会有时间差。受限于crontab最短也是1分钟,因此这就导致了服务端和客户端数据可能出现不一致,更无法在应用故障时做到数据的完全恢复。
为了满足这方面的需求,我们就结合了 linux文件系统事件监控机制这样一个系统特性(即inotify),通过使用工具 inotify-tools,整合出了 rsync+inotify 这样的一个技术架构,来实现数据实时同步的功能!
下面就跟着我一起来部署一下 rsync+inotify-tools 架构吧!

简易架构图




实验环境介绍:

下面,就开始我们的实验!

一、在两台 SLAVE 机器上部署 rsync 服务端程序
特别提醒:本文的Slave机器即为S1(172.16.100.1),S2(172.16.100.2)。此处仅以S1的rsync服务端部署为例,S2的部署和S1一样,此处不再敖述。
1、安装rsync
[root@rsync-server-1 ~]# yum install rsync -y


2、修改rsync配置文件
[root@rsync-server-1 ~]# cat > /etc/rsyncd.conf << EOF
#Rsync server
#created by sunsky 00:17 2013-06-28
##rsyncd.conf start##
uid = root      # rsync对后面模块中的path路径拥有什么权限
gid = root      # rsync对后面模块中的path路径拥有什么权限
use chroot = no      # 安全操作
max connections = 2000      # 定义连接数2000
timeout = 600      # 600秒超时
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors      # 忽略错误
read only = false    # false才能上传文件,true不能上传文件
list = false    # 文件列表
hosts allow = 172.16.100.0/24
hosts deny = *
auth users = rsync_backup    # 虚拟用户,同步时需要用这个用户
secrets file = /etc/rsync.password      # 密码文件
#####################################
[web]      # 模块名称
comment = redhat.sx site files by sunsky 00:17 2013-06-28    # 注释
path = /data/web/redhat.sx                 # 模块的路径
####################################
[data]
comment = redhat.sx site sit data files  by sunsky 00:17 2013-06-28
path = /data/web_data/redhat.sx
#####################################
EOF


特别提示:此处,使用一个多目录同步的案例。大家可以看需求,如果需求只有一个,那仅仅做一个目录即可。

上面的rsync服务的配置文件,表明允许172.16.100.0网段的主机访问,rsync同步模块名为[web]和[data],将同步过来的文件分别放入对应path指定的目录/data/web/redhat.sx,/data/web_data/redhat.sx下。
如果有多台目标服务器,则每一台都需要进行类似的rsync服务端配置,上面的uid和gid需要换成你服务器的相应的同步用户。注意,rsync服务账户(本文用root)要有对被同步目录(/data/web/redhat.sx和/data/web_data/redhat.sx)的写入和更新权限。

3、创建相关待同步目录
[root@rsync-server-1 ~]# mkdir /data/{web,web_data}/redhat.sx -p
[root@rsync-server-1 ~]# tree /data
/data
├── web_data
│   └── redhat.sx
└── web
└── redhat.sx


提示:此步在S1,S2上都要执行,否则,rsync服务会因为没有PATH路径而无法启动。

4、相关认证和权限项配置
[root@rsync-server-1 /]# echo 'rsync_backup:redhat' > /etc/rsync.password
[root@rsync-server-1 /]# chmod 600 /etc/rsync.password
[root@rsync-server-1 /]# cat /etc/rsync.password
rsync_backup:redhat
[root@rsync-server-1 /]# ll /etc/rsync.password
-rw-------. 1 root root 20 Jun  4 04:27 /etc/rsync.password


5、以守护进程方式启动rsync服务
[root@rsync-server-1 /]# rsync --daemon


6、查看rsync服务状态
[root@rsync-server-1 /]# lsof -i tcp:873
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   10309 root    4u  IPv4  94045      0t0  TCP *:rsync (LISTEN)
rsync   10309 root    5u  IPv6  94046      0t0  TCP *:rsync (LISTEN)


7、为rsync添加开机自启动
[root@rsync-server-1 /]# echo "# rsyncd service daemon by sun 20140628" >>/etc/rc.local
[root@rsync-server-1 /]# echo "/usr/bin/rsync --daemon"  >> /etc/rc.local
[root@rsync-server-1 /]# grep daemon /etc/rc.local
# rsyncd service daemon by sun 20140628
/usr/bin/rsync --daemon


这里顺带附上重启的命令,rsync重启有点麻烦,需要以先杀掉后台守护进程,然后再启动的方式来重启服务。
[root@rsync-server-1 /]# pkill rsync
[root@rsync-server-1 /]# rsync --daemon


二、在M1 上配置rsync客户端
1、安装Rsync并配置相关权限
在Master上配置rsync客户端相关权限认证:
[root@rsync-client-inotify /]# yum install rsync -y
[root@rsync-client-inotify /]# echo "redhat">/etc/rsync.password
[root@rsync-client-inotify /]# chmod 600 /etc/rsync.password
[root@rsync-client-inotify /]# cat /etc/rsync.password
Redhat
[root@rsync-client-inotify /]# ll /etc/rsync.password
-rw-------. 1 root root 20 Jun  4 04:27 /etc/rsync.password


2、M1上手动测试rsync的同步情况
特别强调:此步很关键,如果这不能同步,后面的inotify配好了也不会同步数据。
1)分别创建待同步数据
[root@rsync-client-inotify ~]# mkdir /data/{web,web_data}/redhat.sx -p
[root@rsync-client-inotify ~]# touch /data/{web/redhat.sx/index.html,web_data/redhat.sx/a.jpg}
[root@rsync-client-inotify ~]# tree /data
/data
├── web_data
│   └── redhat.sx
│       └── a.jpg
└── web
└── redhat.sx
└── index.html

4 directories, 2 files


2)执行同步命令
针对S1(172.16.100.1):
[root@rsync-client-inotify ~]# rsync -avzP /data/web/redhat.sx rsync_backup@172.16.100.1::web/ --password-file=/etc/rsync.password
[root@rsync-client-inotify ~]# rsync -avzP /data/web_data/redhat.sx  rsync_backup@172.16.100.1::data/ --password-file=/etc/rsync.password


针对S2(172.16.100.2):
[root@rsync-client-inotify ~]# rsync -avzP /data/web/redhat.sx rsync_backup@172.16.100.2::web/ --password-file=/etc/rsync.password
[root@rsync-client-inotify ~]# rsync -avzP /data/web_data/redhat.sx  rsync_backup@172.16.100.2::data/ --password-file=/etc/rsync.password
提示:在后面进行部署inotify之前,inotify主服务器(即rsync-client-inotify)上必须要确保手动可以把文件推送到S1,S2上,这样后续inotify-tools才能调用这些命令来自动推送。
同步完之后,分别对S1,S2的相应目录进行查看!此处以 S1 为例:
[root@rsync-server-1 ~]# tree /data
/data
├── web_data
│   └── redhat.sx
│       └── a.jpg
└── web
└── redhat.sx
└── index.html

4 directories, 2 files


三、在M1 上配置inotify
1、查看 M1的 内核是否支持inotify
[root@rsync-client-inotify ~]# uname -r
2.6.32-431.el6.x86_64
[root@rsync-client ~]# ll /proc/sys/fs/inotify/*
-rw-r--r-- 1 root root 0 Jun  4 15:30 /proc/sys/fs/inotify/max_queued_events
-rw-r--r-- 1 root root 0 Jun  4 15:30 /proc/sys/fs/inotify/max_user_instances
-rw-r--r-- 1 root root 0 Jun  4 15:30 /proc/sys/fs/inotify/max_user_watches


2、安装inotify
[root@rsync-client-inotify ~]# yum install make  gcc gcc-c++
[root@rsync-client-inotify ~]# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz [root@rsync-client ~]# tar xzf inotify-tools-3.13.tar.gz
[root@rsync-client ~]# cd inotify-tools-3.13
[root@rsync-client inotify-tools-3.13]# ./configure
[root@rsync-client inotify-tools-3.13]# make && make install


3、查看inotify提供的工具
[root@rsync-client-inotify ~]# ll /usr/local/bin/inotify*
-rwxr-xr-x. 1 root root 38582 Jun  3 22:23 /usr/local/bin/inotifywait
-rwxr-xr-x. 1 root root 40353 Jun  3 22:23 /usr/local/bin/inotifywatch


在inotify-tools的博文中,我已经详尽的介绍过了inotify-tools的两个命令的用法,这里就不多做介绍!

四、rsync和inotify-tools做结合
我们知道rsync可以实现推送和拉取,而inotify-tools借助内核的inotify机制实现了文件的实时监控。因此,借助这个思路,我们可以通过使用shell脚本,调整inotifywait的输出格式,然后借助inotifywait的监控文件或目录实时变化去通知rsync做相应的推送或者拉取操作!
1、实时备份脚本
下面,我就贴出来,我所使用的脚本!
[root@rsync-client-inotify ~]# cat auto_rsync.sh
#!/bin/bash
src1='/data/web/redhat.sx/'
src2='/data/web_data/redhat.sx/'
des1=web
des2=data
host1=172.16.100.1
host2=172.16.100.1
user=rsync_backup
allrsync='/usr/bin/rsync -rpgovz --delete --progress'
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %w%f %e' -e modify,delete,create,attr
ib $src | while read DATE TIME DIR FILE EVENT;
do
case $DIR in
${src1}*)
$allrsync $src1 $user@$host1::$des1 --password-file=/etc/rsync.password && echo "$DATE $TIME $FILE was rsynced" &>> /var/log
/rsync-$des1-$host1.log
$allrsync $src1 $user@$host2::$des1 --password-file=/etc/rsync.password && echo "$DATE $TIME $FILE was rsynced" &>> /var/log
/rsync-$des1-$host2.log
;;
${src2}*)
$allrsync  $src2 $user@$host1::$des2 --password-file=/etc/rsync.password && echo "$DATE $TIME $FILE was rsynced" &>> /var/lo
g/rsync-$des2-$host1.log
$allrsync  $src2 $user@$host2::$des2 --password-file=/etc/rsync.password && echo "$DATE $TIME $FILE was rsynced" &>> /var/lo
g/rsync-$des2-$host2.log
;;
esac
done


2、加入开机自启动
另外,为了保证开机之后自动开始实时同步备份,因此我们需要将该脚本加入开机自启动,并且设置在启动的时候,将脚本执行输出重定向到指定日志文件以及放到后台运行!
[root@rsync-client-inotify ~]# chmod o+x auto_rsync.sh
[root@rsync-client-inotify ~]# mv auto_rsync.sh /usr/sbin/
[root@rsync-client-inotify ~]# vim /etc/rc.local
Bash /usr/sbin/auto_rsync.sh &>> /var/log/auto_rsync.log &


以上,就是rsync+inotify-tools的整体架构操作了,下面我将我个人做的一些同步测试,贴在这里方便大家对照学习!

SLAVE:
[root@rsync-server-1 ~]# tree /data/
/data/
├── web
│   └── redhat.sx
└── web_data
└── redhat.sx

4 directories, 0 files
[root@rsync-server-2 ~]# tree /data/
/data/
├── web
│   └── redhat.sx
└── web_data
└── redhat.sx

4 directories, 0 files


MASTER机:
[root@rsync-client-inotify ~]# tree /data/
/data/
├── web
│   └── redhat.sx
└── web_data
└── redhat.sx

4 directories, 0 files


以上为初始状态,现在我运行 auto_rsync.sh 脚本,然后在 M1上执行如下命令,最后分别在S1和S2上查看同步状态,已经同步的时间!
我在这里模拟分别向/data/web/redhat.sx和/data/web_data/redhat.sx各写入10个10M的小文件,然后使用ls命令对比M1和S1与S2的同步时间!
[root@rsync-client-inotify ~]# bash /usr/sbin/auto_rsync.sh &>> /var/log/auto_rsync.log &
[1] 40802
[root@rsync-client-inotify ~]# for i in {1..10};do dd if=/dev/zero of=/data/web/redhat.sx/$i.html bs=10M count=1 ;dd if=/dev/zero of=/data/web_data/redhat.sx/$i.html bs=10M count=1;done
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0370034 s, 283 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0253611 s, 413 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0277293 s, 378 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0234147 s, 448 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0211738 s, 495 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0381865 s, 275 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0431456 s, 243 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0442159 s, 237 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0413299 s, 254 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0633694 s, 165 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0812681 s, 129 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0911646 s, 115 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0677276 s, 155 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0539137 s, 194 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.055881 s, 188 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0601847 s, 174 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0575894 s, 182 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0670688 s, 156 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.119225 s, 87.9 MB/s
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0874657 s, 120 MB/s
[root@rsync-client-inotify ~]# ll --full-time /data/{web_data,web}/redhat.sx/*
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:03.244999994 +0800 /data/web_data/redhat.sx/10.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.179999994 +0800 /data/web_data/redhat.sx/1.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.237999994 +0800 /data/web_data/redhat.sx/2.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.285999993 +0800 /data/web_data/redhat.sx/3.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.378999994 +0800 /data/web_data/redhat.sx/4.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.475999995 +0800 /data/web_data/redhat.sx/5.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.627999994 +0800 /data/web_data/redhat.sx/6.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.799999994 +0800 /data/web_data/redhat.sx/7.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.924999994 +0800 /data/web_data/redhat.sx/8.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:03.057999994 +0800 /data/web_data/redhat.sx/9.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:03.121999994 +0800 /data/web/redhat.sx/10.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.149999996 +0800 /data/web/redhat.sx/1.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.207999995 +0800 /data/web/redhat.sx/2.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.260999994 +0800 /data/web/redhat.sx/3.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.330999994 +0800 /data/web/redhat.sx/4.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.424999994 +0800 /data/web/redhat.sx/5.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.542999995 +0800 /data/web/redhat.sx/6.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.730999994 +0800 /data/web/redhat.sx/7.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.862999994 +0800 /data/web/redhat.sx/8.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:02.991999994 +0800 /data/web/redhat.sx/9.html


下面,我们去S1和S2上查看同步后的文件以及时间!
[root@rsync-server-1 ~]# ll --full-time /data/{web_data,web}/redhat.sx/*
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.082999997 +0800 /data/web_data/redhat.sx/10.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.008999997 +0800 /data/web_data/redhat.sx/1.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.237999998 +0800 /data/web_data/redhat.sx/2.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.427999998 +0800 /data/web_data/redhat.sx/3.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.546999997 +0800 /data/web_data/redhat.sx/4.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.652999997 +0800 /data/web_data/redhat.sx/5.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.792999997 +0800 /data/web_data/redhat.sx/6.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.902999998 +0800 /data/web_data/redhat.sx/7.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:04.997999997 +0800 /data/web_data/redhat.sx/8.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:05.124999997 +0800 /data/web_data/redhat.sx/9.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:40.318999989 +0800 /data/web/redhat.sx/10.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:40.152999989 +0800 /data/web/redhat.sx/1.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:40.436999989 +0800 /data/web/redhat.sx/2.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:40.601999989 +0800 /data/web/redhat.sx/3.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:40.736999989 +0800 /data/web/redhat.sx/4.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:39.132999990 +0800 /data/web/redhat.sx/5.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:39.277999989 +0800 /data/web/redhat.sx/6.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:39.432999990 +0800 /data/web/redhat.sx/7.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:39.578999991 +0800 /data/web/redhat.sx/8.html
-rw-r--r--. 1 root root 10485760 2014-06-28 02:54:39.721999990 +0800 /data/web/redhat.sx/9.html


大家可以对比MASTER和S1的文件的时间,不难发现,rsync+inotify架构的整体同步速率还是蛮高的!

OK!
本篇博文就写到这里!
希望能对大家有所帮助!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息