1安装

[root@server ~]# wget ftp://ftp.samba.org/pub/rsync/rsync-3.0.8.tar.gz

[root@server ~]# tar xzvf rsync-3.0.8.tar.gz

[root@server ~]# cd rsync-3.0.8

[root@server ~]# ./configure

[root@server ~]# make

[root@server ~]# make install

 

[root@server~]# 

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

 

[root@server ~]# tar xzvf inotify-tools-3.14.tar.gz

[root@server ~]# cd inotify-tools-3.14

[root@server ~]# ./configure

[root@server ~]# make

[root@server ~]# make install

 

 

 

2.配置ssh key信任

 

服务器

[root@server ~]# useradd -m rsync

[root@server ~]# passwd rsync

 

客户机

[root@client ~]# useradd -m rsync

[root@client ~]# passwd rsync

 

服务器

[root@server ~]# su - rsync

[rsync@server ~]$ ssh-keygen -t rsa (所有选项回车,使用默认设置)

[rsync@server ~]$ ssh-copy-id -i .ssh/id_rsa.pub rsync@172.16.10.253

 

 

分别重启sshd服务

[root@server ~]# /etc/init.d/sshd restart

[root@client ~]# /etc/init.d/sshd restart

 

3.添加同步脚本

 

[root@Server ~]# vim inotify_rsync.sh

#!/bin/sh

SRC=/home/rsync/test/

DST=rsync@172.16.10.253:/home/rsync/test

INWT=/usr/local/bin/inotifywait

RSYNC=/usr/local/bin/rsync

$INWT -mrq -e create,move,delete,modify $SRC | while read D E F;do

            rsync -aHqzt --delete $SRC $DST

        done

[root@Server ~]# chmod +x inotify_rsync.sh

[root@Server ~]# cp inotify_rsync.sh /home/rsync/

[root@Server ~]# su - rsync

[rsync@server ~]$ ./inotify_rsync.sh &

[rsync@server ~]$ mkdir test

测试

[rsync@server ~]$ cd test

[rsync@server text]$ touch a.txt

验证

[rsync@client ~]$ cd test

[rsync@client test]$ ls

a.txt

 

(这个脚本里在rsync用户里面执行的,可以在root下试下,设为开机启动的话:

echo "/root/inotify_rsync.sh &" >> /etc/rc.local )