Rsync+Inotify-client 实现实时同步

        在前面的博文实践记录之-Rsync镜像备份介绍了镜像备份工具Rsync的安装和使用.但在大数据时代,rsync的不足之处也暴露出来.

        首先.rsync本身实现不了实时备份.靠系统的crontab实现的话也受限于1分钟.因此这就导致了服务端和客户端数据可能出现不一致,更无法在应用故障时做到数据的完全恢复。其次,rsync在备份时,要扫描所有文件,这样效率就特别低,特别在数据量很大的时候.

        不过,结合Inotify可以很好的解决Rsync在这方面的缺陷.基本实现原理是这样:通过使用shell脚本,获取inotifywait输出,然后借助inotifywait的监控文件或目录实时变化去通知rsync做相应的推送或者拉取操作!

        Inotify-client的安装和使用查看另一篇博文实践记录之-系统监控工具Inotify-tool

        下面实践下Rsync+Inotify-client 实现实时同步.

        实验环境:

       Master:  Rsync客户端+Inotify服务 IP:192.168.1.33   hostname:RsyncClient-Inotify

       Slave: Rsync服务端 IP:192.168.1.34   hostname:RsyncServer

       本实验只设置一台Slave ,现实操作可以有多台Slave ,更有保障些;

一、在 SLAVE 机器上部署 rsync 服务端程序

1.安装rsync:  

[root@RsyncClient-Inotify ~]# yum -y install rsync

2.添加rsync配置文件

[root@RsyncServer ~]# vi /etc/rsyncd.conf

   内容如下:

uid = root
gid = root
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = *
auth users = rsync
secrets file = /etc/rsync.password
[snbo]
path = /test

3、创建 rsync.password 作为用户密码文件

[root@RsyncServer ~]# touch /etc/rsync.password
[root@RsyncServer ~]# chmod 600 /etc/rsync.password 
[root@RsyncServer ~]# vi /etc/rsync.password 
[root@RsyncServer ~]# cat /etc/rsync.password 
rsync:123456

        上面的rsync服务的配置文件,表明允许192.168.1.0网段的主机访问,rsync同步模块名为[snbo],将同步过来的文件放入对应path指定的目录/test,如果有多台目标服务器,则每一台都需要进行类似的rsync服务端配置,上面的uid和gid需要换成你服务器的相应的同步用户。

4、创建同步目录

[root@RsyncServer ~]# mkdir /test
[root@RsyncServer ~]# chown root:root /test

5、以守护进程方式启动rsync服务

[root@RsyncServer ~]# rsync --daemon

6、查看rsync服务状态

[root@RsyncServer ~]# lsof -i:873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   1087 root    3u  IPv4   9007      0t0  TCP *:rsync (LISTEN)
rsync   1087 root    5u  IPv6   9008      0t0  TCP *:rsync (LISTEN)

7、为rsync添加开机自启动

在/etc/rc.local文件里添加一行以下内容:

/usr/bin/rsync --daemon  --config=/etc/rsyncd.conf

补充:

      重启rsync的组合命令 :

  pkill rsync  #关闭rsync服务
  rsync --daemon #启动rsync服务
  ps -ef | grep rsync   或 lsof -i:873  #检查是否启动

二、在Master 上配置rsync客户端

      

1、安装Rsync并配置相关权限

在Master上配置rsync客户端相关权限认证(rsync.password):

[root@RsyncClient-Inotify ~]# yum -y install rsync
[root@RsyncClient-Inotify ~]# vi /etc/rsync.password
[root@RsyncClient-Inotify ~]# chmod 600 /etc/rsync.password
[root@RsyncClient-Inotify ~]# cat /etc/rsync.password 
123456

2、Master上手动测试rsync的同步情况

要确保这一步能成功执行,否则后面的Inotify配置好了也同步不了.

1)创建待同步数据

[root@RsyncClient-Inotify ~]# mkdir /test
[root@RsyncClient-Inotify ~]# touch /test/{a,b,c,d}
[root@RsyncClient-Inotify ~]# ll /test/
total 0
-rw-r--r-- 1 root root 0 Jul 12 06:50 a
-rw-r--r-- 1 root root 0 Jul 12 06:50 b
-rw-r--r-- 1 root root 0 Jul 12 06:50 c
-rw-r--r-- 1 root root 0 Jul 12 06:50 d

