1. 程式人生 > >CentOS7.2下unison+inotify的Web目錄同步方案

CentOS7.2下unison+inotify的Web目錄同步方案

web 同步 unison inotify

CentOS7.2下unison+inotify的Web目錄同步方案


學習 unison



  • CentOS7.2下unison+inotify的Web目錄同步方案

    • 1. 背景

    • 2. Unison簡介

    • 3. 環境準備

    • 4. 安裝Objective Caml compiler

    • 5. 安裝unison

    • 6. 安裝inotify

    • 7. 配置雙機ssh信任

    • 8. unison的使用

    • 9. 配置雙機web目錄同步

    • 10. 總結


1. 背景

最近需要上線一個公司展廳項目,項目中主要是後臺圖片管理。因此它基本不會出現多人同時修改同一圖片的情況,這樣做雙機的情況下,WEB目錄最好是雙向同步。

在Linux下做WEB目錄文件同步,一般有如下幾種方式:

  1. nfs實現web數據共享

  2. rsync +inotify實現web數據同步

  3. rsync+sersync更快更節約資源實現web數據同步

  4. unison+inotify實現web數據雙向同步

他們各有優缺點,這裏我根據實際情況,選擇方案4。

2. Unison簡介

技術分享

Unison是一款跨平臺的文件同步工具,不僅支持本地對本地同步,也支持通過SSH、RSH和Socket等網絡協議進行同步。更棒的是,Unison支持雙向同步操作,你既可以從A同步到B,也可以從B同步到A,這些都不需要額外的設定。

官方文檔:
http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4-manual.html

3. 環境準備

CentOS7.2 2臺:
show160 10.1.0.160
show161 10.1.0.161

4. 安裝Objective Caml compiler

Objective Caml compiler (version 3.11.2 or later) 官網地址:http://caml.inria.fr/

cd /tmp
wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz
tar -zxvf ocaml-4.03.0.tar.gz
cd ocaml-4.03.0
./configure
make configure
make world opt
make install

5. 安裝unison

如果需要同步到遠程目錄,則遠程機器也需要安裝unison。

yum -y install ctags-etags  # 缺少此安裝包時下面make步驟會報錯
cd /tmp
wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
mkdir unison-2.48.4 && cd unison-2.48.4
tar -zxvf /tmp/unison-2.48.4.tar.gz
cd src
make UISTYLE=text THREADS=true
cp unison /usr/local/bin/
unison -version # 有版本信息出現,則安裝成功

6. 安裝inotify

inotify官方地址:https://en.wikipedia.org/wiki/Inotify

yum -y install inotify-tools

7. 配置雙機ssh信任

show160上生成密鑰,不輸入私鑰密碼。

[root@show160 src]# ssh-keygen
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:
d6:3b:8a:47:23:04:5d:31:9a:97:d2:d3:5c:1b:f7:a3 root@show160
The key‘s randomart image is:
+--[ RSA 2048]----+
| . .+. o . |
| . .+ = . + . |
| .+ = o . ..|
| .o o . .|
| . S . E |
| ..o . |
| o .o |
| ... . |
| ... |
+-----------------+
[root@show160 src]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
[root@show160 src]# chmod 700 ~/.ssh
[root@show160 src]# chmod 600 ~/.ssh/authorized_keys
[root@show160 src]# rsync -avz /root/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys

show161上生成密鑰,不輸入私鑰密碼。

[root@show161 tomcat]# ssh-keygen 
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:
e8:b4:f7:91:ad:a0:83:fb:00:55:c2:c6:2c:65:08:91 root@show161
The key‘s randomart image is:
+--[ RSA 2048]----+
|o+ *+ . |
|E o.=o |
| o. |
| . . |
| . o S |
| . o . o |
| ..o o o . |
| ...o o o |
| .oo. o |
+-----------------+
[root@show161 tomcat]# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
[root@show161 tomcat]# rsync -avz /root/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys

在2臺機器上分別ssh對方IP,能無密碼登錄則表示配置成功。

8. unison的使用

unison的用法非常靈活和簡單,可以通過如下三種方式調用unison。
第一種方式:”unison profile_name [options]”
unison默認會讀取~/.unison目錄下的配置文件”profile_name.prf”。

註意,在這種方式下,命令行中並沒有指出要進行同步的兩個地址,所以,此種調用unison的方式必須在配置文件profile_name.prf中通過相關的root指令設置同步的路徑和同步的參數,如:

#Unison preferences file 
root = /tmp/test
root = ssh://root@10.1.0.161//tmp/test/
#force =
#ignore =
batch = true

第二種方式:”unison profile root1 root2 [options]”
root1、root2分別表示要執行同步的兩個路徑。這兩個路徑可以是本地目錄路徑,也可以是遠程服務器的路徑,如ssh://username@//tmp/test 。由於同步的路徑已經在命令行指定了,所以這裏無需在profile.prf配置文件中進行root指令的相關設置。

第三種方式:”unison root1 root2 [options]”
這種方式相當於執行”unison default root1 root2”命令,即unison默認讀取default.prf的配置。

9. 配置雙機web目錄同步

[root@show160 src]# mkdir -p /root/.unison/
[root@show160 src]# vim /root/.unison/default.prf
#Unison preferences file 
root = /data/showroom/
root = ssh://root@10.1.0.161//data/showroom/
#force =
#ignore =
batch = true
maxthreads = 300
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /root/.unison/unison.log
[root@show161 tmp]# mkdir -p /root/.unison/
[root@show161 tmp]# vim /root/.unison/default.prf
#Unison preferences file 
root = /data/showroom/
root = ssh://root@10.1.0.160//data/showroom/
#force =
#ignore =
batch = true
maxthreads = 300
#repeat = 1
#retry = 3
owner = true
group = true
perms = -1
fastcheck = false
rsync = false
sshargs = -C
xferbycopying = true
log = true
logfile = /root/.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 可以多次出現。

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

在兩臺機器都添加如下腳本,並使用nohup方式運行。有需要則將腳本添加到/etc/rc.local中。

#/bin/bash 

src="/data/showroom/"

/usr/bin/inotifywait -mrq -e create,delete,modify,move $src | while read line; do
/usr/local/bin/unison
echo -n "$(date +%F-%T) $line" >> /var/log/inotify.log
done

10. 總結

經過以上介紹,我們大體知道了unison的使用方法。它的雙向同步的確給我們帶來了極大的方便,但同時也有一個缺點:對於一個文件在兩個同步文件夾中都被修改時,unison是不會去同步的,因為unison無法判斷以哪個為準,需要人工幹預處理,這就需要我們自己權衡利弊。


本文出自 “ygqygq2” 博客,請務必保留此出處http://ygqygq2.blog.51cto.com/1009869/1922203

CentOS7.2下unison+inotify的Web目錄同步方案