unison + inotify文件雙向?qū)崟r(shí)同步


unison+inotify實(shí)現(xiàn)文件數(shù)據(jù)雙向?qū)崟r(shí)同步

Unison是一款跨平臺(tái)的文件同步工具,不僅支持本地對(duì)本地同步,

也支持通過SSH、RSH和Socket等網(wǎng)絡(luò)協(xié)議進(jìn)行同步。更棒的是,

Unison支持雙向同步操作,你既可以從A同步到B,也可以從B同步到A,這些都不需要額外的設(shè)定。

系統(tǒng):CentOS7.4 兩臺(tái)

server1:192.168.10.10

server2:192.168.10.11

一.安裝Objective Caml compiler

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

cd /opt

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

二.安裝unison

如果需要單向同步到遠(yuǎn)程目錄,則遠(yuǎn)程機(jī)器也需要安裝unison。

(例如只需要A機(jī)器遠(yuǎn)程實(shí)時(shí)同步文件到B機(jī)器,B不需同步到A。則兩臺(tái)機(jī)器都需要安裝unison.)

yum -y install ctags-etags? # 缺少此安裝包時(shí)下面make步驟會(huì)報(bào)錯(cuò)

cd /opt

wgethttp://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 /opt/unison-2.48.4.tar.gz

cd src

make UISTYLE=text THREADS=true

cp unison /usr/local/bin/

unison -version? # 有版本信息出現(xiàn),則安裝成功

三.安裝inotify

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

yum -y install inotify-tools (yum源安裝)

wget?http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar?xvf?inotify-tools-3.14.tar.gz

cd?inotify-tools-3.14

./configure

make

make?install

四.雙機(jī)ssh信任

server1上執(zhí)行

ssh-keygen -t rsa 一直打回車就行了

ssh-copy-id root@192.168.10.11 #回車輸入11的密碼

server2上執(zhí)行

ssh-keygen -t rsa 一直打回車就行了

ssh-copy-id root@192.168.10.10 #回車輸入10的密碼

#注 !如果非22端口,如2222端口,如下:

ssh-keygen -t rsa 一直打回車

ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 2222 root@192.168.10.11"

五.unison的使用

unison的用法非常靈活和簡(jiǎn)單,可以通過如下三種方式調(diào)用unison。?

第一種方式:”unison profile_name [options]”?

unison默認(rèn)會(huì)讀取~/.unison目錄下的配置文件”default.prf”

第二種方式:”unison profile root1 root2 [options]”

root1、root2分別表示要執(zhí)行同步的兩個(gè)路徑。這兩個(gè)路徑可以是本地目錄路徑,也可以是遠(yuǎn)程服務(wù)器的路徑,如ssh://username@//tmp/test 。由于同步的路徑已經(jīng)在命令行指定了,所以這里無需在profile.prf配置文件中進(jìn)行root指令的相關(guān)設(shè)置。

第三種方式:”unison root1 root2 [options]”

這種方式相當(dāng)于執(zhí)行”unison default root1 root2”命令,即unison默認(rèn)讀取default.prf的配置。

六.配置雙機(jī)文件目錄同步(如需單向同步則只需要配置server端)

unison默認(rèn)讀取/root/.unison/default.prf的配置.但是安裝完成后默認(rèn)不會(huì)創(chuàng)建這個(gè)文件.需要手動(dòng)創(chuàng)建

mkdir -p /root/.unison/

vim /root/.unison/default.prf

配置文件如下:

#Unison preferences file

root = /data/share/

root = ssh://root@192.168.10.11//data/share/

#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

七.創(chuàng)建同步腳本

mkdir -p /opt/script

cd /opt/script

vim inotify-unison.sh

#!/bin/bash

##########################################

/usr/bin/inotifywait -mrq? /data/share/ --format "%w%f"? -e create|while read line

? do

? ? echo `date +%F' '%T'? '%A`

/usr/local/bin/unison

? ? echo -e ' \n \n '

done

#保存腳本文件

chmod +xinotify-unison.sh

八.編寫腳本啟動(dòng)停止服務(wù)

vim /etc/init.d/unison_inotify #以下為腳本內(nèi)容

#!/bin/bash

# intofitywait watch /data/share/

# unison ---? sync from /data/share to //remote//data/share

. /etc/rc.d/init.d/functions

use_username=root

log_path=/$use_username/.unison/unison-diy.log

procID=`ps aux |grep inotify-unison.sh |grep -v grep|awk '{print $2}'`start() {

? ? ? ? echo -n $"Starting unison_inotify... "

? ? ? ? su - $use_username -c "nohup? /script/inotify-unison.sh? >>$log_path 2>&1 &"

? ? RETVAL=$?

? ? ? ? [ $RETVAL = 0 ] && action

}

stop() {

? ? #echo? $"Stopping unison_inotify ... "

? ? kill $procID? >/dev/null 2>&1

? ? RETVAL=$?

? ? [ $RETVAL = 0 ] && echo? "Stopping unison_inotify...? `action`" || echo 'unison_inotify(PID) is not run'

}

status() {

? ? check_proc=`ps aux |grep inotify-unison.sh |grep -v grep|awk '{print $2}'|wc -l`? ?

? ? if [ $check_proc -ne 0 ]

? ? ? then

? ? ? echo 'unison_inotify is running'

? ? else

? ? ? echo 'unison_inotify is not run'

? ? fi

}

case "$1" in

? start)

? ? start

? ? ;;

? stop)

? ? stop

? ? ;;

? status)

? ? status

? ? ;;

? restart)

? ? stop

? ? start

? ? ;;

? *)

? ? echo $"Usage: $prog {start|stop|status}"

? ? RETVAL=2

esac

exit $RETVAL

#保存文件

chmod +x/etc/init.d/unison_inotify

啟動(dòng)|關(guān)閉|狀態(tài)|重啟腳本文件

/etc/init.d/unison_inotify start|stop|status|restart

9.inotify優(yōu)缺點(diǎn)

優(yōu)點(diǎn):? ?監(jiān)控文件系統(tǒng)事件變化, 通過同步工具實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)同步.

缺點(diǎn):? ? 并發(fā)如果大于200個(gè)文件?( 10-100K ),?同步就會(huì)有延遲.

10.注意事項(xiàng)

如果inotify監(jiān)控文件過大(T級(jí)別文件)需要配置

查看系統(tǒng)默認(rèn)參數(shù)值

sysctl -a | grep max_queued_events

結(jié)果是:fs.inotify.max_queued_events = 16384

sysctl -a | grep max_user_watches

結(jié)果是:fs.inotify.max_user_watches = 8192

sysctl -a | grep max_user_instances

結(jié)果是:fs.inotify.max_user_instances = 128

修改參數(shù):

vim /etc/sysctl.conf #添加以下代碼

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

:wq! #保存退出

sysctl -p #生效

參數(shù)說明:

max_queued_events #inotify隊(duì)列最大長(zhǎng)度,如果值太小,會(huì)出現(xiàn)"** Event Queue Overflow **"錯(cuò)誤,導(dǎo)致監(jiān)控文件不準(zhǔn)確

max_user_watches #要同步的文件包含多少目錄,可以用:find /data -type d | wc -l 統(tǒng)計(jì),必須保證max_user_watches值大于統(tǒng)計(jì)結(jié)果(這里/data為同步文件目錄)

max_user_instances #每個(gè)用戶創(chuàng)建inotify實(shí)例最大值.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容