inotify+rsync實現實時同步部署


1.1.架構規划

1.1.1架構規划准備

服務器系統 角色 IP
Centos6.7 x86_64 NFS服務器端(NFS-server-inotify-tools) 192.168.1.14
Centos6.7 x86_64 rsync服務器端(rsync熱備服務器) 192.168.1.17

 

 

 

1.1.2架構圖

 

2.1 部署前檢查

2.1.1 檢查rsync熱備服務器daemon是否起來

1 [root@backup data]# lsof -i:873
2 COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
3 rsync   3071 root    3u  IPv4  18684      0t0  TCP *:rsync (LISTEN)
4 rsync   3071 root    5u  IPv6  18685      0t0  TCP *:rsync (LISTEN)
5 [root@backup data]# ps -ef|grep rsync|grep -v grep
6 root      3071     1  0 Dec02 ?        00:00:00 rsync --daemon
7 [root@backup data]# 

2.1.2 NFS服務器相對於rsync服務器來說是客戶端,所以要配置一個客戶端密碼

1 [root@nfs-server ~]# echo "zhurui" >>/etc/rsync.password
2 [root@nfs-server ~]# cat /etc/rsync.password 
3 zhurui
4 [root@nfs-server ~]# chmod 600 /etc/rsync.password 
5 [root@nfs-server ~]# ll /etc/rsync.password 
6 -rw-------. 1 root root 7 Jan 31 00:49 /etc/rsync.password
7 [root@nfs-server ~]# 

2.1.3 NFS客戶端手動推/data/目錄下數據到rsync熱備服務器/backup目錄下

 1 [root@nfs-server data]# showmount -e localhost ##檢查共享目錄
 2 Export list for localhost:
 3 /data 192.168.1.0/24
 4 [root@nfs-server ~]# cd /data/
 5 [root@nfs-server data]# ll
 6 total 12
 7 -rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 03:05 beautiful.txt
 8 -rw-r--r--. 1 nfsnobody nfsnobody    9 Jan 30 04:33 f11.txt
 9 -rw-r--r--. 1 root      root         0 Jan 30 04:41 fs.txt
10 drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 03:05 huang
11 -rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 03:17 reew.txt
12 -rw-r--r--. 1 root      root         0 Jan 30 02:51 test1.txt
13 -rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 04:41 wd.txt
14 drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 02:57 zhurui
15 -rw-r--r--. 1 nfsnobody nfsnobody    0 Jan 30 02:47 zhurui1.txt
16 -rw-r--r--. 1 nfsnobody root         0 Jan 30 02:10 zhurui.txt
17 [root@nfs-server /]# rsync -avz /data/ rsync_backup@192.168.1.17::backup/ --password-file=/etc/rsync.password   ##推送數據命令
18 sending incremental file list
19 ./
20 beautiful.txt
21 f11.txt
22 fs.txt
23 reew.txt
24 test1.txt
25 wd.txt
26 zhurui.txt
27 zhurui1.txt
28 huang/
29 zhurui/
30 
31 sent 572 bytes  received 171 bytes  114.31 bytes/sec
32 total size is 9  speedup is 0.01
33 [root@nfs-server /]# 

2.1.4  rsync服務器端檢查/backup目錄下數據

 

3.1 服務部署

3.1.1 NFS服務端安裝inotify工具

inofity-tools下載地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

1)查看當前系統是否支持inotify

1 [root@nfs-server tools]# uname -r   ##內核版本,從kernel2.6.13開始支持
2 2.6.32-573.el6.x86_64   
3 [root@nfs-server tools]# ls -l /proc/sys/fs/inotify/  ##查看是否支持
4 total 0
5 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_queued_events
6 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_instances
7 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_watches
8 ##顯示當前三個文件表示支持

2)下載inotify源碼包

1 [root@nfs-server tools]# ll
2 total 352
3 -rw-r--r--. 1 root root 358772 Mar 14  2010 inotify-tools-3.14.tar.gz

3)編譯安裝inotify

 

1 [root@nfs-server tools]# tar zxf inotify-tools-3.14.tar.gz 
2 [root@nfs-server tools]# cd inotify-tools-3.14
3 [root@nfs-server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
4 [root@nfs-server inotify-tools-3.14]# make&&make install
 1 [root@nfs-server inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools  ##創建軟鏈接
 2 [root@nfs-server inotify-tools-3.14]# cd ..
 3 [root@nfs-server local]# ll
 4 total 44
 5 drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
 6 drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
 7 drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
 8 drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
 9 lrwxrwxrwx. 1 root root   30 Jan 31 01:58 inotify-tools -> /usr/local/inotify-tools-3.14/
10 drwxr-xr-x. 6 root root 4096 Jan 31 01:55 inotify-tools-3.14
11 drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
12 drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib64
13 drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
14 drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
15 drwxr-xr-x. 5 root root 4096 Nov 25 02:40 share
16 drwxr-xr-x. 2 root root 4096 Sep 23  2011 src
17 [root@nfs-server local]# cd inotify-tools
18 [root@nfs-server inotify-tools]# ll
19 total 16
 
         

提示編譯成功后會生成4個目錄,分別是:

 

1 [root@nfs-server /]# cd /usr/local/inotify-tools
2 [root@nfs-server inotify-tools]# ll
3 total 16
4 drwxr-xr-x. 2 root root 4096 Jan 31 01:55 bin    ##inotify執行命令(二進制)
5 drwxr-xr-x. 3 root root 4096 Jan 31 01:55 include   ##inotify程序所需用的頭文件
6 drwxr-xr-x. 2 root root 4096 Jan 31 01:55 lib  ##動態鏈接的庫文件
7 drwxr-xr-x. 4 root root 4096 Jan 31 01:55 share  ##幫助文檔

 

工具集合介紹:

一共安裝了2個工具(命令),即inotifywait和inotifywatch

inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open、close、delete等)發生,執行后處於阻塞狀態,適合在shell腳本中使用。

inotifywatch:收集被監視的文件系統使用度統計數據,指定文件系統事件發生的次數統計。

4)inotify命令常用參數詳解

 

 1 [root@nfs-server inotify-tools]# ./bin/inotifywait --help
 2 inotifywait 3.14
 3 Wait for a particular event on a file or set of files.
 4 Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
 5 Options:
 6  -r|--recursive  Watch directories recursively.  ##遞歸查詢目錄
 7  -q|--quiet      Print less (only print events)   ##打印很少的信息,僅僅打印監控相關的信息
 8 -m|--monitor    Keep listening for events forever.  Without
 9                         this option, inotifywait will exit after one
