1. 程式人生 > >inotify+rsync實時備份總結

inotify+rsync實時備份總結

inotify+rsync實時同步

第1章 inotify簡介

1.1 rsync+inotify組合的起源
Rsync(remote sync)遠程同步工具,通過rsync可以實現對遠程服務器數據增量備份同步,但是rsync自身也有瓶頸,同步數據時,rsync采用核心算法對遠程服務器的目標文件進行對比,只進行差異同步,如果服務器文件的數量達到百萬甚至是千萬量級,那麽文件對比將是非常耗時的,而且發生變化的往往只是其中很少的一部分,這是非常低效的方式,Inotify的出現,可以緩解rsync的不足之處,取長補短
1.2 Inotify介紹
Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了inotify支持,通過Inotify可以監控文件系統添加、刪除、移動、修改等各種事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況。而inotify-tools正是實施這樣監控的軟件

Inotify實際是始終事件驅動機制,它為應用程序監控文件系統事件提供了實時響應事件的機制,而無需通過諸如cron等輪詢機制來獲取事件。Cron等機制不僅無法做到實時性,而且耗費大量系統資源。相比之下,inotify基於事件驅動,可以做到對事件處理的實時響應,也沒有輪詢造成的系統資源消耗,是非常自然的事件通知接口,也與自然世界的事件機制相符合。
1.3 Inotify實現的幾款軟件
inotify-tools、sersync(國人金山周洋)、lsyncd

第2章 實時同步的搭建

2.1 實時同步的拓撲結構
技術分享圖片
2.2 inotify實施前的環境準備
2.2.1 特別註意事項
配置的大前提是rsync daemon服務配置成功

2.2.2 查看服務器內核版本是否支持
[root@nfs01 ~]# uname -r
2.6.32-696.el6.x86_64
2.2.2 服務器環境準備

操作系統 服務器角色 IP地址
CentOS release 6.9 x86_64 NFS服務端(nfs-server) 內網:172.16.1.31/24 外網:10.0.0.31/24
CentOS release 6.9 x86_64 backup服務端 內網:172.16.1.41/24 外網:10.0.0.41/24

