雙向同步軟件Unison的安裝與配置

一、Unison簡介
Unison是Windows、Linux以及其他Unix平臺下都可以使用的文件同步工具,它能使兩個文件夾(本地或網絡上的)保持內容的一致。Unison擁有與其它一些同步工具或文件系統的相同的特性,但也有自身的特點:
1.跨平臺使用;
2.對內核和用戶權限沒有特別要求;
3.Unison是雙向的,它能自動處理兩分拷貝中更新沒有衝突的部分,有衝突的部分將會顯示出來讓用戶選擇更新策略;
4.只要是能連通的兩臺主機,就可以運行unison,可以直接使用socket連接或安全的ssh連接方式,對帶寬的要求不高,使用類似rsync的壓縮傳輸協議。
5. Unison實時雙向同步的配置不太好做,而且雙向同步本身就是不靠譜的,很容易發生混亂。


server1

192.168.137.61

centos6.5

server2

192.168.137.102

centos6.5

關閉防火牆和selinux

reboot  重啓服務器


二、編譯安裝Unison(只在一臺服務器上安裝ocaml、unison即可)

Linux下通過源碼包編譯安裝Unison時,需要用到Objective Caml compiler。
通過以下方式安裝

                              yum  install gcc gcc-c++ cmake openssh-server openssh-client    兩臺都運行這條命令~

[root@server1 ~]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.0.tar.gz
[root@server1 ~]# tar -xzvf ocaml-3.12.0.tar.gz
[root@server1 ~]# cd ocaml-3.12.0
[root@server1 ocaml-3.12.0]# ./configure
[root@server1 ocaml-3.12.0]# make world opt
[root@server1 ocaml-3.12.0]# make install


編譯安裝Unison 

                                 cd ~
[root@server1 ~]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.40.63/unison-2.40.63.tar.gz

[root@server1 ~]# tar -xzvf unison-2.40.63.tar.gz
[root@server1 ~]# cd unison-2.40.63
[root@server1 unison-2.40.63]# make UISTYLE=text THREADS=true STATIC=true            
[root@server1 unison-2.40.63]# make install


在執行make install的過程中,可能會出現以下錯誤提示:
mv: cannot stat '/root/bin//unison': No such file or directory
make: [doinstall] Error 1 (ignored)
cp unison /root/bin/
cp: cannot create regular file '/root/bin/': Is a directory
make: *** [doinstall] Error 1


出現錯誤的原因在與Unison默認是將文件Copy到/root/bin目錄,但Linux默認是沒有該目錄的,因此我們需要將生成的可執行文件unison複製到系統的PATH目錄。
[root@server1 unison-2.40.63]# cp unison /usr/local/bin


將可執行文件unison上傳到遠程主機(假設server2主機IP爲192.168.137.102)
[root@server1 unison-2.40.63]# scp unison root@192.168.137.102:/root/


通過SSH登陸到遠程主機,再將unison複製到server2的PATH目錄


[root@server2 ~]#cp unison /usr/local/bin


三、配置ssh key信任
建議通過普通用戶進行操作,理由是通過root操作本身就危險,免密碼登陸的root就更危險了。


在兩臺服務器上創建unison用戶
[root@server1 ~]# useradd -m unison
[root@server1 ~]# passwd unison


[root@server2 ~]# useradd -m unison
[root@server2 ~]# passwd unison


在server1上創建key並配置server2的信任
[root@server1 ~]# su  unison

                                 cd  ~
[unison@server1 ~]$ ssh-keygen -t rsa
在提示保存私鑰(key)和公鑰(public key)的位置時,使用默認值;
在提示是否需要私鑰密碼(passphrase)時,直接敲回車,即不使用私鑰密碼。
之後,將生成一對密鑰,id_rsa(私鑰文件)和id_rsa.pub(公鑰文件),保存在/home/unison/.ssh/目錄下。


