四、實時同步
(一)課程概念介紹
- 為什么要用實時同步服務
因為定時任務有缺陷,一分鐘以內的資料無法進行同步,容易造成資料丟失 - 實時同步作業原理
a .創建要存盤資料的目錄
b .利用實時同步的軟體監控我們進行備份的資料目錄
c .利用rsync服務進行資料推送傳輸備份
(二)實時同步服務軟體部署
1.inotify+rsync實作實時同步備份
第一個里程:將inotify軟體安裝成功
yum install -y inotify-tools
說明:作業系統的yum源檔案中,是否存在epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@nfs01 ~]# rpm -ql inotify-tools
/usr/bin/inotifywait <--- 實作對資料目錄資訊變化監控(重點了解的命令)
/usr/bin/inotifywatch <--- 監控資料資訊變化,對變化的資料進行統計
[root@nfs01 ~]# cd /proc/sys/fs/inotify/
[root@nfs01 inotify]# ll
總用量 0
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_queued_events
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_user_instances
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_user_watches
max_user_watches: 設定inotifywait或inotifywatch命令可以監視的檔案數量(單行程)
默認只能監控8192個檔案
max_user_instances: 設定每個用戶可以運行的inotifywait或inotifywatch命令的行程數
默認每個用戶可以開啟inotify服務128個行程
max_queued_events: 設定inotify實體事件(event)佇列可容納的事件數量
默認監控事件佇列長度為16384
第二個里程:將rsync守護行程模式部署完畢
rsync服務端部署
a 檢查rsync軟體是否已經安裝
b 撰寫rsync軟體主組態檔
c 創建備份目錄管理用戶
d 創建備份目錄,并進行授權
e 創建認證檔案,撰寫認證用戶和密碼資訊,設定檔案權限為600
f 啟動rsync守護行程服務
rsync客戶端部署
a 檢查rsync軟體是否已經安裝
b 創建認證檔案,撰寫認證用戶密碼資訊即可,設定檔案權限為600
c 利用客戶端進行資料同步測驗
第三個里程:要讓inotify軟體和rsync軟體服務建立連接(shell腳本)
rsync軟體應用命令:
rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
inotify軟體應用命令:
inotifywait
-m|--monitor 始終保持事件監聽狀態
-r 進行遞回監控
-q|--quiet 將無用的輸出資訊,不進行顯示
--timefmt <fmt> 設定日期的格式
man strftime 獲取更多時間引數資訊
--format <fmt> 命令執行程序中,輸出的資訊格式
-e 指定監控的事件資訊
man inotifywait 查看所有引數說明和所有可以監控的事件資訊
總結主要用到的事件資訊:
create創建、delete洗掉、moved_to移入、close_write修改
inotifywait -mrq --timefmt "%F" --format "%T %w%f 事件資訊:%e" /data <-- 相對完整的命令應用
inotifywait -mrq --timefmt "%F" --format "%T %w%f 事件資訊:%e" -e create /data <-- 指定監控什么事件資訊
%w:表示發生事件的目錄
%f:表示發生事件的檔案
%e:表示發生的事件
%Xe:事件以“X”分隔
%T:使用由–timefmt定義的時間格式
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /data
以上為實作實時同步程序,所需要的重要監控命令
撰寫腳本:實作inotify與rsync軟體結合
#!/bin/bash
####################
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /data|\
while read line
do
rsync -az --delete /data/ [email protected]::backup --password-file=/etc/rsync.password
done
shell回圈語法總結:
for回圈 for xx in 回圈條件內容資訊;do xxx;done
while回圈 while 回圈條件;do xx ;done <-- 只要條件滿足,就一直回圈
while true;do xx ;done <-- 死回圈
運維作業中撰寫自動化腳本規范:
1. 先完成基本功能需求
2. 優化完善腳本內容
3. 寫上一些注釋說明資訊
4. 進行反復測驗
第四個里程:最終的測驗
sh -x intofy.sh
2、sersync+rsync實作實時同步備份
第一個里程:下載安裝sersync軟體
先進行軟體下載,把軟體包上傳到系統中
unzip sersync_installdir_64bit.zip
cd sersync_installdir_64bit
mv sersync /usr/local/
tree
第二個里程:撰寫sersync組態檔
[root@nfs01 sersync]# cd /usr/local/sersync/conf/
[root@nfs01 conf]# ll
總用量 4
-rw-r--r-- 1 root root 2214 2011-10-26 11:54 confxml.xml
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>
說明:類似于inotify的-e引數功能,指定監控的事件資訊
23 <sersync>
24 <localpath watch="/data">
25 <remote ip="172.16.1.41" name="backup"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-az"/>
31 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
說明:以上內容是資料相關的配置資訊,是必須進行修改調整配置
第三個里程:應用sersync軟體,實作實時同步
[root@nfs01 conf]# cd /usr/local/sersync/
[root@nfs01 sersync]# cd bin/
[root@nfs01 bin]# ll
總用量 1768
-rw-r--r-- 1 root root 1810128 2011-10-26 14:19 sersync
sersync命令引數:
引數-d: 啟用守護行程模式
引數-r: 在監控前,將監控目錄與遠程主機用rsync命令推送一遍(測驗)
引數-n: 指定開啟守護執行緒的數量,默認為10個
引數-o: 指定組態檔,默認使用confxml.xml檔案
./sersync -dro /usr/local/sersync/conf/confxml.xml
(三)實時同步軟體概念介紹
inotify軟體
Inotify是一種強大的,細粒度的,異步的檔案系統事件監控機制,linux內核從2.6.13起,
加入了Inotify支持,通過Inotify可以監控檔案系統中添加、洗掉,修改、移動等各種事件,
利用這個內核介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,
而inotify-tools正是實施這樣監控的軟體
軟體參考鏈接:https://github.com/rvoicilas/inotify-tools/wiki
sersync軟體
Sersync專案利用inotify與rsync技術實作對服務器資料實時同步的解決方案,
其中inotify用于監控sersync所在服務器上檔案系統的事件變化,
rsync是目前廣泛使用的本地及異地資料同步工具,
其優點是只對變化的目錄資料操作,甚至是一個檔案不同的部分進行同步,
所以其優勢大大超過使用掛接檔案系統或scp等方式進行鏡像同步,
軟體參考鏈接:https://github.com/wsgzao/sersync
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/89810.html
標籤:Linux
