您的位置:首页 > 其它

rsync+inotify-tools+ssh实现触发式远程实时同步

2012-04-28 18:34 966 查看
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 )

本文出自 “对月当歌” 博客,请务必保留此出处http://zhang101527.blog.51cto.com/3639673/846852
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: