inoutfy與rsync進行實時同步

一.更新阿里epel源

a.安裝鏡像源:

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo    --- 擴展源信息

yum  makecache   --更新yum源信息

二.安裝inotfy-tools 

yum install -y inotify-tools
[root@nfs01 ~]# rpm   -ql    inotify-tools
/usr/bin/inotifywait <--- 實現對數據目錄信息變化監控(重點了解的命令) /usr/bin/inotifywatch <--- 監控數據信息變化,對變化的數據進行統計

三.文件目錄信息

[root@web01 inotify]# ls
max_queued_events  max_user_instances  max_user_watches


max_user_watches: 設置inotifywait或inotifywatch命令能夠監視的文件數量(單進程)
默認只能監控8192個文件

max_user_instances: 設置每一個用戶能夠運行的inotifywait或inotifywatch命令的進程數
默認每一個用戶能夠開啓inotify服務128個進程

max_queued_events: 設置inotify實例事件(event)隊列可容納的事件數量
默認監控事件隊列長度爲16384

四.部署rsync服務

 

rsync客戶端與服務端部署html

 

rsync服務端部署

a 檢查rsync軟件是否已經安裝
b 編寫rsync軟件主配置文件
c 建立備份目錄管理用戶
d 建立備份目錄,並進行受權
e 建立認證文件,編寫認證用戶和密碼信息,設置文件權限爲600
f 啓動rsync守護進程服務
linux

----------------------------------------------------------------------------------------web

rsync客戶端部署

a 檢查rsync軟件是否已經安裝
b 建立認證文件,編寫認證用戶密碼信息便可,設置文件權限爲600
c 利用客戶端進行數據同步測試
shell

rsync    -avz   /backup   rsync_backup@172.16.1.41::backup/`hostname  -i`     --passworld-file=/etc/rsync.passworldbash

五.將inotify與rsync進行結合(shell腳本)

rsync軟件應用命令:

rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password   curl

 

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
以上爲實現實時同步過程,所須要的重要監控命令ui

編寫腳本:實現inotify與rsync軟件結合
#!/bin/bash
####################
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write /data|\
while read line
do
rsync -az --delete /backup  rsync_backup@172.16.1.41::backup/`hostname -i`    --password-file=/etc/rsync.password     --delete無差別同步   就是我沒有你也要沒有
done
相關文章
相關標籤/搜索