Linux unison 效率,Linux下inotify+unison双向同步环境部署
在Linux下使用unison+inotify實(shí)現(xiàn)web數(shù)據(jù)雙向同步,當(dāng)其中一臺(tái)服務(wù)器宕機(jī),也不會(huì)影響web的訪問(wèn)。(ps:之前一直喜歡nfs,配置非常簡(jiǎn)單,但是其有個(gè)致命的缺點(diǎn)就是其中一臺(tái)web服務(wù)掛掉之后,會(huì)直接導(dǎo)致web頁(yè)面無(wú)法訪問(wèn))。
環(huán)境部署,有如下兩臺(tái)服務(wù)器需要做雙向同步
192.168.10.1是server1,
192.168.10.2是server2
第一步,保證兩臺(tái)服務(wù)器之間可以通過(guò)ssh無(wú)密碼訪問(wèn),操作如下(這里以root用戶為例):
分別在server1和server2下,創(chuàng)建秘鑰
mkdir ~/.ssh
chmod 700 ~/.ssh
生成RSA密鑰
ssh-keygen -t rsa
(然后連續(xù)三次回車)
添加密鑰到授權(quán)密鑰文件中
cd ~/.ssh
ssh "-p 22" 192.168.10.1 cat /root/.ssh/id_rsa.pub >> authorized_keys? #小寫(xiě)p
ssh "-p 22" 192.168.10.2 cat /root/.ssh/id_rsa.pub >> authorized_keys
scp? -P 22 authorized_keys 192.168.10.2:/root/.ssh/? #大寫(xiě)P
chmod 600 /root/.ssh/authorized_keys
在服務(wù)器server2上操作
chmod 600 /root/.ssh/authorized_keys
分別在兩臺(tái)機(jī)器上執(zhí)行如下測(cè)試
ssh -p 22 192.168.10.1 date
ssh -p 22 192.168.10.2 date
至此用戶授權(quán)完成。
第二步,軟件安裝,server1和server2都得安裝
安裝unison
首先安裝ocaml,版本至少為3.07或更高
下載地址:http://caml.inria.fr/pub/distrib/ocaml-3.10/
tar xf ocaml-3.10.2.tar.gz
cd ocaml-3.10.2
./configure
make world opt
make install
cd ..
安裝unison
下載地址:http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.13.16/
tar xvf unison-2.13.16.tar.gz
cd unison-2.13.16
make UISTYLE=text THREADS=true STATIC=true
cp unison /usr/local/bin
cd ..
安裝inotify
下載地址:http://inotify-tools.sourceforge.net
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
cd ..
到此所需的軟件都已安裝完畢,可以在server1服務(wù)器上執(zhí)行這個(gè)命令,來(lái)查看兩臺(tái)服務(wù)器之間是否可以同步文件,unison -batch /home/server1/ ssh://192.168.10.2//home/server2 ,如果這時(shí)候抱如下錯(cuò)誤:
/usr/local/bin/inotifywait: error while loading shared libraries: libinotify
可以執(zhí)行下這個(gè)命令:
ln -sv /usr/local/lib/libinotify* /usr/lib/
執(zhí)行成功后,看目錄下的文件是否同步。
第三步,創(chuàng)建.sh腳本來(lái)執(zhí)行同步
1)server1上創(chuàng)建腳本/root/inotify.sh(chmod a+x /root/inotify.sh):
#/bin/bash
ip2="192.168.10.2"
src2="/home/server1/"
dst2="/home/server2/"
/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.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
1)server2上創(chuàng)建腳本/root/inotify.sh(chmod a+x /root/inotify.sh):
#/bin/bash
ip1="192.168.10.1"
src1="/home/server2/"
dst1="/home/server1/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do
/usr/local/bin/unison -batch $src1 ssh://$ip1/$dst1
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
最后分別在server1和server2上執(zhí)行上面兩個(gè)腳本,這樣兩臺(tái)服務(wù)器的目錄會(huì)保持相互實(shí)時(shí)同步了!!!
總結(jié)
以上是生活随笔為你收集整理的Linux unison 效率,Linux下inotify+unison双向同步环境部署的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: parallelStream数据丢失问题
- 下一篇: 【一文学Linux系统基础操作】