一.配置rsync
1.安装rsync
# yum install -y rsync
2.写rsync配置
# vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 0      #最大连接数不限制
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
hosts allow = *      #允许所有主机
[test]
path = /home/test/
comment = rsync from everyone
read only = no
list = no
auth users = rsyncuser
secrets file = /etc/rsync.pas
3.创建密码文件
# echo "123456" > /etc/rsync.pas
4.更改密码文件权限
# chmod 600 /etc/rsync.pas
5.启动服务
# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf


二.配置inotify
1.安装inotify
# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
# tar zxf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure --preifx=/usr/local/inotify
# make && make install

2.创建inotify_rsync.sh脚本

#!/bin/bash
SRC='/home/test/'
DES='[email protected]::test'
/usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib ${SRC} |while read DATE TIME DIR FILE
do
        FILECHAGE=${DIR}${FILE}
        /usr/bin/rsync -av --progress --delete  --password-file=/etc/rsync.pas $SRC $DES && echo "At ${TIME} on ${DATE}, file $FILECHAGE was backed up via rsync" >> /var/log/rsyncd.log
done
相关注释如下
/usr/local/inotify/bin/inotifywait -mr
-m      是保持一直监听
-r      是递归查看目录
-q      是打印出事件
-e modify,delete,create,attrib 是指“监听 创建 移动 删除 写入权限”
/usr/bin/rsync -av --progress --delete  --password-file=/etc/rsync.pas
-a      存档模式
-delete      删除多余的文件
--password-file      密码文件
3.添加权限
# chmod +x inotify_rsync.sh
4.执行命令
# ./inotify_rsync.sh &
三.测试
两台实时同步的表示A,B服务器,在A的同步目录创建test1, 在B的同步目录放test2.然后分别访问A,B的同步目录是否有两个目录test1,test2