天天看點

rsync+inotify實時同步資料

1.首先,在實踐實時同步之前我們先來了解一下rsync無差異同步 無差異推送資料加--delete 1)備份 --delete風險 本地有,遠端就有。本地沒有遠端有也會删除,伺服器端的目錄資料可能會丢失 無差異拉取資料 2)代碼釋出、下載下傳 --delete風險 遠端有,本地就有。遠端沒有的本地有也會删除,本地的目錄資料可能丢失
rsync+inotify實時同步資料
ps:上圖會将遠端端的/hzftp/test裡的檔案完全克隆到用戶端的/data1目錄中

多子產品共享配置,隻需在伺服器端/etc/rsyncd.conf新增子產品目錄位址即可,其他

rsync+inotify實時同步資料

inotify參考資料wiki:https://github.com/rvoicilas/inotify-tools/wiki

inotify實施(切記在用戶端上安裝)

前提:需要rsync daemon已經搭建完成的情況下,可以在服務端上推拉資料

inotifu安裝:

檢查服務端的rsync服務是正常啟用的,并且可以在用戶端往服務端推送檔案

檢查核心版本:uname -r

[root@localhost inotify]# ls

max_queued_events max_user_instances max_user_watches

[root@localhost inotify]# ll /proc/sys/fs/inotify

總用量 0

-rw-r--r--. 1 root root 0 6月 27 22:58 max_queued_events #生産中調大一些

-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_instances

-rw-r--r--. 1 root root 0 6月 27 22:58 max_user_watches #隊列大小,生産中調大500000000

然後從網際網路上下載下傳inotify源碼安裝包

下載下傳好後,解壓:tar xf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure --prefix=/usr/local/inotify-tools-3.14

make && make install

ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify

[root@localhost inotify]# ll /usr/local/inotify

總用量 4

drwxr-xr-x. 2 root root 43 6月 27 23:10 bin #inotify執行指令

drwxr-xr-x. 3 root root 25 6月 27 23:10 include #inotify所需頭檔案

drwxr-xr-x. 2 root root 4096 6月 27 23:10 lib #動态連結庫檔案

drwxr-xr-x. 4 root root 26 6月 27 23:10 share #幫助文檔

使用inotify來監控目錄檔案,當被監控的目錄發生變化會産生輸出:

監聽建立:create

監聽删除:delete

監聽寫入:close_write

檔案屬性:attrib

ps:當create和close_write同時使用時,建立檔案會被監控兩遍

======create,delete,close_write同時使用用逗号隔開=======

/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create /backup

rsync+inotify實時同步資料

建立腳本存放路徑:mkdir -p /server/scripts

進入腳本存放目錄:cd /server/scripts

[root@localhost scripts]# vim inotify.sh

#!/bin/bash``` #para host01=172.17.0.112 src=/backup dst=backup user=backup rsync_passfile=/etc/rsync.pwd inotify_home=/usr/local/inotify-tools-3.14/ #judge if [ ! -e "$src" ] [ ! -e "${rsync_passfile}" ] [ ! -e "${inotify_home}bin/inotifywait" ] [ ! -e "/usr/bin/rsync" ]; then echo "Check File and Folder" exit 9 fi ${inotify_home}/bin/inotifywait -mrq --timefmt '%y%m%d %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \ | while read file do 注釋:# rsync -avzP --delete --timout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1 cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 done exit 0

執行腳本檔案sh -x inotify.sh,觀察遠端共享檔案目錄

最後,按照需求将腳本添加到定時任務。

實時同步并發測試:(使用paste指令并列顯示)

[root@localhost backup]# seq 10 >a.log

[root@localhost backup]# echo {a..j}|tr " " "\n" >b.log

[root@localhost backup]# paste a.log b.log >c.log

[root@localhost backup]# cat c.log

1 a

2 b

3 c

4 d

5 e

6 f

7 g

8 h

9 i

10 j

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

ps:指令解釋:

1.seq按序排列

2.|tr 替換(\n是換行符)

3.paste按列合并

繼續閱讀