手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux实时同步----Rsync+Inotify-Tools

[日期:2014-06-28] 来源:Linux社区  作者:nolinux [字体:大 中 小]

二、在M1 上配置rsync客户端

1、配置rsync权限

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

[root@rsync-client-inotify /]# echo "111111">/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::www/ --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::www/ --password-file=/etc/rsync.password

[root@rsync-client-inotify ~]# rsync -avzP /data/web_data/redhat.sx  rsync_backup@172.16.100.2::web_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!

        本篇博文就写到这里!

        希望能对大家有所帮助!

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103806.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/1038.htm

linux
相关资讯       rsync 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款