一:Unison簡介
Unison是
windows和unix平臺(tái)下都可以使用的文件同步工具,它能使兩個(gè)文件夾(本地或網(wǎng)絡(luò)上的)保持內(nèi)容的一致。unison擁有其它一些同步工具或
文件系統(tǒng)的相同特性,但也有自己的特點(diǎn):
跨平臺(tái)使用;
對(duì)內(nèi)核和
用戶權(quán)限沒有特別要求;
unison是雙向的,它能自動(dòng)處理兩分拷貝中更新沒有沖突的部分,有沖突的部分將會(huì)顯示出來讓用戶選擇更新策略;
只要是能連通的兩臺(tái)主機(jī),就可以運(yùn)行unison,可以直接使用socket連接或安全的ssh連接方式,對(duì)帶寬的要求不高,使用類似rsync的壓縮傳輸
協(xié)議。
Unison有文字界面和圖形界面,這里只介紹如何在文字界面下使用.
二:安裝unison
unison各種版本
下載地址:
http://www.seas.upenn.edu/~bcpierce/unison//download.html
unison編譯器下載地址:
http://caml.inria.fr/pub/distrib/ocaml-3.10
其它有用鏈接;
http://worldhello.net/doc/bcp/unison.html
http://www.seas.upenn.edu/~bcpierce/unison//contrib.html
http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#rshmeth
http://linux.chinaunix.net/bbs/viewthread.php?tid=888250&extra=page%3D1%26amp%3Bfilter%3Dreward
從以上地址可以下載各種平臺(tái),各種版本的unison,有基于源碼安裝的,有二進(jìn)制的,我下載的是二進(jìn)制的,可以直接使用.這里介紹源碼安裝:
源碼安裝unison
Linux下從源碼包編譯安裝,需要一個(gè)叫做Objective Caml compiler的工具,版本至少3.0.7,可以從這里下載:
http://caml.inria.fr/
Ocaml安裝腳本如下:
# tar -zxf ocaml-3.09.3.tar.gz
# cd ocaml-3.09.3
# ./configure
# make world opt
# make install
Unison對(duì)版本要求很高,進(jìn)行同步的兩臺(tái)主機(jī)需要相同版本的unison,所以這里使用和windows一致的版本2.13.16,unison-2.13.16.tar.gz
安裝腳本如下:
# tar -zxf unison-2.13.16.tar.gz
# cd unison-2.13.16
# make UISTYLE=text
# make install
之后將生成可執(zhí)行文件unison,將其cp到系統(tǒng)PATH即可。
# cp ./unison /usr/local/bin
三:配置雙機(jī)ssh信任
由于unison在遠(yuǎn)程同步文件夾要登陸遠(yuǎn)程
服務(wù)器,因此要配置兩機(jī)互相信任
本例假設(shè)本地機(jī)為:10.178.1.132(linux)
遠(yuǎn)程機(jī):10.178.1.110(solaris)
1. 在兩臺(tái)機(jī)器上創(chuàng)建 RSA密鑰
以下操作要在本地機(jī)和遠(yuǎn)程機(jī)上都執(zhí)行一遍
(1)以 root 用戶
登錄
(2)在 root 用戶的 主目錄內(nèi)創(chuàng)建.ssh 目錄并設(shè)置正確的權(quán)限
[root@gsgatzhapp1 ~]# mkdir ~/.ssh
[root@gsgatzhapp1 ~]# chmod 700 ~/.ssh
(3)使用 ssh-keygen 命令生成第 2 版本的 SSH 協(xié)議的 RSA 密鑰
[root@gsgatzhapp1 ~]# 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:
17:e4:7c:79:8d:a0:00:3b:d9:f7:7a:56:f3:ac:54:4d [e
mail=oracle@gsgatzhapp1]
oracle@gsgatzhapp1[/email]
在提示保存私鑰(key)和公鑰(public key)的位置時(shí),使用默認(rèn)值。 如果需要私鑰密碼(passphrase),則輸入一個(gè)私鑰密碼(如果使用私鑰密碼,使用 ssh 執(zhí)行遠(yuǎn)程命令時(shí)需要輸入私鑰密碼,因此,本案例中未使用私鑰密碼),因此,直接回車即可。
2. 添加密鑰到授權(quán)密鑰文件(authorized key file)中
(1)以 root 用戶登錄
(2)在本地機(jī)上執(zhí)行
[root@gsgatzhapp1 ~] # cd ~/.ssh
[
root@gsgatzhapp1.ssh]#ssh 10.178.1.132 cat /root/.ssh/id_rsa.pub >> authorized_keys
[
oracle@gsgatzhapp1.ssh]#ssh 10.178.1.110 cat /root/.ssh/id_rsa.pub >> authorized_keys
[
oracle@gsgatzhapp1.ssh]#scp authorized_keys 10.178.1.110:/root/.ssh/
[oracle@gsgatzhapp1 .ssh]# chmod 600 /root/.ssh/authorized_keys
(3)在遠(yuǎn)程機(jī)10.178.1.110 上:
bash-2.05# chmod 600 /root/.ssh/authorized_keys
(4)測試
完成后,在 gsgatzhapp1 上執(zhí)行:
[root@gsgatzhapp1 ~]# ssh 10.178.1.132 date
[root@gsgatzhapp1 ~]#ssh 10.178.1.110 date
如果不需要輸入密碼就出現(xiàn)系統(tǒng)日期,說明 SSH 配置成功。
Unison使用文檔(下)
四:unison的使用
Unison可以在一臺(tái)主機(jī)上使用,同步兩個(gè)文件夾,也可以在網(wǎng)絡(luò)上是用。
1:本地使用
使用方法:
#unison 111 222 #同步本地的111和222文件夾
Contacting server...
Looking for changes
Warning: No archive files were found for these roots. This can happen either
because this is the first time you have synchronized these roots,
or because you have upgraded Unison to a new version with a different
archive format.
Update detection may take a while on this run if the replicas are
large.
Unison will assume that the 'last synchronized state' of both replicas
was completely empty. This means that any files that are different
will be reported as conflicts, and any files that exist only on one
replica will be judged as new and propagated to the other replica.
If the two replicas are identical, then no changes will be reported.
Press return to continue.[<spc>] Reconciling changes
111 222
<---- file aaaaaaaaaaaaa [f] ?
Commands:
<ret> or f or <spc> follow unison's recommendation (if any)
I ignore this path permanently
E permanently ignore files with this extension
N permanently ignore paths ending with this name
m merge the versions
d show differences
x show details
l list all suggested changes
p or b go back to previous item
g proceed immediately to propagating changes
q exit unison without propagating any changes
/ skip
> or . propagate from left to right
< or , propagate from right to left
<---- file aaaaaaaaaaaaa [f] f
Proceed with propagating updates? [] y
Propagating updates
UNISON started propagating changes at 15:06:08 on 27 Aug 2007
[BGN] Copying aaaaaaaaaaaaa
from /222
to /111
[END] Copying aaaaaaaaaaaaa
UNISON finished propagating changes at 15:06:08 on 27 Aug 2007
Saving synchronizer state
Synchronization complete (1 item transferred, 0 skipped, 0 failures)
如果檢測到兩個(gè)文件夾有所不同,unison會(huì)提示,讓你選擇相應(yīng)的操作。如上例所示.
表示右邊222的文件夾有新的文件,是否同步到左邊的111文件夾,f表示force,然后將確認(rèn),進(jìn)行更新,如果輸入?會(huì)有更詳細(xì)的介紹。
2: unison遠(yuǎn)程使用
使用方法:
# unison <本地目錄> ssh://remotehostname(IP)/<遠(yuǎn)程目錄的絕對(duì)路徑>
例如:
# unison /home/AAA ssh://username@remotehostname(ip)//DB/path/BBB
表示將本機(jī)的目錄/home/AAA和遠(yuǎn)端主機(jī)的/DB/path/BBB進(jìn)行同步。一般的,需要兩臺(tái)機(jī)能ssh連接。
注意 在主機(jī)和目錄間又多加了一個(gè) "/"
3:unison參數(shù)說明
Unison有很多參數(shù),這里只介紹經(jīng)常使用的幾個(gè),詳細(xì)的請(qǐng)參看unison手冊(cè):
" -testserver
測試連通性,連接到服務(wù)器即退出。示例:
$ unison / ssh://opensou1@bluehost/ -servercmd=~/bin/unison -testserver
如果服務(wù)器端 unison 可執(zhí)行文件不在默認(rèn)目錄下,甚至沒有 unison 命令(需要你編譯一個(gè)上傳到服務(wù)器),則需要使用 -servercmd 參數(shù)告訴要執(zhí)行的服務(wù)器 unison 命令位置。
使用 -testserver 參數(shù),則成功鏈接即退出,也不會(huì)去執(zhí)行目錄的比較等后續(xù)操作。
" -servercmd xxx
告訴 unison, 服務(wù)器端的 unison 命令是什么。參見上面的示例。
" -auto
接受缺省的動(dòng)作,然后等待用戶確認(rèn)是否執(zhí)行。
" -batch
batch mode, 全自動(dòng)模式,接受缺省動(dòng)作,并執(zhí)行。
" -ignore xxx
增加 xxx 到忽略列表中
" -ignorecase [true|false|default]
是否忽略文件名大小寫
" -follow xxx
是否支持對(duì)符號(hào)連接指向內(nèi)容的同步
" owner = true (保持同步過來的文件屬主)
" group = true (保持同步過來的文件組信息)
" perms = -1 (保持同步過來的文件讀寫權(quán)限)
" repeat = 1 (間隔1秒后,開始新的一次同步檢查)
" retry = 3 (失敗重試)
" sshargs = -C (使用ssh的壓縮傳輸方式)
" xferbycopying = true
" -immutable xxx
不變目錄,掃描時(shí)可以忽略
" -silent
安靜模式
" -times
同步修改時(shí)間
" -path xxx 參數(shù)
只同步 -path 參數(shù)指定的子目錄以及文件,而非整個(gè)目錄。-path 可以多次出現(xiàn),例如
unison /home/username ssh://remotehost//home/username \
-path shared \
-path pub \
-path .netscape/bookmarks.html
4:通過配置文件來使用unison
盡管可以完全通過命令行的方式來指定unison運(yùn)行所需要的參數(shù),但我們還是推薦使用配置文件來進(jìn)行配置使用unison,原因很簡單,看配置文件比看命令行容易理解,而且可管理性更強(qiáng)。
默認(rèn)的配置文件夾位于~currentuser/.unison,即當(dāng)前用戶的home目錄下,windows則位于C:\Documents and Settings\currentuser\.unison,默認(rèn)的配置文件名是default.prf.
運(yùn)行這樣的命令:
#unison exitgogo
Unison將默認(rèn)讀取~currentuser/.unison/exitgogo.prf文件里的配置信息.我的配置信息在/root/.unison/exitgogo.prf,因此我們可以根據(jù)上面參數(shù)的介紹,把所有的參數(shù)配置信息寫入到一個(gè).prf的文件中.
下面是我的一個(gè)web應(yīng)用中兩個(gè)文件夾同步的配置信息:
root = /sina/webdata
root = ssh://root@192.168.60.121//sina/webdata
#force =/sina/webdata
ignore = Path as/*
#prefer = ssh://root@192.168.60.121//sina/webdata
batch = true
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck=false
rsync =false
#debug=verbose
sshargs = -C
xferbycopying = true
log = true
logfile = /root/.unison/sina_122.1547.log
說明如下:
兩個(gè)root表示需要同步的文件夾
force表示以本地的/var/www/bbsnew文件夾為標(biāo)準(zhǔn),將該目錄同步到遠(yuǎn)端。注意,如果指定了force參數(shù),那么unison就變成了單項(xiàng)同步了,也就是說會(huì)以force指定的文件夾為準(zhǔn)進(jìn)行同步.
Unison本身是可以雙向同步的,但是要做到雙向同步,就不要設(shè)置force參數(shù),如果設(shè)置了force參數(shù),就成了單項(xiàng)同步了,此時(shí)unison類似與sync.
Unison雙向同步基本原理是:假如有A B兩個(gè)文件夾,A文件夾把自己的改動(dòng)同步到B,B文件夾也把自己的改動(dòng)同步到A,最后A B兩文件夾的內(nèi)容相同,是 A B文件夾的合集.
Unison雙向同步的一個(gè)缺點(diǎn)是,對(duì)于一個(gè)文件在兩個(gè)同步文件夾中都被修改時(shí),unison是不會(huì)去同步的,因?yàn)閡nison無法判斷以那個(gè)為準(zhǔn).
ignore = Path表示忽略/sina/webdata下面的WEB-INF/tmp目錄,即同步時(shí)不同步它。
batch = true,表示全自動(dòng)模式,接受缺省動(dòng)作,并執(zhí)行
-fastcheck true表示同步時(shí)使用文件的創(chuàng)建時(shí)間來比較兩地文件,如果這個(gè)選項(xiàng)為false,unison則將比較兩地文件的內(nèi)容.建議設(shè)置為true
log = true表示在終端輸出運(yùn)行信息。
logfile則指定了同時(shí)將輸出寫入log文件。
五:unison FAQ
如何在和遠(yuǎn)程服務(wù)器同步大量數(shù)據(jù),上傳一部分?jǐn)?shù)據(jù)后,超時(shí):
9% 559:15 ETARead from remote host bluehost: Connection reset by peer
Fatal error: Lost connection with the server
實(shí)際操作中,最好的方法是,第一次先把要上傳的文件打成包,用 ftp 上傳,然后展開到服務(wù)器中,之后執(zhí)行一次 unison 同步即可