实验环境:

windows8 64位+vmware workstation11+centos6.6 64位

网络采用host-only模式

server端:node1.test.com 192.168.245.11

client端:node2.test.com 192.168.245.12

关闭防火墙和selinux

#service iptables stop

#setenforce 0

linux内核版本要在2.6.13之上

查看linux内核版本:

#uname -r

2.6.32-504.el6.x86_64

查看系统是否支持inotify:

#ls -l /proc/sys/fs/inotify/

如果出现

-rw-r--r-- 1 root root 0 Mar  4 11:10 max_queued_events
-rw-r--r-- 1 root root 0 Mar  4 11:10 max_user_instances
-rw-r--r-- 1 root root 0 Mar  4 11:10 max_user_watches

表示系统支持inotify

在server和client安装rsync

#yum -y install rsync

配置client端:

#vi /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections =10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web1]
path = /data/www1/
comment = web file
ignore errors
read only = no
write only = no
hosts allow = 192.168.245.0/255.255.255.0
hosts deny = *
list = false
auth users = www1
secrets file = /etc/www1.pwd

保存退出

启动rsync

#rsync --daemon --config=/etc/rsyncd.conf

#vi /etc/www1.pwd

www1:123456  //保存退出并设置权限600(必须)

#chmod 600 /etc/www1.pwd

 

在server端配置密码文件:

#vi /etc/www1.pwd

123456       //保存退出并设置权限600(必须)

接下来安装inotify-tools:

#tar -xf inotify-tools-3.14.tar.gz

#cd inotify-tools-3.14

#./configure --prefix=/usr/local/inotify-tools

#make && make install

写一个监控脚本:

#vi inotifyrsync.sh

#!/bin/bash
host1=192.168.245.12
src=/data/www/
des1="web1"
user1="www1"
/usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f'  -e modify,delete,create,attrib \
${src} \
| while read  file
do
rsync -vzrtopg --delete --progress ${src} ${user1}@${host1}::${des1} --password-file=/etc/www1.pwd &&
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
done

保存退出

#chmod a+x inotifyrsync.sh

在client端创建目录:

#mkdir /data/www1/ -pv

在server端创建目录:

#mkdir /data/www -pv

运行脚本

#./inotifyrsync.sh &

就会看到server端的目录/data/www/下的文件会自动同步到client端的/data/www1上。

至此,rsync+inotify配置完毕。