? 註意:在實際的生產環境中是不要外網的
2.3 安裝inotify-tools
2.3.1 添加epel源
[root@nfs01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

註意:yum安裝inotify-tools時需要額外的源,所以要提前安裝epel源,否則是安裝不了的
2.3.1.1 epel是什麽
如果既想獲得RHEL的高質量、高性能、高可靠性,又需要方便易用(關鍵是免費)的軟件包更新功能,那麽Fedora Project退出的EPEL(Extra Packages for Enterprise linux)正好適合。
EPEL(http://fedoraproject.org/wiki/EPEL)是由Fedora社區打造,為RHEL及衍生發行版如Centos、Scientific Linux等提供高質量軟件包的項目
2.3.2 開始安裝inotify-tools並進行檢查
安裝:
[root@nfs01 ~]#yum install inotify-tools -y
查看:
[root@nfs01 ~]#rpm -qa inotify-tools
inotify-tools-3.14-1.el6.x86_64
2.3.3 工具集介紹
該軟件包共安裝了兩個工具(命令),即inotifywait和inotifywatch
inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open、close、delete等)發生,執行後處於阻塞狀態,適合在腳本中使用
inotifywatch:收集被監視的文件系統使用度統計數據,指文件系統事件發生的次數統計
2.3.4 inotifywait命令常用參數詳解

參數 說明 含義
-r --recursive 遞歸查詢目錄
-q --quiet 打印很少的信息,僅僅打印監控事件的信息
-m --monitor 始終保持事件監聽狀態
--format -- 打印使用指定的輸出類似格式字符串
--timefmt -- 指定事件輸出的格式
-e --event 通過此參數可以指定要監控的事件
access 文件或目錄被讀取
modify 文件或目錄的內容被修改
attrib 文件或目錄的屬性被改變
close 文件或目錄被封閉,無論讀/寫模式
open 文件或目錄被打開
move_to 文件或目錄被移動至另外一個目錄
move 文件或目錄被移動至另外一個目錄或從另一個目錄移動至當前目錄
create 文件或目錄被創建在當前目錄
delete 文件或目錄被刪除
umount 文件系統被卸載

? 可以用下面的命令可以看到上述參數的詳解
[root@nfs01 ~]# inotifywait –help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-‘ for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.

Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.

Events:
access file or directory contents were read
modify file or directory contents were written
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
2.4 人工測試監控工具
註意:測試需開啟兩個窗口
2.4.1 測試create
? a窗口執行如下命令:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
create /backup
? b窗口執行如下命令:
[root@nfs01 backup]# touch {a..f}
? a窗口監測變化:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
create /backup
180318 15:37 /backup/a
180318 15:37 /backup/b
180318 15:37 /backup/c
180318 15:37 /backup/d
180318 15:37 /backup/e
180318 15:37 /backup/f
技術分享圖片
2.4.2 測試delete
? a窗口執行如下命令:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
delete /backup
? b窗口執行如下命令:
[root@nfs01 backup]# rm -f a b c
? a窗口監測變化:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
delete /backup
180318 15:42 /backup/a
180318 15:42 /backup/b
180318 15:42 /backup/c
技術分享圖片
2.4.3 測試close_write
? a窗口執行如下命令:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
close_write /backup
? b窗口執行如下命令:
[root@nfs01 backup]# ls
back_2018-03-15.tar.gz d e f
[root@nfs01 backup]# rm -f d e f
[root@nfs01 backup]# touch close_wait.txt {a..c}.txt
[root@nfs01 backup]# ls
a.txt back_2018-03-15.tar.gz b.txt close_wait.txt c.txt
[root@nfs01 backup]# echo 123456 >a.txt
[root@nfs01 backup]# rm -f a
[root@nfs01 backup]# rm -f a.txt
[root@nfs01 backup]# echo 123456 >close_wait.txt
[root@nfs01 backup]# cat close_wait.txt
123456
[root@nfs01 backup]# ls
back_2018-03-15.tar.gz b.txt close_wait.txt c.txt
? a窗口監測變化:
技術分享圖片
2.4.4 同時測試create、modify、delete
? a窗口執行如下命令:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --timefmt ‘%d%m%y %H:%M‘ --format ‘%T %w%f‘ -e
delete,close_write,modify /backup/
? b窗口執行如下命令:
[root@nfs01 backup]# ls
back_2018-03-15.tar.gz b.txt close_wait.txt c.txt
[root@nfs01 backup]# rm -f .txt
[root@nfs01 backup]# touch {a..c}.txt
[root@nfs01 backup]# echo "hello world" >a.txt
[root@nfs01 backup]# echo "hello world" >b.txt
? a窗口監測變化:
技術分享圖片
2.4.4 簡化事件輸出
? a窗口執行如下命令:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --format ‘%w%f‘ -e create /backup
? b窗口執行如下命令:
[root@nfs01 backup]# ls
a.txt back_2018-03-15.tar.gz b.txt c.txt
[root@nfs01 backup]# rm -f
.txt
[root@nfs01 backup]# touch {a..c}.txt
[root@nfs01 backup]# echo 111 >d
[root@nfs01 backup]# echo 111 >a.txt
? a窗口監測變化:
[root@nfs01 ~]# /usr/bin/inotifywait -mrq --format ‘%w%f‘ -e create /backup
/backup/a.txt
/backup/b.txt
/backup/c.txt
/backup/d
技術分享圖片

第3章 inotify實時監控腳本

3.1 創建腳本存放目錄並查看
[root@nfs01 ~]# mkdir -p /server/scripts/
[root@nfs01 ~]# ll -d /server/scripts/
3.2 實時監控腳本

rsync 參數 src user@host::dst rsync_passdfile
rsync -avz /data [email protected]::nfslookup --password-file= /etc/rsync.password

3.3 腳本實例
3.3.1 腳本實例1


root@nfs01 ~]# vim /server/scripts/inotify.sh
#!/bin/sh
Path=/data
IP=172.16.1.41
/usr/bin/inotifywait -mrq --format ‘%w%f‘ -e close_write,delete $
Path \
|while read file
do
cd $Path &&\
rsync -az “$file” --delete rsync_backup@$IP::nfsbackup \
--password-file=/etc/rsync.password &
done
~
? 註意:此方法非常用方法(此方法為精準處理法之一)【此方法為無差異同步,慎用】
? \ 為換行符


3.3.2 腳本實例2


[root@nfs01 ~]# vim /server/scripts/inotify1.sh
#!/bin/bash
Path=/data
Ip=172.16.1.41
/usr/bin/inotifywait -mrq --format ‘%w%f‘ -e close_write,delete $
Path \
|while read file
do
if [ -f $file ];then
rsync -az $file --delete rsync_backup@$Ip::nfsbackup \
--password-file=/etc/rsync.password
else
cd $Path &&\
rsync -az ./ --delete rsync_backup@$Ip::nfsbackup \
--password-file=/etc/rsync.password
fi
done


3.4 腳本調試方法
3.4.1 客戶端執行如下命令
[root@nfs01 scripts]# sh +x /server/scripts/inotify.sh
3.4.2 backup服務端執行如下命令
[root@backup nfsbackup]# watch ls
3.4.3 測試無誤後在執行腳本的命令後加& 表示在後臺執行
[root@nfs01 scripts]# /bin/sh /server/scripts/inotify.sh &
# 第4章 inotify+rsync優化腳本
4.1 腳本實例


#!/bin/bash
#chkconfig: 2345 38 46
####################################
#this scripts is created by xxxx
####################################
. /etc/init.d/functions

