1.1 什么是實時同步:如何實現實時同步
要利用監控服務(inotify),監控同步數據服務器目錄中信息的變化
發現目錄中數據產生變化,就利用rsync服務推送到備份服務器上
1.2 實現實時同步的方法
inotify+rsync 方式實現數據同步
sersync 方式實現實時數據同步 詳情參照:sersync實現數據實時同步的方法
1.2.1 實時同步原理介紹
1.3 inotify+rsync 方式實現數據同步
1.3.1 Inotify簡介
Inotify是一種強大的,細粒度的。異步的文件系統事件監控機制,linux內核從2.6.13起,加入了 Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而 inotify-tools 正是實施這樣監控的軟件。國人周洋在金山公司也開發了類似的實時同步軟件sersync。
提示信息:
sersync軟件實際上就是在 inotify軟件基礎上進行開發的,功能要更加強大些 ,多了定時重傳機制,過濾機制了提供接口做 CDN,支持多線程橾作。
Inotify實際是一種事件驅動機制,它為應用程序監控文件系統事件提供了實時響應事件的機制,而無須通過諸如cron等的輪詢機制來獲取事件。cron等機制不僅無法做到實時性,而且消耗大量系統資源。相比之下,inotify基于事件驅動,可以做到對事件處理的實時響應,也沒有輪詢造成的系統資源消耗,是非常自然的事件通知接口,也與自然世界事件機制相符合。
inotify的實現有幾款軟件:
inotify-tools,sersync,lrsyncd
1.3.2 inotify+rsync使用方式
inotify 對同步數據目錄信息的監控
rsync 完成對數據信息的實時同步
利用腳本進行結合
1.4 部署inotify軟件的前提
需要2.6.13以后內核版本才能支持inotify軟件。2.6.13內核之后版本,在沒有安裝inotify軟件之前,應該有這三個文件。
[root@backup ~]# ll /proc/sys/fs/inotify/total 0-rw-r--r-- 1 root root 0 Oct 17 10:12 max_queued_events-rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_instances-rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_watches
1.4.1 三個重要文件的說明
文件 | 默認值 | 作用說明 |
max_user_watches | 8192 | 設置inotifywait或inotifywatch命令可以監視的文件數量(單進程) |
max_user_instances | 128 | 設置每個用戶可以運行的inotifywait或inotifywatch命令的進程數 |
max_queued_events | 16384 | 設置inotify實例事件(event)隊列可容納的事件數量 |
1.4.3 【官方說明】三個重要文件1.4.2 【服務優化】可以將三個文件的數值調大,監聽更大的范圍
[root@nfs01 ~]# man proc/proc/sys/fs/inotify (since Linux 2.6.13) This directory contains files max_queued_events, max_user_instances, and max_user_watches, that can be used to limit the amount of kernel memory consumed by the inotify interface. for further details, see inotify(7).
通過man手冊的第7級別中查到 inotify的默認文件的詳細說明。
[root@nfs01 ~]# man 7 inotify/proc/sys/fs/inotify/max_queued_events The value in this file is used when an application calls inotify_init(2) to set an upper limit on the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated./proc/sys/fs/inotify/max_user_instances This specifies an upper limit on the number of inotify instances that can be created per real user ID./proc/sys/fs/inotify/max_user_watches This specifies an upper limit on the number of watches that can be created per real user ID.
1.5 inotify軟件介紹及參數說明
1.5.1 兩種安裝方式
1) yum install -y inotify-tools
2) 手工編譯安裝
注:
YUM 安裝需要有epel源
http://mirrors.aliyun.com
手工編譯安裝方式需要到github上進行下載軟件包
inotify軟件的參考資料鏈接:
https://github.com/rvoicilas/inotify-tools/wiki
1.5.2 inotify主要安裝的兩個軟件
inotifywait: (主要)
在被監控的文件或目錄上等待特定文件系統事件(open close delete等)發生,執行后處于阻塞狀態,適合在shell腳本中使用
inotifywatch:
收集被監控的文件系統使用的統計數據,指文件系統事件發生的次數統計。
說明:在實時實時同步的時候,主要是利用inotifywait對目錄進行監控
1.5.3 inotifywait命令參數說明
參數 | 含義 |
-m, --monitor (重要參數) | Keep listening for events forever. Without this option, inotifywait will exit after one event is received. 始終保持事件監聽。 |
-d, --daemon | 111 |
-r, --recursive (重要參數) | Watch all subdirectories of any directories passed as arguments. 遞歸監控目錄數據信息變化 |
-o, --outfile <file> | Print events to <file> rather than stdout. 打印事件到文件中,相當于標準正確輸出 |
-s, --syslog | Output errors to syslog(3) system log module rather than stderr. 發送錯誤到syslog相當于標準錯誤輸出 |
-q, --quiet (重要參數) | If specified once, the program will be less verbose. Specifically, it will not state when it has completed establishing all inotify watches. 輸出信息少(只打印事件信息) |
--exclude <pattern> | Exclude all events on files matching the extended regular expression <pattern>. 排除文件或目錄 |
--excludei <pattern> | Like --exclude but case insensitive. 排除文件或目錄時,不區分大小寫 |
--timefmt <fmt> (重要參數) | Print using a specified printf-like format string; read the man page for more details. 指定時間輸出格式 |
--format <fmt> (重要參數) | Print using a specified printf-like formatstring; read the man page for more details. 打印使用指定的輸出類似格式字符串;即實際監控輸出內容 |
-e (重要參數) | Listen for specific event(s). If omitted, all events are listened for. 指定監聽指定的事件,如果省略,表示所有事件都進行監聽 |
以上的信息可以通過 inotifywait --help 獲得 |
事件名稱 | 事件說明 |
access | file or directory contents were read 文件或目錄內容被讀取 |
modify | file or directory contents were writterv 文件或目錄內容被寫入 |
attrib | file or directory attributes changed 文件或目錄屬性改變 |
close_write (重要參數) | file or directory closed, after being opened in writeable mode. 文件或目錄關閉,在寫入模式打開之后關閉的。 |
close_nowrite | file or directory closed, after being opened in read-only mode. 文件或目錄關閉,在只讀模式打開之后關閉的 |
close | file or directory closed, regardless of read/write mode 文件或目錄關閉,不管讀或是寫模式 |
open | file or directory opened 文件或目錄被打開 |
moved_to 拉 | file or directory moved to watched directory 文件或目錄被移動到監控的目錄中 |
moved_from 推 | file or directory moved from watched directory 文件或目錄被移動從監控的目錄中 |
move (重要參數) | file or directory moved to or from watched directory 文件或目錄不管移動到或是移出監控目錄都觸發事件 |
create (重要參數) | file or directory created within watched directory 文件或目錄創建在監控目錄中 |
delete (重要參數) | file or directory deleted within watched directory 文件或目錄被刪除在監控目錄中 |
delete_self | file or directory was deleted 文件或目錄被刪除,目錄本身被刪除 |
unmount | file system containing file or directory unmounted |
以上的信息可以通過 inotifywait --help 獲得 |
1.5.4.1 【實例】inotifywait監控中的事件測試
1、創建事件
[root@nfs01 data]# touch test2.txt[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e create17-10-17 11:19 /data/test2.txt 事件信息: CREATE
2、刪除事件
[root@nfs01 data]# /rm -f test1.txt[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e delete17-10-17 11:28 /data/test1.txt 事件信息: DELETE
3、修改事件
[root@nfs01 data]# echo "132" > test.txt[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e close_write17-10-17 11:30 /data/test.txt 事件信息: CLOSE_WRITE,CLOSE
4、移動事件 moved_to
[root@nfs01 data]# mv /etc/hosts .[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_to17-10-17 11:33 /data/hosts 事件信息: MOVED_TO
5、移動事件 moved_from
[root@nfs01 data]# mv ./hosts /tmp/[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from17-10-17 11:34 /data/hosts 事件信息: MOVED_FROM
1.5.5 inotifywait 參數 --format <fmt>格式定義參數
命令參數 | 參數說明 |
%w(重要參數) | 事件出現時,監控文件或目錄的名稱信息 |
%f(重要參數) | 事件出現時,將顯示監控目錄下觸發事件的文件或目錄信息,否則為空 |
%e(重要參數) | 顯示發生的事件信息,不同的事件信息用逗號進行分隔 |
%Xe | 顯示發生的事件信息,不同的事件信息有x進行分隔,可以修改X為指定分隔符 |
%T(重要參數) | 輸出時間格式中定義的時間格式信息,通過 --timefmt option 語法格式指定時間信息 這個格式是通過strftime函數進行匹配時間格式信息的 |
以上的信息可以通過 inotifywait --help 獲得 |
1.5.6 inotifywait 參數--timefmt <fmt>時間格式參數
命令參數 | 參數說明 |
%d(重要參數) | The day of the month as a decimal number(range 01 to 31) 每月的第幾天,顯示倍息為十進制數(范圍是 01-31 ) |
%m(重要參數) | The month as a decimal number (range 01 to 12). 顯示月份,顯示信息為十進制(范圍 01-12 ) |
%M | The minute as a decimal number (range 00 to 59). 顯示分鐘,顯示信息為十進制(范圍 00-59 ) |
%y(重要參數) | The year as a decimal number without a century (range 00 to 99). 年份信息,顯示信息為十進制,并且沒有世紀信息 |
%Y | The year as a decimal number including the century. 年份信息,顯示信息為十進制,并且包含世紀信息 |
%H | The hour as a decimal number using a 24-hour clock (range 00 to 23). 小時信息,顯示信息為十進制,使用 24小時制(范圍 00-23 ) |
說明:以上信息可以通過 man strftime信息獲取 |
1.5.6.1 修改輸出的日期格式
[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f"17/10/17 11:12 /data/test1.txt
1.5.7 -e[參數] 重要監控事件參數匯總表: