unison+inotify實現數據實時雙向同步
1、unison簡介
Unison是windows和unix平臺下都可以使用的文件同步工具,它能使兩個文件夾(本地或網絡上的)保持內容的一致,也支持經由過程SSH、RSH和Socket同步支持雙向同步。Unison有文字界面和圖形界面,這裏只介紹如何在文字界面下使用. unison擁有其它一些同步工具或文件系統的相同特性,也有自己的特點,可以跨平臺使用,對內核和用戶權限沒有特別要求,unison是雙向的,它能自動處理兩分拷貝中更新沒有沖突的部分,有沖突的部分將會顯示出來讓用戶選擇更新策略;只要是能連通的兩臺主機,就可以運行unison,可以直接使用
unison可以作為兩臺以上web服務器的雙向同步工具,當其中一臺web服務器宕機時,另一臺web服務器可以正常工作。
2、環境準備(本次操作系統為centos7)
(1)準備兩臺web服務器
web1:192.168.115.120 需要同步的目錄為/data/www
web2:192.168.115.150 需要同步的目錄為/data/www
註意:Unison對版本要求很高,進行同步的兩臺主機需要相同版本的unison
3、編譯安裝相關軟件(兩臺機器都需安裝)
(1)安裝ocaml,版本至少為3.07或更高
[root@web1 ~]# yum -y install gcc gcc-c++
[root@web1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-4.06/ocaml-4.06.1.tar.gz
[root@web1 ~]# tar -xf ocaml-4.06.1.tar.gz
[root@web1 ~]# cd ocaml-4.06.1
[root@web1 ocaml-4.06.1]# ./configure
[root@web1 ocaml-4.06.1]# make world opt
[root@web1 ocaml-4.06.1]# make install
(2)安裝unison,兩臺機器要求版本一樣
[root@web1 ~]# wget https://github.com/bcpierce00/unison/archive/v2.51.2.tar.gz
[root@web1 ~]# tar -xf v2.51.2.tar.gz
[root@web1 ~]# cd unison-2.51.2/
[root@web1 unison-2.51.2]# cd src/
[root@web1 src]# make UISTYLE=text THREADS=true
[root@web1 src]# cp unison /usr/local/bin/
[root@web1 src]# unison -version
unison version 2.51.2 (ocaml 4.06.1)
有版本信息出現,則安裝成功
特別註意:這個軟件不需要執行./configure, make的時候後面的選項也比較特殊,千萬不要弄錯了。最後也不需要執行make install
(3)安裝inotify
[root@web1 ~]# wget https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@web1 ~]# tar -xf inotify-tools-3.14.tar.gz
[root@web1 ~]# cd inotify-tools-3.14
[root@web1 inotify-tools-3.14]# ./configure && make && make install
4、配置雙機ssh信任
web1上生成密鑰,不輸入私鑰密碼並上傳到web2服務器
[root@web1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
32:63:9f:92:7c:fc:7b:9d:8d:70:bc:f0:f1:a3:16:d1 root@web1
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| . E |
| = S .. |
| o B . o.+ |
| + = *.B |
| o . ..*.o|
| oo... .|
+-----------------+
[root@web1 ~]# ssh-copy-id 192.168.115.150
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.115.150'"
and check to make sure that only the key(s) you wanted were added.
web2上生成密鑰,不輸入私鑰密碼並上傳到web1服務器
[root@web2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
fd:9a:e2:75:39:03:27:d0:01:e3:73:ca:2c:d3:51:f3 root@web2
The key's randomart image is:
+--[ RSA 2048]----+
| o.+ |
| . + + |
| = o E |
| + B |
| o S + . |
| o = . |
| . * |
| .. + o |
| ...o |
+-----------------+
[root@web2 ~]# ssh-copy-id 192.168.115.120
The authenticity of host '192.168.115.120 (192.168.115.120)' can't be established.
ECDSA key fingerprint is c7:79:b3:87:3f:cf:d9:18:34:e7:38:bd:2d:8c:db:36.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.115.120'"
and check to make sure that only the key(s) you wanted were added.
在2臺機器上分別ssh對方IP,能無密碼登錄則表示配置成功。
5、unison的使用
unison的用法非常靈活和簡單,可以通過如下三種方式調用unison。
(1)第一種方式:”unison profile_name
[options]”
unison默認會讀取~/.unison目錄下的配置文件”profile_name.prf”。
註意,在這種方式下,命令行中並沒有指出要進行同步的兩個地址,所以,此種調用unison的方式必須在配置文件profile_name.prf中通過相關的root指令設置同步的路徑和同步的參數,如:
#Unison preferences file
root = /tmp/test
root = ssh://[email protected]//tmp/test/
#force =
#ignore =
batch = true
(2)第二種方式:”unison profile root1
root2 [options]”
root1、root2分別表示要執行同步的兩個路徑。這兩個路徑可以是本地目錄路徑,也可以是遠程服務器的路徑,如ssh://username@//tmp/test 。由於同步的路徑已經在命令行指定了,所以這裏無需在profile.prf配置文件中進行root指令的相關設置。
(3)第三種方式:”unison root1 root2
[options]”
這種方式相當於執行”unison default root1 root2”命令,即unison默認讀取default.prf的配置。
6、配置雙機web目錄同步
(1)web1上腳本
[root@web1 ~]# vim unison_inotify.sh
#!/bin/bash
src=/data/www/
host=192.168.115.150
cd ${src}
/usr/local/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file
do
/usr/local/bin/unison -batch $src ssh://$host/$src/
echo -n "$file " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
[root@web1 ~]# chmod +x unison_inotify.sh
[root@web1 ~]# nohup ./unison_inotify.sh &
(2)web2上腳本
[root@web1 ~]# vim unison_inotify.sh
#!/bin/bash
src=/data/www/
host=192.168.115.120
cd ${src}
/usr/local/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file
do
/usr/local/bin/unison -batch $src ssh://$host/$src/
echo -n "$file " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
[root@web2 ~]# chmod +x unison_inotify.sh
[root@web2 ~]# nohup ./unison_inotify.sh &
最後分別在web1和web2上執行上面兩個腳本,這樣兩臺服務器的目錄會保持相互實時同步了!
7、總結
unison+inotify的雙向同步的確給我們帶來了極大的方便,但同時也有一個缺點:對於一個文件在兩個同步文件夾中都被修改時,unison是不會去同步的,因為unison無法判斷以哪個為準,需要人工幹預處理,這就需要我們自己權衡利弊。
unison+inotify實現數據實時雙向同步