Centos7 unison+inotify數據目錄雙向同步

unison+inotify

#安裝Objective Caml compiler
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 world opt
make install

#安裝unison
yum -y install ctags-etags
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
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install inotify-tools

#配置雙機ssh信任

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

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

#/bin/bash 
src="/data/"
/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

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