歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix基礎知識 >> 用Unison實現數據的雙向實時同步

用Unison實現數據的雙向實時同步

日期:2017/3/3 15:23:28   编辑:Unix基礎知識

一、Unison簡介

Unison是windows和unix平台下都可以使用的文件同步工具,它能使兩個文件夾(本地或網絡上的)保持內容的一致。unison擁有其它一些同步工具或文件系統的相同特性,但也有自己的特點:

跨平台使用;

對內核和用戶權限沒有特別要求;

unison是雙向的,它能自動處理兩分拷貝中更新沒有沖突的部分,有沖突的部分將會顯示出來讓用戶選擇更新策略;

只要是能連通的兩台主機,就可以運行unison,可以直接使用socket連接或安全的ssh連接方式,對帶寬的要求不高,使用類似rsync的壓縮傳輸協議。

Unison有文字界面和圖形界面,這裡只介紹如何在文字界面下使用.

二、安裝unison

unison各種版本下載地址:

http://www.seas.upenn.edu/~bcpierce/unison//download.html

unison編譯器下載地址:

http://caml.inria.fr/pub/distrib/ocaml-3.10

從以上地址可以下載各種平台,各種版本的unison,有基於源碼安裝的,有二進制的,我下載的是二進制的,可以直接使用.這裡介紹源碼安裝:

1:源碼安裝unison

Linux下從源碼包編譯安裝,需要一個叫做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對版本要求很高,進行同步的兩台主機需要相同版本的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

之後將生成可執行文件unison,將其cp到系統PATH即可。

# cp ./unison /usr/local/bin

三、配置雙機ssh信任

由於unison在遠程同步文件夾要登陸遠程服務器,因此要配置兩機互相信任

本例假設本地機為:10.178.1.132(linux)

遠程機:10.178.1.110(solaris)

1. 在兩台機器上創建 RSA密鑰

以下操作要在本地機和遠程機上都執行一遍

(1)以 root 用戶登錄

(2)在 root 用戶的 主目錄內創建.ssh 目錄並設置正確的權限

[root@gsgatzhapp1 ~]# mkdir ~/.ssh

[root@gsgatzhapp1 ~]# chmod 700 ~/.ssh

(3)使用 ssh-keygen 命令生成第 2 版本的 SSH 協議的 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 oracle@gsgatzhapp1

在提示保存私鑰(key)和公鑰(public key)的位置時,使用默認值。 如果需要私鑰密碼(passphrase),則輸入一個私鑰密碼(如果使用私鑰密碼,使用 ssh 執行遠程命令時需要輸入私鑰密碼,因此,本案例中未使用私鑰密碼),因此,直接回車即可。

2. 添加密鑰到授權密鑰文件(authorized key file)中

(1)以 root 用戶登錄

(2)在本地機上執行

[root@gsgatzhapp1 ~] # cd ~/.ssh

[[email protected]]#ssh 10.178.1.132 cat /root/.ssh/id_rsa.pub >> authorized_keys

[[email protected]]#ssh 10.178.1.110 cat /root/.ssh/id_rsa.pub >> authorized_keys

[[email protected]]#scp authorized_keys 10.178.1.110:/root/.ssh/

[oracle@gsgatzhapp1 .ssh]# chmod 600 /root/.ssh/authorized_keys

(3)在遠程機10.178.1.110 上:

bash-2.05# chmod 600 /root/.ssh/authorized_keys

(4)測試

完成後,在 gsgatzhapp1 上執行:

[root@gsgatzhapp1 ~]# ssh 10.178.1.132 date

[root@gsgatzhapp1 ~]#ssh 10.178.1.110 date

如果不需要輸入密碼就出現系統日期,說明 SSH 配置成功。

四、unison的使用

Unison可以在一台主機上使用,同步兩個文件夾,也可以在網絡上是用。

1:本地使用

使用方法:

#unison ixdba ixdba1 #同步本地的ixdba和ixdba1文件夾

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

ixdba ixdba1