2)执行同步命令

[root@RsyncClient-Inotify ~]# rsync -avzP /test  [email protected]::snbo --password-file=/etc/rsync.password 
sending incremental file list
test/
test/a
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
test/b
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/5)
test/c
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/5)
test/d
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/5)
sent 213 bytes  received 88 bytes  46.31 bytes/sec
total size is 0  speedup is 0.00

查看RsyncServer的同步目录:

[root@RsyncServer ~]# ll /test/
total 0
-rw-r--r-- 1 root root 0 Jul 12 06:50 a
-rw-r--r-- 1 root root 0 Jul 12 06:50 b
-rw-r--r-- 1 root root 0 Jul 12 06:50 c
-rw-r--r-- 1 root root 0 Jul 12 06:50 d

三、在M1 上配置inotify

1、查看 M1的 内核是否支持inotify

[root@RsyncClient-Inotify ~]# uname -r
2.6.32-358.el6.x86_64
[root@RsyncClient-Inotify ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_queued_events
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_user_instances
-rw-r--r-- 1 root root 0 Jul 12 07:12 max_user_watches

2、安装inotify-tool

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

3、查看inotify提供的工具

[root@RsyncClient-Inotify ~]# ll /usr/local/bin/
total 80
-rwxr-xr-x 1 root root 38582 Jul  9 15:25 inotifywait
-rwxr-xr-x 1 root root 40353 Jul  9 15:25 inotifywatch

 Inotify-client的详细使用查看另一篇博文实践记录之-系统监控工具Inotify-tool

四、rsync和inotify-tools做结合

通过脚本,把rsync和inotify做一个结合.实现rsync的实时备份!

现贴出脚本如下:

[root@RsyncClient-Inotify ~]# cat auto_rsync.sh 
#!/bin/bash
dir='/test/'
des=snbo
host=192.168.1.34
user=rsync
rsyncall='/usr/bin/rsync -rpgovz --delete --progress'
/usr/local/bin/inotifywait -mrq --timefmt '%Y%m%d %H:%M:%S' --format '%T %w %w%f %e' -e modify,delete,create,attrib $dir | while read DATE TIME DIR FILE EVENT;
do
$rsyncall $dir $user@$host::$des --password-file=/etc/rsync.password && echo $DATE $TIME $FILE was Rsynced:$EVENT >>/var/log/rsync-snbo.log
done

将auto_rsync.sh加入开机启动:

[root@RsyncClient-Inotify ~]# mv auto_rsync.sh /usr/sbin/
[root@RsyncClient-Inotify ~]# vi /etc/rc.local 
加入下面一行
sh /usr/sbin/auto_rsync.sh  >>/var/log/rsync.log &

测试:

两台主机提前同步过时间,好作对比.

网络同步时间命令:ntpdate cn.pool.ntp.org && hwclock -w
创建文件:
[root@RsyncClient-Inotify ~]# touch /test/{1,2,3,4}
[root@RsyncClient-Inotify ~]# ll --full-time /test/
total 0
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 1
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 2
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 3
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.023852271 +0800 4
查看RsyncServer目录文件变化
[root@RsyncServer ~]# ll --full-time /test/
total 0
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 1
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 2
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.254956293 +0800 3
-rw-r--r-- 1 root root 0 2014-07-16 11:56:10.294956574 +0800 4

对比文件的时间,可见同步的效率很高.

测试较大文件同步:

在/test目录下创建一500M大小的文件

[root@RsyncClient-Inotify ~]# dd if=/dev/zero of=/test/file count=1000000
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 10.5061 s, 48.7 MB/s
[root@RsyncClient-Inotify ~]# ll --full-time /test/
total 500004
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 1
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 2
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 3
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.023852271 +0800 4
-rw-r--r-- 1 root root 512000000 2014-07-16 12:05:26.566868352 +0800 file
[root@RsyncServer ~]# ll --full-time /test/
total 500004
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 1
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 2
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.254956293 +0800 3
-rw-r--r-- 1 root root         0 2014-07-16 11:56:10.294956574 +0800 4
-rw-r--r-- 1 root root 512000000 2014-07-16 12:06:05.143956010 +0800 file

同步时间为30几秒.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章