inotify+rsync實現實時同步備份

注意:inotify軟件是內核2.6.13開始才支持的
第一個里程:將inotify軟件安裝成功
yum install -y inotify-tools

[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   <-- 指定監控什麼事件信息

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 intify.sh  &
-x 查看腳本執行過程   &讓腳本在後臺運行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章