if [ $# -ne 1 ];then
usage: $0 {start|stop}
exit 1
fi

case "$1" in
start)
/bin/bash /server/scripts/inotify.sh &
echo $$ >/var/run/inotify.pid
if [ ps -ef|grep inotify|wc -l -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 cat /var/run/inotify.pid >/dev/null 2>&1
pkill inotifywait
sleep 2
if [ ps -ef|grep inotify|grep -v grep|wc -l -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 {start|stop}
exit 1
esac


4.2 創建該腳本並查看
[root@nfs01 ~]# vim /etc/init.d/syncd
[root@nfs01 ~]# cat /etc/init.d/syncd
4.3 給該腳本賦予可執行權限
[root@nfs01 ~]# chmod +x /etc/init.d/syncd
4.4 加入開機自啟動並查看
[root@nfs01 ~]# chkconfig --add syncd
[root@nfs01 ~]# chkconfig --list syncd
syncd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4.5 執行腳本看是否成功
[root@nfs01 ~]# /etc/init.d/syncd start
inotify service is started [ OK ]
[root@nfs01 ~]# ps -ef |grep inotify
root 4636 1 0 16:45 pts/1 00:00:00 /bin/bash /server/scripts/inotify.sh
root 4641 4636 0 16:45 pts/1 00:00:00 /usr/bin/inotifywait -mrq --format %w%f -e close_write,delete /data
root 4642 4636 0 16:45 pts/1 00:00:00 /bin/bash /server/scripts/inotify.sh
root 4648 2157 0 16:45 pts/1 00:00:00 grep inotify
[root@nfs01 ~]# /etc/init.d/syncd stop
inotify service is stopped [ OK ]
[root@nfs01 ~]# ps -ef |grep inotify
root 4664 2157 0 16:45 pts/1 00:00:00 grep inotify
4.6 測試
4.6.1 在nfs客戶端建立測試文件
[root@nfs01 ~]# cd /data
[root@nfs01 data]# touch stu{01..100}
4.6.2 在backup服務端查看
[root@backup ~]# cd /nfsbackup/
[root@backup nfsbackup]# ls
stu001 stu008 stu015 stu022 stu029 stu036 stu043 stu050 stu057 stu064 stu071 stu078 stu085 stu092 stu099
stu002 stu009 stu016 stu023 stu030 stu037 stu044 stu051 stu058 stu065 stu072 stu079 stu086 stu093 stu100
stu003 stu010 stu017 stu024 stu031 stu038 stu045 stu052 stu059 stu066 stu073 stu080 stu087 stu094
stu004 stu011 stu018 stu025 stu032 stu039 stu046 stu053 stu060 stu067 stu074 stu081 stu088 stu095
stu005 stu012 stu019 stu026 stu033 stu040 stu047 stu054 stu061 stu068 stu075 stu082 stu089 stu096
stu006 stu013 stu020 stu027 stu034 stu041 stu048 stu055 stu062 stu069 stu076 stu083 stu090 stu097
stu007 stu014 stu021 stu028 stu035 stu042 stu049 stu056 stu063 stu070 stu077 stu084 stu091 stu098
技術分享圖片

上述情況說名測試成功

第5章 /proc/sys/fs/inotify/目錄詳解

5.1 /proc/sys/fs/inotify/目錄下的文件
/proc/sys/fs/inotify/目錄下共有三個文件,對inotify機制有一定的限制
[root@nfs01 inotify]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Mar 18 13:03 max_queued_events
-rw-r--r-- 1 root root 0 Mar 18 09:32 max_user_instances
-rw-r--r-- 1 root root 0 Mar 18 13:03 max_user_watches
5.2三個文件的說明
? max_queued_events:設置inotify實例事件(event)隊列可容納的事件數量
? max_user_instances:設置每個用戶可以運行的inotifywait或inotifywatch命令的進程數
? max_user_watches:設置inotifywait和inotifywatch命令可以監視的文件數量(單進程)
5.3 inotify事件相關參數的默認大小
[root@nfs01 inotify]# cat max_queued_events max_user_instances max_user_watches
327679
128
50000000
5.4 實戰調整
[root@nfs01 ~]# echo 655350 >/proc/sys/fs/inotify/max_user_watches
[root@nfs01 ~]# echo 655350 >/proc/sys/fs/inotify/max_queued_events
[root@nfs01 ~]# cat /proc/sys/fs/inotify/max_queued_events
655350
[root@nfs01 ~]# cat /proc/sys/fs/inotify/max_user_watches
655350
? 註意:echo 655350 >/proc/sys/fs/inotify/xxx 設置參數重啟會失效,因此想要永久需要追加到/etc/rc.local中

第6章 rsync+inotify實時數據同步並發簡單測試

測試腳本為sersync中的腳本
10k-100k的文件
每秒100個並發
技術分享圖片
? 結論:經過測試,每秒200個並發文件,數據同步幾乎無延遲(小於1秒)

第7章 inotify的優缺點

7.1 優點
? 監控文件系統事件變化,通過同步工具實現實時數據同步
7.2 缺點
? 並發如果大於200個文件(10k-100k),同步就會有延遲
? 監控到事件後,調用rsync同步是單進程的
? 上文寫的腳本,每次都是全部推送一次,但是確實是增量的,也可以只同步變化的文件,不變化的不理

inotify+rsync實時備份總結