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 <--- 監控數據信息變化,對變化的數據進行統計shell
[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個文件express
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 rsync_backup@172.16.1.41::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/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
donebash
運維工做中編寫自動化腳本規範:
1. 先完成基本功能需求
2. 優化完善腳本內容
3. 寫上一些註釋說明信息
4. 進行反覆測試
第四個里程:最終的測試
sh -x intofy.sh運維
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.xmlssh