<---- file readme.txt [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 readme.txt [f] f

Proceed with propagating updates? [] y

Propagating updates

UNISON started propagating changes at 15:06:08 on 27 Aug 2007

[BGN] Copying readme.txt

from /ixdba1

to /ixdba

[END] Copying readme.txt

UNISON finished propagating changes at 15:06:08 on 27 Aug 2007

Saving synchronizer state

Synchronization complete (1 item transferred, 0 skipped, 0 failures)

如果檢測到兩個文件夾有所不同,unison會提示,讓你選擇相應的操作。如上例所示.

表示右邊ixdba1的文件夾有新的文件,是否同步到左邊的ixdba文件夾,f表示force,然後將確認,進行更新,如果輸入?會有更詳細的介紹。

2: unison遠程使用

使用方法:

# unison <本地目錄> ssh://remotehostname(IP)/<遠程目錄的絕對路徑>

例如:

# unison /home/AAA ssh://username@remotehostname(ip)//DB/path/BBB

表示將本機的目錄/home/AAA和遠端主機的/DB/path/BBB進行同步。一般的,需要兩台機能ssh連接。

注意 在主機和目錄間又多加了一個 "/"

3:unison參數說明

Unison有很多參數,這裡只介紹經常使用的幾個,詳細的請參看unison手冊:

-testserver

測試連通性,連接到服務器即退出。示例:

$ unison / ssh://opensou1@bluehost/ -servercmd=~/bin/unison -testserver

如果服務器端 unison 可執行文件不在默認目錄下,甚至沒有 unison 命令(需要你編譯一個上傳到服務器),則需要使用 -servercmd 參數告訴要執行的服務器 unison 命令位置。

使用 -testserver 參數,則成功鏈接即退出,也不會去執行目錄的比較等後續操作。

-servercmd xxx

告訴 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 可以多次出現,例如

unison /home/username ssh://remotehost//home/username \

-path shared \

-path pub \

-path .netscape/bookmarks.html

4:通過配置文件來使用unison

盡管可以完全通過命令行的方式來指定unison運行所需要的參數,但我們還是推薦使用配置文件來進行配置使用unison,原因很簡單,看配置文件比看命令行容易理解,而且可管理性更強。

默認的配置文件夾位於~currentuser/.unison,即當前用戶的home目錄下,windows則位於C:\Documents and Settings\currentuser\.unison,默認的配置文件名是default.prf.

運行這樣的命令:

#unison exitgogo

Unison將默認讀取~currentuser/.unison/exitgogo.prf文件裡的配置信息.我的配置信息在/root/.unison/exitgogo.prf,因此我們可以根據上面參數的介紹,把所有的參數配置信息寫入到一個.prf的文件中.

下面是我的一個web應用中兩個文件夾同步的配置信息:

root = /sina/webdata

root = ssh://[email protected]//sina/webdata

#force =/sina/webdata

ignore = Path as/*

#prefer = ssh://[email protected]//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

說明如下:

兩個root表示需要同步的文件夾

force表示以本地的/var/www/bbsnew文件夾為標准,將該目錄同步到遠端。注意,如果指定了force參數,那麼unison就變成了單項同步了,也就是說會以force指定的文件夾為准進行同步.

Unison本身是可以雙向同步的,但是要做到雙向同步,就不要設置force參數,如果設置了force參數,就成了單項同步了,此時unison類似與sync.

Unison雙向同步基本原理是:假如有A B兩個文件夾,A文件夾把自己的改動同步到B,B文件夾也把自己的改動同步到A,最後A B兩文件夾的內容相同,是 A B文件夾的合集.

Unison雙向同步的一個缺點是,對於一個文件在兩個同步文件夾中都被修改時,unison是不會去同步的,因為unison無法判斷以那個為准.

ignore = Path表示忽略/sina/webdata下面的WEB-INF/tmp目錄,即同步時不同步它。

batch = true,表示全自動模式,接受缺省動作,並執行

-fastcheck true表示同步時使用文件的創建時間來比較兩地文件,如果這個選項為false,unison則將比較兩地文件的內容.建議設置為true

log = true表示在終端輸出運行信息。

logfile則指定了同時將輸出寫入log文件。

注意事項:

如何在和遠程服務器同步大量數據,上傳一部分數據後,超時:

9% 559:15 ETARead from remote host bluehost: Connection reset by peer

Fatal error: Lost connection with the server

實際操作中,最好的方法是,第一次先把要上傳的文件打成包,用 ftp 上傳,然後展開到服務器中,之後執行一次 unison 同步即可。

出處:http://ixdba.blog.51cto.com/2895551/584334

Copyright © Linux教程網 All Rights Reserved