1. inotify+sersync實時復(fù)制應(yīng)用簡介
1.1 實時復(fù)制介紹
利用實時復(fù)制方式,實現(xiàn)實時備份重要數(shù)據(jù)信息。實時復(fù)制是企業(yè)數(shù)據(jù)備份最重要的方式,主要用于對用戶提交的數(shù)據(jù)的實時備份梯浪。對于用戶提交到服務(wù)器上的普通文件(壓縮包捌年、視頻、文檔等)驱证,可采用inotify/sersync+rsync實時備份方式延窜;對于數(shù)據(jù)文件還有較復(fù)雜的分布式存儲工具自身可以實現(xiàn)將數(shù)據(jù)同時備份成多份恋腕,例如:FastDFS抹锄、GlusterFS等;對于提交到數(shù)據(jù)庫中的數(shù)據(jù)荠藤,可使用數(shù)據(jù)庫的主從復(fù)制(例如:MySQL伙单、Oracle)方式備份,這是軟件自帶的實時復(fù)制備份方式哈肖。
1.2 實時復(fù)制細節(jié)實現(xiàn)說明
實時復(fù)制軟件會監(jiān)控磁盤文件系統(tǒng)的變化吻育,比如:指定一個/data目錄,實時復(fù)制軟件服務(wù)進程會實時監(jiān)控這個/data目錄中數(shù)據(jù)的變化淤井,即文件系統(tǒng)block與inode信息的變化布疼。
一旦/data目錄文件發(fā)生變化,就會執(zhí)行sync命令币狠,將變化的數(shù)據(jù)信息推送到備份服務(wù)器相應(yīng)的備份目錄中游两,從而完成數(shù)據(jù)的實時備份。
1.3 實時復(fù)制軟件介紹
企業(yè)中較為常用的實時復(fù)制軟件有如下兩種:(推薦使用第二個)
| 軟件服務(wù) | 依賴程序 | 部署難點 | 說明
|inotify-tools| rsync守護進程務(wù) | 復(fù)制腳本編寫 | 監(jiān)控目錄數(shù)據(jù)變化漩绵,實時數(shù)據(jù)復(fù)制
| sersync | rsync守護進程務(wù)| 配置文件編寫 | 監(jiān)控目錄數(shù)據(jù)變化贱案,實時數(shù)據(jù)復(fù)制
1.4 實時復(fù)制inotify機制簡介
Inotify是一種異步的系統(tǒng)事件監(jiān)控機制,linux內(nèi)核從2.6.13起止吐,加入了Inotify支持麦牺,通過Inotify可以監(jiān)控文件系統(tǒng)中添加市殷、刪除,修改、移動等各種事件叹话。實際上inotify-tools是一個監(jiān)控指定目錄數(shù)據(jù)實時變化的軟件程序。
(sersync軟件是在inotify基礎(chǔ)上進行開發(fā)的脓魏,功能要更加強大些慰丛,支持配置文件配置,多了定時重試機制套鹅,過濾機制站蝠,提供接口做CDN數(shù)據(jù)更新,支持多線程操作)卓鹿。
inotify機制優(yōu)點:監(jiān)控文件系統(tǒng)事件變化菱魔,通過復(fù)制工具實現(xiàn)實時數(shù)據(jù)復(fù)制,支持多線程實時復(fù)制吟孙。
inotify機制缺點:實測并發(fā)如果大于200個文件(10~100K)澜倦,復(fù)制會有延遲聚蝶。
1.5 inotify工具命令介紹
1.5.1 inotify-tools工具主要命令:
inotifywait:在被監(jiān)控的目錄等待特定文件系統(tǒng)事件發(fā)生,執(zhí)行后處于阻塞狀態(tài)藻治,適合在shell腳本中使用碘勉。
inotifywatch:收集被監(jiān)控的文件系統(tǒng)使用的統(tǒng)計數(shù)據(jù),指文件系統(tǒng)時間發(fā)生的次數(shù)統(tǒng)計桩卵。
1.5.2 inotifywait命令參數(shù)詳細說明
| 命令參數(shù)|參數(shù)說明|
| -m(--monitor)| 始終保持事件監(jiān)聽狀態(tài)※|
| -d(--daemon) | 類似-m參數(shù)验靡,只是將命令運行在后臺記錄觸發(fā)的事件信息在指定文件中,利用—outfile參數(shù)定義程序日志使用—syslog參數(shù) |
| -r| 遞歸監(jiān)控目錄數(shù)據(jù)信息變化※ |
| -o(--outfile<file>) | 打印事件到文件中雏节,相當(dāng)于標(biāo)準(zhǔn)正確輸出 |
| -s(--syslog) | 發(fā)送錯誤到syslog相當(dāng)于標(biāo)準(zhǔn)錯誤輸出 |
| -q(--qulet) | 輸出信息少(只打印事件信息) |
| --excludei<pattern> | 排除文件或目錄時胜嗓,不區(qū)分大小寫|
| --timefmt<fmt> | 指定時間輸出的格式|
| --fromat<fmt> | 打印使用指定的輸出類似格式字符串;即實際監(jiān)控輸出的內(nèi)容|
| -e | 指定監(jiān)聽指定的事件钩乍,如果省略辞州,表示所有時間都進行監(jiān)聽※ |
| 時間名稱| 事件說明|
| access| 文件或目錄內(nèi)容被讀取|
| modify | 文件或目錄內(nèi)容被寫入 |
| attrib | 文件或目錄屬性改變 |
| close_write | 文件或目錄關(guān)閉,在寫入模式打開之后關(guān)閉的※ |
| close_nowrite | 文件或目錄關(guān)閉寥粹,在只讀模式打開之后關(guān)閉的 |
| close | 文件或目錄關(guān)閉变过,不管讀或是寫模式 |
| open | 文件或目錄被打開 |
| moved_to | 文件或目錄被移動到監(jiān)控的目錄中 |
| moved_from| 文件或目錄被移動從監(jiān)控的目錄中 |
| move| 文件或目錄不管移動到或是移動出監(jiān)控目錄都吃觸發(fā)事件|
| create| 文件或目錄創(chuàng)建在監(jiān)控的目錄中※|
| delete | 文件或目錄被刪除在監(jiān)控的目錄中※ |
| delete_self| 文件或目錄被刪除|
| unmount| 文件系統(tǒng)包含的文件或目錄不能卸載 |
2. inotify+rsync實時復(fù)制項目實踐
## 2.1 前提:backup rsync服務(wù)端部署完成然后在nfs01中定義密碼認證變量,并測試數(shù)據(jù)推送和拉壤缘印:
[root@nfs01 ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc #<==定義密碼環(huán)境變量
[root@nfs01 ~]# source /etc/bashrc #<==使配置生效
[root@nfs01 ~]# echo $RSYNC_PASSWORD #<==檢查媚狰,確保生效
oldboy
[root@nfs01 /]# rsync -avz /data rsync_backup@172.16.1.41::backup
sending incremental file list #<==出現(xiàn)以下幾行提示,說明rsync服務(wù)環(huán)境是正常的
data/
sent 72 bytes received 28 bytes 200.00 bytes/sec
total size is 0 speedup is 0.00
2.2 檢查Linux系統(tǒng)是否支持inotify實時監(jiān)控
[root@nfs01 /]# uname -r #<==內(nèi)核是3.10妄痪,支持inotify機制
3.10.0-957.5.1.el7.x86_64
[root@nfs01 /]# ls -l /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 4月 19 12:27 max_queued_events #<==設(shè)置可容納的事件數(shù)量
-rw-r--r-- 1 root root 0 4月 19 09:45 max_user_instances #<==設(shè)置每個用戶可以運行的進程數(shù)
-rw-r--r-- 1 root root 0 4月 19 12:27 max_user_watches #<==設(shè)置可以監(jiān)視的文件數(shù)量
顯示以上三個文件證明系統(tǒng)支持inotfiy程序功能
2.3 inptify-tools安裝
[root@nfs01 /]# yum install epel-release -y #<==安裝epel源哈雏,默認的官方源沒有inotify-tools
[root@nfs01 /]# yum install inotify-tools -y #<==下載inotify軟件工具
[root@nfs01 /]# rpm -ql inotify-tools|head -2 #<==查看(必須出現(xiàn)以下兩個命令)
/usr/bin/inotifywait
/usr/bin/inotifywatch
2.4 測試實踐
xshell中打開兩個nsf01窗口,第一個窗口輸入如下命令衫生,第二個窗口進入/data目錄下創(chuàng)建文件裳瘪。
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
![M1GXVTFBZL5LGIA@V$D]L~S.png](https://upload-images.jianshu.io/upload_images/16833174-0c668bd3bc1fabea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.5 編寫實時監(jiān)控和復(fù)制腳本
執(zhí)行的命令和編寫的腳本如下:
[root@nfs01 /]# mkdir /server/scripts/ -p
[root@nfs01 /]# cd /server/scripts/
[root@nfs01 /server/scripts]# cat monitor.sh
#!/bin/sh
cmd="/usr/bin/inotifywait"
$cmd -mrq --format '%w%f' -e close_write,delete /data|\
while read line
do
#刪除事件發(fā)生
[ ! -e "$line" ] && cd /data &&\
rsync -az --delete ./ rsync_backup@172.16.1.41::backup && continue
#處理增改事件
rsync -az --delete $line rsync_backup@172.16.1.41::backup
done
#測試沒有問題之后,在nfs01上讓腳本在后臺運行罪针,并放入/etc/rc.local中
[root@nfs01 /]# /bin/sh /server/scripts/monitor.sh & #<==&表示讓腳本在后臺運行
[1] 45998
[root@nfs01 /]# tail -2 /etc/rc.local
/bin/sh /server/scripts/monitor.sh &
2.6 inotify優(yōu)化
[root@nfs01 /]# cat /proc/sys/fs/inotify/max_user_watches
50000000
[root@nfs01 /]# cat /proc/sys/fs/inotify/max_queued_events
327679
[root@nfs01 /]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches
[root@nfs01 /]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events
#以上兩個參數(shù)重啟可能會失效彭羹,所以放在/etc/rc.local中
[root@nfs01 /]# tail -2 /etc/rc.local
echo "50000000" >/proc/sys/fs/inotify/max_user_watches
echo "50000000" >/proc/sys/fs/inotify/max_queued_events
3. sersync項目實踐
3.1 先測試客戶端能否推送成功(nfs01上)
[root@nfs01 /]# rsync -az /data/ [rsync_backup@172.16.1.41::backup]
3.2 部署sersync服務(wù)(nfs01上)
#sersync_oldboy_64bit_20160928.tar.gz #<==配置文件壓縮包
#鏈接: https://pan.baidu.com/s/1ZGz9tQYKHYqolnjZ9WL7Cw 提取碼: u82f
[root@nfs01 /]# mkdir -p /server/tools/
[root@nfs01 /]# cd /server/tools/
[root@nfs01 /server/tools]# tar xf sersync_oldboy_64bit_20160928.tar.gz
[root@nfs01 /server/tools]# mkdir /application/ -p
[root@nfs01 /server/tools]# mv application/sersync /application/
3.3 sersync配置文件(nfs01中)
[root@nfs01 /server/tools]# cd /application/sersync/
[root@nfs01 /application/sersync]# ls
bin conf logs readme.txt
[root@nfs01 /application/sersync]# cd conf/
[root@nfs01 /application/sersync/conf]# ls
confxml.xml confxml.xml.ori
[root@nfs01 /application/sersync/conf]# cat -n confxml.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <head version="2.5">
3 <host hostip="localhost" port="8008"></host>
4 <debug start="false"/>
5 <fileSystem xfs="false"/>
6 <filter start="false">
7 <exclude expression="(.*)\.svn"></exclude>
8 <exclude expression="(.*)\.gz"></exclude>
9 <exclude expression="^info/*"></exclude>
10 <exclude expression="^static/*"></exclude>
11 </filter>
12 <inotify>
13 <delete start="true"/>
14 <createFolder start="true"/>
15 <createFile start="false"/>
16 <closeWrite start="true"/>
17 <moveFrom start="true"/>
18 <moveTo start="true"/>
19 <attrib start="false"/>
20 <modify start="false"/>
21 </inotify>
22
23 <sersync>
24 <localpath watch="/data">
25 <remote ip="172.16.1.41" name="backup"/>
26 <remote ip="172.16.1.41" name="oldboy"/>
27 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
28 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
29 </localpath>
30 <rsync>
31 <commonParams params="-artuz"/>
32 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
33 <userDefinedPort start="false" port="874"/><!-- port=874 -->
34 <timeout start="true" time="100"/><!-- timeout=100 -->
35 <ssh start="false"/>
36 </rsync>
37 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
38 <crontab start="false" schedule="600"><!--600mins-->
39 <crontabfilter start="false">
40 <exclude expression="*.php"></exclude>
41 <exclude expression="info/*"></exclude>
42 </crontabfilter>
43 </crontab>
44 <plugin start="false" name="command"/>
45 </sersync>
46
47 <plugin name="command">
48 <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
49 <filter start="false">
50 <include expression="(.*)\.php"/>
51 <include expression="(.*)\.sh"/>
52 </filter>
53 </plugin>
54
55 <plugin name="socket">
56 <localpath watch="/opt/tongbu">
57 <deshost ip="192.168.138.20" port="8009"/>
58 </localpath>
59 </plugin>
60 <plugin name="refreshCDN">
61 <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
62 <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
63 <sendurl base="http://pic.xoyo.com/cms"/>
64 <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
65 </localpath>
66 </plugin>
67 </head>
3.4 啟動服務(wù)并添加到/etc/rc.local中
[root@nfs01 /application/sersync/conf]# /application/sersync/bin/sersync -d -n 10 -o /application/sersync/conf/confxml.xml #<==開啟服務(wù)
[root@nfs01 /application/sersync/conf]# tail -3 /etc/rc.local
/application/sersync/bin/sersync -d -n 10 -o /application/sersync/conf/confxml.xml &>/dev/null
4. 拓展知識
#配置:systemctl start sersync啟動方案
#參考網(wǎng)址:https://blog.51cto.com/oldboy/2155931
[root@nfs01 /data]# cat /etc/rc.d/init.d/sersync
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com
start(){
/application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
}
stop(){
killall sersync 2>/dev/null
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
*)
echo $"Usage:$0 {start|stop|restart}"
exit 1
esac
[root@nfs01 /data]# chmod +x /etc/rc.d/init.d/sersync
[root@nfs01 /data]# cat /usr/lib/systemd/system/sersync.service
[Unit]
Description=sersyncd service
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/sersync start
ExecReload=/etc/rc.d/init.d/sersync restart
ExecStop=/etc/rc.d/init.d/sersync stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod +x /usr/lib/systemd/system/sersync.service
[root@nfs01 /data]# systemctl enable sersync.service
Created symlink from /etc/systemd/system/multi-user.target.wants/sersync.service to /usr/lib/systemd/system/sersync.service.
[root@nfs01 /data]# systemctl status sersync.service
● sersync.service - sersyncd service
Loaded: loaded (/usr/lib/systemd/system/sersync.service; enabled; vendor preset: disabled)
Active: inactive (dead)
[root@nfs01 /data]# systemctl stop sersync
[root@nfs01 /data]# ps -ef|grep sersync|grep -v grep
[root@nfs01 /data]# systemctl start sersync