將公鑰添加到server2的 authorized_keys 文件中
將文件上傳到server2(假設server2主機IP爲192.168.137.102
[unison@server1 ~]$ scp ~/.ssh/id_rsa.pub unison@192.168.137.102:/home/unison/


使用rsync用戶SSH到登陸到遠程主機server2 ,並將公鑰添加到 authorized_keys 文件中

                                  su  unison

                                  cd ~
[unison@server2 ~]$ mkdir .ssh
[unison@server2 ~]$ chmod 700 .ssh
[unison@server2 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys


同理,執行以下步驟在server2上創建key並配置server1的信任

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

                                   

將文件上傳到server1(假設server1主機IP爲192.168.137.61
[unison@server2 ~]$ scp ~/.ssh/id_rsa.pub unison@192.168.137.61:/home/unison/


使用rsync用戶SSH到登陸到server1,並將公鑰添加到 authorized_keys 文件中
[unison@server1 ~]$ mv ~/id_rsa.pub ~/.ssh/authorized_keys


重啓SSH服務

                               su root
[root@server1 ~]# /etc/init.d/sshd restart


[root@server2 ~]# /etc/init.d/sshd restart


四、Unison的配置與使用
在兩臺服務器上創建test目錄,用於測試
[root@server1 ~]# su  unison

                                   cd ~
[unison@server1 ~]$ mkdir test


[root@server2 ~]# su unison

                                  cd ~
[unison@server2 ~]$ mkdir test


在兩臺服務器上分別執行一次unison,如果出現提示確認,則直接敲回車選擇默認值  
[unison@server1 ~]$ unison /home/unison/test/ ssh://[email protected]//home/unison/test/



[unison@server2 ~]$ unison /home/unison/test/ ssh://[email protected]//home/unison/test/



修改兩臺服務器的unison配置文件,輸入以下內容
[unison@server1 ~]$ vim /home/root/.unison/default.prf


#Unison preferences file
root = /home/unison/test
root = ssh://unison@192.168.137.102//home/unison/test/
#force =
#ignore =
batch = true
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /home/unison/.unison/unison.log



[unison@server2 ~]$ vim /home/unison/.unison/default.prf


#Unison preferences file
root = /home/unison/test
root = ssh://unison@192.168.137.61//home/unison/test/
#force =
#ignore =
batch = true
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /home/unison/.unison/unison.log


相關注解如下:
force表示會以本地所指定文件夾爲標準,將該目錄同步到遠端。這裏需要注意,如果指定了force參數,那麼Unison就變成了單項同步了,也就是說會以force指定的文件夾爲準進行同步,類似與rsync。
Unison雙向同步基本原理是:假如有A B兩個文件夾,A文件夾把自己的改動同步到B,B文件夾也把自己的改動同步到A,最後A B兩文件夾的內容相同,是AB文件夾的合集。
Unison雙向同步的一個缺點是,對於一個文件在兩個同步文件夾中都被修改時,unison是不會去同步的,因爲unison無法判斷以那個爲準。
ignore = Path表示忽略指定目錄,即同步時不同步它。
batch = true,表示全自動模式,接受缺省動作,並執行。
-fastcheck true 表示同步時僅通過文件的創建時間來比較,如果選項爲false,Unison則將比較兩地文件的內容。
log = true 表示在終端輸出運行信息。
logfile 指定輸出的log文件。


另外,Unison有很多參數,這裏僅介紹常用的幾個,詳細的請參看Unison手冊。
-auto //接受缺省的動作,然後等待用戶確認是否執行。
-batch //batch mode, 全自動模式,接受缺省動作,並執行。
-ignore xxx //增加 xxx 到忽略列表中
-ignorecase [true|false|default] //是否忽略文件名大小寫
-follow xxx //是否支持對符號連接指向內容的同步
owner = true //保持同步過來的文件屬主
group = true //保持同步過來的文件組信息
perms = -1 //保持同步過來的文件讀寫權限
repeat = 1 //間隔1秒後,開始新的一次同步檢查
retry = 3 //失敗重試
sshargs = -C //使用ssh的壓縮傳輸方式
xferbycopying = true"
-immutable xxx //不變目錄,掃描時可以忽略
-silent //安靜模式
-times //同步修改時間
-path xxx 參數 //只同步 -path 參數指定的子目錄以及文件,而非整個目錄,-path 可以多次出現。


PS:Windows下的unison配置文件默認位於C:\Documents and Settings\currentuser\.unison目錄,默認的配置文件名是default.prf。



五、測試
首先分別在server1與server2的/home/unison/test目錄下創建文件或目錄,然後在server1上執行unison,接着如果在server1與server2上都能看到各自創建的文件,就說明同步成功。


分別在server1與server2上創建文件
[unison@server1 ~]$ cd test
[unison@server1 test]$ touch 1.txt touch 3.txt


[unison@server2 ~]$ cd test
[unison@server2 test]$ touch 2.txt touch 4.txt


在server1上執行unison
[unison@server1 ~]$ unison


在server1與server2上查看文件是否同步
[unison@server1 ~]$ cd test
[unison@server1 test]$ ls
1.txt 2.txt 3.txt 4.txt


[unison@server2 ~]$ cd test
[unison@server2 test]$ ls
1.txt 2.txt 3.txt 4.txt


均看到了“1.txt 2.txt 3.txt 4.txt”所有文件,說明文件同步已經成功!


注意:第一次SSH連接的時候可能需要輸入一次密碼,之後就不需要輸入了。





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