具体大家可以参照http://www.ibm.com/developerworks/cn/linux/l-ubuntu-inotify/index.html来进行学习。
接下面我们来开始进行rsync与inotify的安装、配置、测试。
下面是2个服务器的结构,分别为主机名、ip、状态、内核、位数、同步的目录,并将2台服务器均是redhat5.4发行版本。

一、主服务器(server端,我这里是nginx)
其中主服务器需要安装rsync与inotify,主服务器作为server,向备份服务器client传输文件
1、安装rsync
[root@nginx ~]# cd /usr/src/ [root@nginx src]# ll total 16 drwxr-xr-x 2 root root 4096 Jan 26 2010 debug drwxr-xr-x 2 root root 4096 Jan 26 2010 kernels [root@nginx src]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz [root@nginx src]# tar zxvf rsync-3.0.9.tar.gz [root@nginx src]# cd rsync-3.0.9 [root@nginx rsync-3.0.9]# ./configure --prefix=/usr/local/rsync [root@nginx rsync-3.0.9]# make [root@nginx rsync-3.0.9]# make install
2、建立密码认证文件
[root@nginx rsync-3.0.9]# cd /usr/local/rsync/
[root@nginx rsync]# echo "rsync-pwd" >/usr/local/rsync/rsync.passwd
其中rsync-pwd可以自己设置密码,rsync.passwd名字也可以自己设置
[root@nginx rsync]# chmod 600 rsync.passwd
无论是为了安全,还是为了避免出现以下错误,密码文件都需要给600权限
password file must not be other-accessible
continuing without password file
3、安装inotify
[root@nginx rsync]# cd /usr/src/ [root@nginx src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz [root@nginx src]# cd inotify-tools-3.14 [root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@nginx inotify-tools-3.14]# make [root@nginx inotify-tools-3.14]# make install
4、创建rsync复制脚本
此项功能主要是将server端的目录/tmp里的内容,如果修改了(无论是添加、修改、删除文件)能够通过inotify监控到,并通过rsync实时的同步给client的/tmp里,下面是通过shell脚本实现的。
#!/bin/bash
host=192.168.10.221
src=/tmp/
des=web
user=webuser
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
注意:经过1楼的提示,我发现如果把rsync.log的放到tmp(备份的目录)或发送一直复制的问题,所以建议各位吧rsync的日志放到其他的目录下(非备份目录)。








