Linux下Unison實現檔案雙向同步

2021-09-05 04:19:55 字數 3182 閱讀 4633

#unison preferences file 

root = /home/admin/test 

root = ssh: 

#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@vm2 ~]$ vim /home/unison/.unison/default.prf

#unison preferences file 

root = /home/admin/test 

root = ssh: 

#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 *** //增加 *** 到忽略列表中

-ignorecase [true|false|default] //是否忽略檔名大小寫

-follow *** //是否支援對符號連線指向內容的同步

owner = true //保持同步過來的檔案屬主

group = true //保持同步過來的檔案組資訊

perms = -1 //保持同步過來的檔案讀寫許可權

repeat = 1 //間隔1秒後,開始新的一次同步檢查

retry = 3 //失敗重試

sshargs = -c //使用ssh的壓縮傳輸方式

xferbycopying = true"

-immutable *** //不變目錄,掃瞄時可以忽略

-silent //安靜模式

-times //同步修改時間

-path *** 引數 //只同步 -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連線的時候可能需要輸入一次密碼,之後就不需要輸入了。

六、定期或實時執行同步

如果想要定期執行,則通過crontab計畫任務來實現,例如通過以下方式設定每5分鐘執行一次

[root@server1 ~]# su - unison

[unison@server1 ~]$ crontab -e

1    */5 * * * * /usr/local/bin/unison

如果想要實時同步,則需要結合inotify-tools來實現,具體實現方式請參考我的這一篇文章:

通過rsync+inotify-tools+ssh實現觸發式遠端實時同步

本文出自 「無雲安全技術站

」 部落格,請務必保留此出處

Linux下Unison實現檔案雙向同步

unison preferences file root home admin test root ssh force ignore batch true repeat 1 retry 3 owner true group true perms 1 fastcheck false rsync fal...

Linux下利用rsync實現多伺服器檔案同步

windows做為檔案伺服器,使用rsync的windows服務版本,然後配置好就可以了。需要的朋友可以參考下。目標 多伺服器檔案同步 環境 2臺centos5.6 web端 192.168.20.20 backup端 192.168.20.21 需要備份目錄為 192.168.20.20 usr ...

Linux下利用rsync實現多伺服器檔案同步

linux下利用rsync實現多伺服器檔案同步 第一部分 簡單的資料備份 第一步 伺服器端的配置 rpm qa grep rsync cd usr local src wget tar zxvf rsync 3.0.6pre1.tar.gz cd rsync 3.0.6pre1 configure ...