拓扑图:

wKiom1Sr5uLwGUHEAACzCTa2k5A028.jpg

实验中我先利用rsync在192.168.150.151下载192.168.150.150的源数据到本地,然后在192.168.150.150利用rsync和inotify实时同步数据到192.168.150.151.


rsync和inotify简介:

   rsync是类unix系统下的数据镜像备份工具——remote sync。一款快速增量备份工具,Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步。

rsync [选项]源位置   目标位置

-r:递归模式,包含目录及子目录中的所有文件

-l:复制符号链接文件

-v:显示同步过程

-a:归档模式,保留文件的权限,属性等信息

-z:在传输过程中压缩

-p:保留权限标记

-t:保留文件的时间标记

-H:保留硬链接

-A:保留ACL信息

--delete:删除目标位置有而原始位置没有的文件


   Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。学习如何将inotify 集成到您的应用程序中,并发现一组可用来进一步自动化系统治理的命令行工具。



实验环境:

系统:centos-6.6.x86_64 

kernel:2.6.32

rsync-3.0.6-12.el6.x86_64

inotify-tools-3.14

httpd-2.2.15-39.el6.centos.x86_64

ssh

[root@centos-server ~]# yum install rsync#在192.168.150.151安装rsync
[root@centos-server ~]# cat /etc/httpd/conf/httpd.conf |grep ^User
User apache#这是192.168.150.151的apache的用户
在192.168.150.150设置ACL
[root@centos-server ~]# setfacl -m default:user:apache:rx /var/www/html  #确保被数据被同步后,在192.168.150.151上有的apache有执行权限,也可以通过other设置,但不安全。
root@centos-server ~]# getfacl /var/www/html/
getfacl: Removing leading '/' from absolute path names
# file: var/www/html/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x    #这让ACL显得多余
default:user::rwx
default:user:apache:r-x
default:group::r-x
default:mask::r-x
default:other::r-x
[root@centos-server ~]# echo "<h1>This is 192.168.150.151</h1>" >> /var/www/html/index.html


在192.168.150.150rsync数据:

wKiom1Sr7JXBVMp_AAKg4uJlkl4526.jpg

wKioL1Sr7VWzUCxNAABmaMsoCOI953.jpg

在192.168.150.150生成证书,并设置rsync-inotify

[root@centos-server ~]# yum install rsync make gcc  gcc-c++
[root@centos-server ~]# ssh-keygen -t rsa    #利用ssh证书实现无密码登录
[root@centos-server ~]# ssh-copy-id root@192.168.150.151
[root@centos-server ~]# ssh 192.168.150.151    #尝试登录
Last login: Wed Dec 24 09:36:27 2014
[root@centos-server ~]# exit
logout
Connection to 192.168.150.151 closed.
[root@centos-server ~]# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@centos-server ~]# tar xf inotify-tools-3.13.tar.gz 
[root@centos-server ~]# cd inotify-tools-3.13
[root@centos-server inotify-tools-3.13]# ./configure && make && make install  #安装inotify
[root@centos-server ~]# cat autorsync.sh         #编写脚本,inotify检查到有数据修改,创建,删除,移动就同步
#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,delete,move /var/www/html/"
rsync_cmd="rsync -aHz --delete /var/www/html/ 192.168.150.151:/var/www/html/"
$inotify_cmd | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l )  -le 0 ];then
$rsync_cmd
fi
done
[root@centos-server ~]# for i in `seq 10 20`;do touch /var/www/html/$i; done

查看192.168.150.151

wKioL1Sr8HbDfSHfAABb8BJ2MkE882.jpg