使用rsync+inotify配置触发式(实时)远程同步

系统环境:
CentOS release 6.3  x86_64
安装软件:
rsync3.x
inotify-tools
条件:
需要实时同步的两台主机: 192.168.1.51 192.168.1.53
同步的网站目录: /var/www/html/
1、在文件服务器端的配置
下载安装inotify-tools:
[root@data ~]# cd /usr/src/
[root@data src]# wget http://iweb.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
[root@data src]# tar zxvf inotify-tools-3.14.tar.gz
[root@data src]# cd inotify-tools-3.14
[root@data inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools 
检查rsync是否安装 :
检查命令: rpm -qa | grep rsync  如果有输出就表示rsync已经安装,不在需要安装,如果没输出则表示没有安装,可以通过源码编译安装或者yum安装,yum 安装的方法 :
[root@cqb html]# yum install -y rsync
[root@cqb inotify-tools-3.13]# cd /var/www/html/
[root@cqb html]# vim rsync.sh    
文件服务器安装好rsync后无需配置,也不需要启动。
同步脚本如下:
#!/bin/bash
######################################
## system:CentOS 6.x 64bit                    ##
## Author:wolf                                         ##
## QQ:1250052800                                         ##
## E-mail:1250052800@qq.com ##
## Date:                    21:40 2012-11-28    ##
## Version: 1.1                                         ##
## rsync+inotify sync script                ##
######################################
 
#define variables
host1=192.168.1.53
src=/var/www/html
dst=www
user=webuser
rsync_passfile=/etc/rsync.password
inotify_home=/usr/local/inotify-tools/
LogFile=/tmp/inotify_sync.log
 
#judge
if [ ! -e "$src" ] \
|| [ ! -e "$rsync_passfile" ] \
|| [ ! -e "$inotify_home/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
        echo "Check File and Folder"
        exit 9
fi
 
#sync
$inotify_home/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
        cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host1::$dst --password-file=$rsync_passfile >$LogFile 2>&1
done
 
exit 0
[root@cqb html]# chmod +x rsync.sh
echo "/var/www/html/rsync.sh &" /etc/rc.local   将此脚本写入到/etc/rc.local 让系统自动加载即可.
脚本相关注解:
    -m 是保持一直监听
    -r 是递归查看目录
    -q 是打印出事件~
    -e create,move,delete,modify
    监听 创建 移动 删除 写入 事件
    rsync -aHqzt $SRC $DST
    -a 存档模式
    -H 保存硬连接
    -q 制止非错误信息
    -z 压缩文件数据在传输
    -t 维护修改时间
    -delete 删除于多余文件
当要排出同步某个目录时,为rsync添加--exculde=PATTERN参数,注意,路径是相对路径。详细查看man rsync
当要排除都某个目录的事件监控的处理时,为inotifywait添加--exclude或--excludei参数。详细查看man inotifywait
另:
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' \
-e modify,delete,create,attrib \
${src} \
上面的命令返回的值类似于:
10/03/09 15:31 /wwwpic/1
这3个返回值做为参数传给read,关于此处,有人是这样写的:
inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
细化了返回值。
说明: 当文件系统发现指定目录下有如上的条件的时候就触发相应的指令,是一种主动告之的而非我用循环比较目录下的文件的异动,该程序
在运行时,更改目录内的文件时系统内核会发送一个信号,这个信号会触发运行rsync命令,这时会同步源目录和目标目录。
--timefmt:指定输出时的输出格式
   --format:  '%T %w%f'指定输出的格式
二.关于inotify介绍
Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。
[root@cqb html]# vim /etc/rsync.password  创建密码文件
12345    这里只要输入密码就可以
[root@cqb html]# chmod 600 /etc/rsync.password   给密码文件权限为属主可读可写(必须的)
启动脚本 ./rsync.sh &
2、客户端的配置 
[root@data ~]# vim /etc/rsyncd.conf     创建rsync住配置文件
uid = nobody                         ##全局配置开始,指文件传输时模块进程的uid 
gid = nobody                         ##同上gid 
use chroot = no                    ##是否让进程离开工作目录 
max connections = 4            ##最大并发数 
syslog facility = local5 ##记录日志的facility 
pid file = /var/run/rsyncd.pid    ##pid位置 
[www]                                        ##模块配置开始 
path = /var/www/html            ##需要备份的目录,必须指定,                 comment = whole ftp area    ##注释 
read only = no                     ##客户端是否只读 
write only = no                    ##是否只能写 
hosts allow = *                    ##允许同步主机 
hosts deny = 192.168.1.0/24 ##禁止访问的主机 
list = yes                             ##是否允许列出所有模块 
uid = root                                
gid = root 
auth users = webuser             ##可以连接该模块的user 
secrets file = /etc/rsync.password    ##密码文件在哪,需要自己建立
[root@data ~]# rsync --daemon    启动主进程
[root@cqb html]# vim /etc/rsync.password    创建密码文件
webuser:12345
[root@cqb html]# chmod 600 /etc/rsync.password     给密码文件权限为属主可读可写(必须的)
3、测试
查看是否同步
[root@cqb html]# cp /etc/passwd /var/www/html/  


本文转自1594cqb 51CTO博客,原文链接:http://blog.51cto.com/wolfchen/1085142,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值