使用rsync+inotify配置觸發式(實時)遠程同步

系統環境:RHEL5 [ 2.6.18-8.el5xen ]
軟件環境:
    rsync-2.6.8-3.1
    nfs-utils-1.0.9-16.el5
    portmap-4.0-65.2.2.1
    inotify-tools-3.13.tar.gz
        —— http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0
目標功能:
    源主機H1: 192.168.1.11/24
    目標主機H2: 192.168.1.12/24
    將H1主機中的開發數據(/var/devel/目錄),上傳同步至H2主機的/backup/devel/h1/目錄。——當源數據有文件或目錄更新時,即時啓動rsync同步進程。[基於安全性考慮,建議只在內部網絡中使用]
################################################################
    除inotify-tools(需要2.6.13以上內核的inotify功能支持)以外,其他軟件均使用RHEL5系統自帶的rpm包安裝。
一、配置目標主機H2(發佈NFS可寫共享)
shell> mkdir -p /backup/devel/h1/
shell> vi /etc/exports
/backup/devel/h1    192.168.1.11(rw,no_root_squash)
shell> service portmap start
shell> service nfs start
shell> chkconfig portmap on
shell> chkconfig nfs on
  如有必要,可以結合防火牆規則控制訪問權限
shell> iptables -I INPUT -p tcp --dport 111 -j DROP
shell> iptables -I INPUT -p tcp --dport 111 -s 192.168.1.11 -j ACCEPT
shell> iptables -I INPUT -p udp --dport 111 -j DROP
shell> iptables -I INPUT -p udp --dport 111 -s 192.168.1.11 -j ACCEPT

二、配置源主機H1(上傳備份發起端)
    1、安裝inotify-tools工具包
shell> tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
shell> cd /usr/src/inotify-tools-3.13
shell> ./configure
shell> make
shell> make install
—— 可以使用man inotify、man inotifywait、man inotifywatch查看相關手冊頁。
    2、掛載H2發佈的備份目錄
shell> service portmap start
shell> chkconfig portmap on
shell> mkdir -p /media/h2nfsdir/
shell> vi /etc/fstab
192.168.0.12:/backup/devel/h1    /media/h2nfsdir    nfs    defaults,noexec    0 0
shell> mount /media/h2nfsdir
    3、編寫觸發同步腳本
shell> vi /opt/h1-h2_inosync.sh
#!/bin/sh
SRC=/var/devel/
DST=/media/h2nfsdir/
INWT=/usr/local/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
    $RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /opt/h1-h2_inosync.sh
    4、每次開機自動運行監控腳本
shell> echo "/opt/h1-h2_inosync.sh &" >> /etc/rc.local
shell> /opt/h1-h2_inosync.sh &

三、測試實時同步
    在源主機H1上,修改/var/devel/目錄中的內容(如增、刪、改文件,添加、移除目錄等),
    ——同時在目標主機H2上,觀察備份目錄/backup/devel/h1/中內容的變化。

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