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

系统环境:CentOS release 6.3 x86_64安装软件:rsync3.xinotify-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 variableshost1=192.168.1.53src=/var/www/htmldst=wwwuser=webuserrsync_passfile=/etc/rsync.passwordinotify_home=/usr/local/inotify-tools/LogFile=/tmp/inotify_sync.log#judgeif [ ! -e “$src” ] \|| [ ! -e “$rsync_passfile” ] \|| [ ! -e “$inotify_home/bin/inotifywait” ] \|| [ ! -e “/usr/bin/rsync” ];thenecho “Check File and Folder”exit 9fi#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 filedocd $src rsync -aruz -R –delete ./ –timeout=100 $user@$host1::$dst –password-file=$rsync_passfile $LogFile 2 1doneexit 0[root@cqb html]# chmod +x rsync.shecho “/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,文件被 writeIN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等IN_CLOSE_WRITE,可写文件被 closeIN_CLOSE_NOWRITE,不可写文件被 closeIN_OPEN,文件被 openIN_MOVED_FROM,文件被移走,如 mvIN_MOVED_TO,文件被移来,如 mv、cpIN_CREATE,创建新文件IN_DELETE,文件被删除,如 rmIN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己IN_UNMOUNT,宿主文件系统被 umountIN_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 = rootgid = 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/ 本文章大部分内容收集于网上,如有问题请联系,51cto博客 、个人网站 痛苦留给的一切,请细加回味!苦难一经过去,苦难就变为甘美。

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

相关文章:

你感兴趣的文章:

标签云: