rsync+inotify-tools+ssh 實現觸發式遠程實時同步

1安裝

[root@server1 soft]# wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz

[root@server1 soft]# tar zxvf rsync-3.0.9.tar.gz

[root@server1 soft]# cd rsync-3.0.9

[root@server1 rsync-3.0.9]# ./configure

[root@server1 rsync-3.0.9]# make && make install

 
[root@server soft]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

[root@server1 soft]# tar zxvf inotify-tools-3.14.tar.gz

[root@server1 soft]# cd inotify-tools-3.14

[root@server1 inotify-tools-3.14]# ./configure

[root@server1 inotify-tools-3.14]# make && make install

 
[root@server1 ~]# yum -y install rsync

[root@server2 ~]# yum -y install rsync

 
2.配置ssh key信任

server1

[root@server1 ~]# useradd -m rsync

[root@server1 ~]# passwd rsync

 
server2

[root@server2 ~]# useradd -m rsync

[root@server2 ~]# passwd rsync

 
server1(創建key並配置server2的信任)

[root@server1 ~]# su - rsync

[rsync@server1 ~]$ ssh-keygen -t rsa (所有選項回車,使用默認設置)

[rsync@server1 ~]$ scp ~/.ssh/id_rsa.pub [email protected]:/home/rsync/

 

server2(將公鑰添加到authorized_keys文件中)

[root@server2 ~]# su - rsync

[rsync@server2 ~]$ mkdir .ssh

[rsync@server2 ~]$ chmod 700 .ssh/

[rsync@server2 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys

server2(創建key並配置對server1的信任)

[rsync@server2 ~]$ ssh-keygen -t rsa

[rsync@server2 ~]$ scp ~/.ssh/id_rsa.pub [email protected]:/home/rsync/

 
server1(將公鑰添加到authorized_keys文件中)

[rsync@server1 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys

 
分別重啓sshd服務

[root@server1 ~]# service sshd restart

[root@server2 ~]# service sshd restart 

 
3.添加同步腳本

[root@server1 ~]# vi inotify_rsync.sh

#!/bin/sh

SRC=/home/rsync/test/

[email protected]:/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@server1 ~]# chmod +x inotify_rsync.sh

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

[root@server1 ~]# su - rsync

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

[1] 15305

[rsync@server1 ~]$ Couldn't watch /home/rsync/test/: No such file or directory

 
[1]+  Done                    ./inotify_rsync.sh

[rsync@server1 ~]$ mkdir test

[rsync@server2 ~]$ mkdir test

測試

[rsync@server1 ~]$ cd test/

[rsync@server1 test]$ touch yhl.txt

[rsync@server1 test]$ Address 172.16.19.89 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

 
 
驗證

[rsync@server2 ~]$ cd test/

[rsync@server2 test]$ ls

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章