10                         event is received.   ##始終保持事件監聽狀態
11 --excludei <pattern>
12                         Like --exclude but case insensitive.   ##排除文件或目錄時,不區分大小寫
13 --timefmt <fmt> strftime-compatible format string for use with
14                         %T in --format string.   ##指定時間的輸出格式

 

 

5)測試監控事件

開啟兩個窗口

1.測試create

 

 1 第一步:
 2 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data  ##實行當前命令后,界面處於阻塞狀態,只有在另外一個客戶端測試時,才會顯示監控狀態
 3 31/01/16 10:23 /data/ceshi.txt
 4 31/01/16 10:24 /data/what
 5 31/01/16 10:27 /data/a
 6 31/01/16 10:27 /data/b
 7 31/01/16 10:27 /data/c
 8 31/01/16 10:27 /data/d
 9 31/01/16 10:27 /data/e
10 31/01/16 10:27 /data/f
11 
12 第二步:
13 克隆一個NFS客戶端切換到/data目錄下,新建ceshi.txt、what文件,觀察監控的變化
14 [root@nfs-server data]# touch ceshi.txt
15 [root@nfs-server data]# mkdir /who
16 [root@nfs-server data]# touch what
17 [root@nfs-server data]# 
18 [root@nfs-server data]# touch {a..f}

 

2、測試delete

 

1 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data   ##監控/data目錄刪除時間
2 
3 監控如下:
4 
5 31/01/16 10:31 /data/reew.txt
6 31/01/16 10:31 /data/test1.txt
7 
8 [root@nfs-server data]# rm -f reew.txt test1.txt   ##刪除這兩個文件
9 [root@nfs-server data]# 

 

3、同時測試create、delete

 1 第一步:
 2 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /data
 3 監控輸出信息:
 4 31/01/16 10:35 /data/hero.sh
 5 31/01/16 10:35 /data/a
 6 31/01/16 10:35 /data/f
 7 31/01/16 10:36 /data/b
 8 31/01/16 10:36 /data/c
 9 31/01/16 10:36 /data/d
10 31/01/16 10:36 /data/e
11 第二步:
12 [root@nfs-server data]# touch hero.sh
13 [root@nfs-server data]# ll
14 [root@nfs-server data]# rm -f [a..f]
15 [root@nfs-server data]# rm -f {a..f}
16 [root@nfs-server data]# 

一般工作中使用到:[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data

工作環境中通過腳本實時同步:

 

 1 #!/bin/bash
 2 inotify=/usr/local/inotify-tools/bin/inotifywait
 3 $inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data
 4  \
 5 |while read file
 6 do
 7   cd / &&
 8   rsync -az /data --delete rsync_backup@192.168.1.17::backup \
 9   --password-file=/etc/rsync.password
10 done

 

測試:

 

 1 腳本屬於開啟狀態:
 2 [root@nfs-server scripts]# sh -x inotify.sh 
 3 + inotify=/usr/local/inotify-tools/bin/inotifywait
 4 + read file
 5 + /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data
 6 + cd /
 7 + rsync -az /data --delete rsync_backup@192.168.1.17::backup --password-file=/etc/rsync.password
 8 + read file
 9 + cd /
10 + rsync -az /data --delete rsync_backup@192.168.1.17::backup --password
11 接着在NFS客戶端/data目錄下創建文件
12 [root@nfs-server data]# touch {a..z}
13 [root@nfs-server data]# echo i am best >a  ##修改a中的內容,查看熱備服務器是否同步
14 
15 rsync熱備服務器端顯示如下:
16 [root@backup //]# cd /backup/
17 [root@backup backup]# cd data/
18 [root@backup data]# ll
19 total 0
20 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 a
21 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 b
22 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 c
23 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 d
24 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 e
25 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 f
26 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 g
27 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 h
28 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 i
29 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 j
30 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 k
31 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 l
32 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 m
33 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 n
34 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 o
35 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 p
36 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 q
37 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 r
38 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 s
39 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 t
40 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 u
41 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 v
42 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 w
43 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 x
44 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 y
45 -rw-r--r--. 1 rsync rsync 0 Mar  4  2016 z
46 [root@backup data]# cat a
47 i am best   ##顯示已經實時同步過來
48 [root@backup data]# 

 

6)優化

1 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches 
2 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events 

結論:經過測試,每秒200個文件並發,數據同步幾乎無延遲(小於1秒)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM