linux中inotify+unison實現數據雙向實時同步

在liux下做文件同步,有如下幾種方式:
1、nfs實現web數據共享
2、rsync +inotify實現web數據同步
3、rsync+sersync更快更節約資源實現web數據同步
4、unison+inotify實現web數據雙向同步
在這裏詳細介紹第四種方案,前幾種各有不足。只有第四種方案支持雙向實時同步,且當其中一臺服務器宕機,也不會影響web的訪問。

1.環境介紹
服務器分別爲:
服務器A:192.168.20.200,同步目錄:/www
服務器B:192.168.20.201,同步目錄:/www

2.安裝unison(A、B都安裝)
首先安裝ocaml,版本至少爲3.07或更高
下載地址:http://caml.inria.fr
例:http://caml.inria.fr/pub/distrib/ocaml-3.10/ocaml-3.10.2.tar.gz
將文件上傳到home目錄,並且切換到home目錄執行如下命令:
tar xvf ocaml-3.10.2.tar.gz

cd ocaml-3.10.2
./configure
make world opt
make install
cd ..

然後安裝unison(A、B都安裝)
下載地址:www.seas.upenn.edu/~bcpierce/unison/
例:http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.32.52/unison-2.32.52.tar.gz

tar xvf unison-2.32.52.tar.gz
cd unison-2.32.52
make UISTYLE=text THREADS=true STATIC=true
cp unison /usr/local/bin
cd ..

注:
UISTYLE=text THREADS=true STATIC=true表示使用命令行方式,加入線程支持以靜態模式編譯

3.安裝inotify
下載地址:http://inotify-tools.sourceforge.net
例:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
cd ..

4.配置雙機ssh信任(除以下方法外,也可以在A中生成密鑰後,把A上的.ssh目錄全SCP到B服務器/root/.ssh,這樣方便些)
以root用戶登陸(A和B機器上都操作)
在服務器A上創建.ssh目錄

mkdir ~/.ssh
chmod 700 ~/.ssh

生成RSA密鑰

ssh-keygen -t rsa
(然後連續三次回車)

添加密鑰到授權密鑰文件中
在192.168.20.200服務器A上操作(22是ssh端口號)

cd ~/.ssh
ssh "-p 22" 192.168.20.200 cat /root/.ssh/id_rsa.pub >> authorized_keys  #小寫p
ssh "-p 22" 192.168.20.201 cat /root/.ssh/id_rsa.pub >> authorized_keys
scp  -P 22 authorized_keys 192.168.20.201:/root/.ssh/  #大寫P
chmod 600 /root/.ssh/authorized_keys

在192.168.20.201服務器B上操作

chmod 600 /root/.ssh/authorized_keys
分別在兩臺機器上執行如下測試(第一次執行時,會要求輸入密碼,以後執行則不需要說明信任成功)
ssh -p 22 192.168.20.200 date
ssh -p 22 192.168.20.201 date

5.添加腳本
在192.168.20.200服務器A上添加腳本:

mkdir /script
vim /script/inotify.sh
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.20.201"
src2="/www/"
dst2="/www/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo ` date +%F %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
fi

在192.168.20.201服務器上添加腳本:

mkdir /script
vim /script/inotify.sh
#/bin/bash
UNISON=`ps -ef |grep -v grep|grep -c inotifywait`
if [ ${UNISON} -lt 1 ]
then
ip2="192.168.20.200"
src2="/www/"
dst2="/www/ "
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line
do
/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log
echo ` date +%F %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).log
done
fi

6.在二臺服務器上修改腳本權限:

chmod a+x /script/inotify.sh

在計劃任務中添加任務(原本在/etc/rc.local下添加開機啓動的,但出問題,腳本並不執行)

crontab -e
* * * * * /bin/sh /script/inotify.sh > /dev/null 2>&1 &

重啓電腦,測試二臺服務器中/www的內容是否能同步
不重啓電腦,手動執行腳本也可以測試

/bin/sh /script/inotify.sh或/script/inotify.sh

在其中一臺/www目錄中添加,或修改,或刪除文件的時候,可以看到腳本狀態,同時另一臺服務器也應該會跟隨操作。

錯誤彙總
在對一個大磁盤進行inotify監聽時,爆出如下錯誤:
Failed to watch /mnt/;
upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches’.

cat一下這個文件,默認值是8192,echo 8192000 > /proc/sys/fs/inotify/max_user_watches即可~

文章翻譯至

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