inotify+rsync 方式實現數據同步linux
sersync 方式實現實時數據同步 詳情參照:http://www.cnblogs.com/clsn/p/7707828.htmlgit
Inotify是一種強大的,細粒度的。異步的文件系統事件監控機制,linux內核從2.6.13起,加入了 Inotify支持,經過Inotify能夠監控文件系統中添加、刪除,修改、移動等各類事件,利用這個內核接口,第三方軟件就能夠監控文件系統下文件的各類變化狀況,而 inotify-tools 正是實施這樣監控的軟件。國人周洋在金山公司也開發了相似的實時同步軟件sersync。github
提示信息:shell
sersync軟件實際上就是在 inotify軟件基礎上進行開發的,功能要更增強大些 ,多了定時重傳機制,過濾機制了提供接口作 CDN,支持多線程橾做。express
Inotify實際是一種事件驅動機制,它爲應用程序監控文件系統事件提供了實時響應事件的機制,而無須經過諸如cron等的輪詢機制來獲取事件。cron等機制不只沒法作到實時性,並且消耗大量系統資源。相比之下,inotify基於事件驅動,能夠作到對事件處理的實時響應,也沒有輪詢形成的系統資源消耗,是很是天然的事件通知接口,也與天然世界事件機制相符合。vim
inotify的實現有幾款軟件:安全
inotify-tools,sersync,lrsyncd
inotify 對同步數據目錄信息的監控bash
rsync 完成對數據信息的實時同步服務器
利用腳本進行結合
須要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
文件 |
默認值 |
做用說明 |
max_user_watches |
8192 |
設置inotifywait或inotifywatch命令能夠監視的文件數量(單進程) |
max_user_instances |
128 |
設置每一個用戶能夠運行的inotifywait或inotifywatch命令的進程數 |
max_queued_events |
16384 |
設置inotify實例事件(event)隊列可容納的事件數量 |
[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) yum install -y inotify-tools
2) 手工編譯安裝
注:
YUM 安裝須要有epel源
http://mirrors.aliyun.com
手工編譯安裝方式須要到github上進行下載軟件包
inotify軟件的參考資料連接:
https://github.com/rvoicilas/inotify-tools/wiki
inotifywait: (主要)
在被監控的文件或目錄上等待特定文件系統事件(open close delete等)發生,執行後處於阻塞狀態,適合在shell腳本中使用
inotifywatch:
收集被監控的文件系統使用的統計數據,指文件系統事件發生的次數統計。
說明:在實時實時同步的時候,主要是利用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 得到 |
一、建立事件
[root@nfs01 data]# touch test2.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e create 17-10-17 11:19 /data/test2.txt 事件信息: CREATE
二、刪除事件
[root@nfs01 data]# \rm -f test1.txt [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e delete 17-10-17 11:28 /data/test1.txt 事件信息: DELETE
三、修改事件
[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_write 17-10-17 11:30 /data/test.txt 事件信息: CLOSE_WRITE,CLOSE
四、移動事件 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_to 17-10-17 11:33 /data/hosts 事件信息: MOVED_TO
五、移動事件 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_from 17-10-17 11:34 /data/hosts 事件信息: MOVED_FROM
命令參數 |
參數說明 |
%w(重要參數) |
事件出現時,監控文件或目錄的名稱信息 |
%f(重要參數) |
事件出現時,將顯示監控目錄下觸發事件的文件或目錄信息,不然爲空 |
%e(重要參數) |
顯示發生的事件信息,不一樣的事件信息用逗號進行分隔 |
%Xe |
顯示發生的事件信息,不一樣的事件信息有x進行分隔,能夠修改X爲指定分隔符 |
%T(重要參數) |
輸出時間格式中定義的時間格式信息,經過 --timefmt option 語法格式指定時間信息 這個格式是經過strftime函數進行匹配時間格式信息的 |
以上的信息能夠經過 inotifywait --help 得到 |
命令參數 |
參數說明 |
%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信息獲取 |
[root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f" 17/10/17 11:12 /data/test1.txt
重要事件 |
包含事件 |
備註說明 |
close |
close_write close_nowrite |
文件或目錄關閉,無論讀或是寫模式 即包含寫關閉與讀關閉 |
close_write |
create |
包含文件建立事件,但不包含目錄建立事件 |
move |
moved_to moved_from |
文件或目錄無論移動到或是移動出監控目錄都觸發事件 即包含信息移入或移出監控目錄事件 |
重要參數彙總:根據以上說明,在實際使用時,只要監控如下事件便可 create 建立、 delete 刪除、 movedjto 移入、 close_write 修 改 inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write |
對inotifywait命令測試的說明:
須要打開兩個鏈接窗口
1窗口運行inotifywait
2窗口對文件夾進行操做,可在一窗口中查看出inotifywait的監控記錄
[root@nfs01 ~]# inotifywait /data Setting up watches. Watches established. /data/ CREATE test1.txt /data/ OPEN test1.txt /data/ ATTRIB test1.txt /data/ CLOSE_WRITE,CLOSE test1.txt 建立文件,inotifywait顯示建立文件的過程↑ [root@nfs01 data]# touch test1.txt
[root@nfs01 data]# mkdir testdir [root@nfs01 ~]# /data/ CREATE,ISDIR testdir
[root@nfs01 data]# touch testdir/test01.txt [root@nfs01 ~]# inotifywait -mrq /data /data/testdir/ OPEN test01.txt /data/testdir/ ATTRIB test01.txt /data/testdir/ CLOSE_WRITE,CLOSE test01.txt
[root@nfs01 data]# sed 's#132#123#g' test.txt -i [root@nfs01 ~]# inotifywait -mrq /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from /data/test.txt 事件信息: OPEN /data/sedDh5R8v 事件信息: CREATE /data/sedDh5R8v 事件信息: OPEN /data/test.txt 事件信息: ACCESS /data/sedDh5R8v 事件信息: MODIFY /data/sedDh5R8v 事件信息: ATTRIB /data/sedDh5R8v 事件信息: ATTRIB /data/test.txt 事件信息: CLOSE_NOWRITE,CLOSE /data/sedDh5R8v 事件信息: CLOSE_WRITE,CLOSE /data/sedDh5R8v 事件信息: MOVED_FROM /data/test.txt 事件信息: MOVED_TO
sed命令替換邏輯 :
01. 建立臨時文件
02. 將原文件內容放置到臨時文件中,修改替換臨時文件中的內容,原有文件不作改動
03. 重命名臨時文件,覆蓋原文件
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e" -e create
說明:表示只監聽create事件
inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e"
說明:不指定-e參數,表示監聽全部事件
02. 刪除事件delete
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete 2017-10-17 11:28 /data/02.txt 事件信息: DELETE 2017-10-17 11:28 /data/03.txt 事件信息: DELETE 2017-10-17 11:28 /data/04.txt 事件信息: DELETE
03. 修改事件close_write
# inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write 2017-10-17 11:30 /data/oldgirl.txt 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE 2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE
04. 移動事件moved_to
inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write,moved_to 2017-10-17 11:34 /data/hosts 事件信息: MOVED_TO
1)軟件是否存在
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
需求:查詢到某個命令很是有用。可是不知道屬於哪一個軟件包
yum provides rysnc
provides Find what package provides the given value
2)進行軟件服務配置
[root@backup ~]# vim /etc/rsyncd.conf uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = "backup dir by oldboy" path = /backup [nfsbackup] comment = "nfsbackup dir by hzs" path = /nfsbackup
3)建立rsync管理用戶
[root@backup ~]# useradd -s /sbin/nologin -M rsync
4)建立數據備份儲存目錄,目錄修改屬主
[root@backup ~]# mkdir /nfsbackup/ [root@backup ~]# chown -R rsync.rsync /nfsbackup/
5)建立認證用戶密碼文件並進行受權600
echo "rsync_backup:clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
6)啓動rsync服務
rsync --daemon
至此服務端配置完成
[root@backup ~]# ps -ef |grep rsync root 2076 1 0 17:05 ? 00:00:00 rsync --daemon root 2163 1817 0 17:38 pts/1 00:00:00 grep --color=auto rsync
1)軟件是否存在
[root@backup ~]# rpm -qa |grep rsync rsync-3.0.6-12.el6.x86_64
2)建立安全認證文件,並進行修改權限600
echo "clsn123" >>/etc/rsync.password chmod 600 /etc/rsync.password
3) 測試數據傳輸
[root@nfs01 sersync]# rsync -avz /data rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password sending incremental file list data/ data/.hzs data/.tar.gz data/.txt
首先先確認是否有epel源用來安裝inotify-tools軟件
[root@nfs01 ~]# yum repolist Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base CentOS-6 - Base - mirrors.aliyun.com 6,706 epel Extra Packages for Enterprise Linux 6 - x86_64 12,401 extras CentOS-6 - Extras - mirrors.aliyun.com 46 updates CentOS-6 - Updates - mirrors.aliyun.com 722 repolist: 19,875
兩種安裝方式
1) yum install -y inotify-tools
2) 手工編譯安裝
注:
手工編譯安裝方式須要到github上進行下載軟件包
inotify軟件的參考資料連接:
https://github.com/rvoicilas/inotify-tools/wiki
[root@nfs01 ~]# rpm -ql inotify-tools /usr/bin/inotifywait #主要 /usr/bin/inotifywatch
一共安裝了2個工具(命令),即inotifywait和inotifywatch
inotifywait : 在被監控的文件或目錄上等待特定文件系統事件(open close delete等)發生,
執行後處於阻塞狀態,適合在shell腳本中使用
inotifywatch :收集被監控的文件系統使用的統計數據,指文件系統事件發生的次數統計。
說明:yum安裝後能夠直接使用,若是編譯安裝須要進入到相應軟件目錄的bin目錄下使用
#命令 man手冊說明 # man inotifywait inotifywait - wait for changes to files using inotify 使用inotify進行監控,等待產生變化的文件信息 # man inotifywatch inotifywatch - gather filesystem access statistics using inotify 使用inotify進行監控,收集文件系統訪問統計佶息
rsync -avz --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
inotifywait -mrq /data -format "%w%f" -e create,delete,move_to,close_write
[root@nfs01 sersync]# vim /server/scripts/inotify.sh #!/bin/bash inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|\ while read line do rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password- file=/etc/rsync.password done
腳本說明:
for循環會定義一個條件,當條件不知足時中止循環
while循環:只要條件知足就一直循環下去
#!/bin/bash Path=/data backup_Server=172.16.1.41 /usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line do if [ -f $line ];then rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password else cd $Path &&\ rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password fi done
在/data 目錄先建立6個文件
[root@nfs01 data]# sh /server/scripts/inotify.sh & [root@nfs01 data]# touch {1..6}.txt
在backup服務器上,已經時候同步過去了6個文件。
[root@backup ~]# ll /nfsbackup/ total 8 -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt -rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt
01. ctrl+z暫停程序運行,kill -9殺死
02. 不要暫停程序,直接利用殺手三劍客進行殺進程
說明:kill三個殺手不是萬能的,在進程暫停時,沒法殺死;kill -9 (危險)
[root@nfs01 data]# jobs [1]+ Running sh /server/scripts/inotify.sh &
[root@nfs01 data]# fg 1 sh /server/scripts/inotify.sh
fg -- 前臺
bg -- 後臺
01. sh inotify.sh &
02. nohup sh inotify.sh &
03. screen實現腳本程序後臺運行
sh /server/scripts/inotify.sh & nohup nohup sh inotify.sh &
[root@test ~]# yum provides screen Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.7 kB 00:00 epel | 4.3 kB 00:00 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on : one terminal Repo : base Matched from:
[root@test ~]# yum install -y screen
在shell中輸入 screen便可進入screen 視圖
[root@test ~]# screen
Screen實現後臺運行程序的簡單步驟:
screen -ls :可看screen會話
screen +a d :退出當前的screen,保持其後臺運行
screen -r ID :指定進入哪一個screen會話
Screen命令中用到的快捷鍵
Ctrl+a c :建立窗口
Ctrl+a w :窗口列表
Ctrl+a n :下一個窗口
Ctrl+a p :上一個窗口
Ctrl+a 0-9 :在第0個窗口和第9個窗口之間切換
Ctrl+a K(大寫) :關閉當前窗口,而且切換到下一個窗口 ,
(當退出最後一個窗口時,該終端自動終止,而且退回到原始shell狀態)
exit :關閉當前窗口,而且切換到下一個窗口
(當退出最後一個窗口時,該終端自動終止,而且退回到原始shell狀態)
Ctrl+a d :退出當前終端,返回加載screen前的shell命令狀態
Ctrl+a " : 窗口列表不一樣於w
提示信息:
sersync軟件實際上就是在 inotify軟件基礎上進行開發的,功能要更增強大些 ,多了定時重傳機制,過濾機制了提供接口作 CDN,支持多線程橾做。
本文出自「慘綠少年」,歡迎轉載,轉載請註明出處!http://blog.znix.top
個人博客即將同步至騰訊雲+社區,邀請你們一同入駐。