以前寫過博文通過rsync實現定時備份: windows2008和Centos7.0通過Rsync來實現通過更新(備份),而現在的需求是通過rsync和inotify實現即時同步備份。 第一步: 準備工作 1. inotify介紹
Inotify 是一個 Linux特性,它監控檔案系統操作,比如讀取、寫入和建立。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。學習如何將 inotify 整合到您的應用程式中,並發現一組可用來進一步自動化系統治理的命令列工具。(來自百度百科)
2. rsync介紹
rsync是類unix系統下的資料鏡像備份工具——remote sync。一款快速增量備份工具 Remote Sync,遠程同步 支援本地複製,或者與其他SSH、rsync主機同步。(來自百度百科)
3. rsync和inotify即時同步原理圖 4. 環境部署
(1) 資料庫伺服器(inotify-master) IP: 192.168.221.131
(2) 備份伺服器(inotify-slave) IP: 192.168.221.136
第二步: 部署備份伺服器inotify-slave
這裡是部署inotify-slave環境,配置rsync daemon工作方式 1. 檢查是否安裝rsync
2. 添加rsync使用者以及模組目錄並更改其使用者組
1 |
useradd rsync -s /sbin/nologin -M #添加rsync使用者 |
1 |
mkdir /jhonse/back #建立rsync daemon工作模式的模組目錄 |
1 |
chown rsync.rsync /jhonse/back #更改模組目錄的使用者組 |
3. 配置rsync的設定檔/etc/rsyncd.conf
01 |
# /etc/rsyncd: configuration file for rsync daemon mode |
03 |
# See rsyncd.conf man page for more options. |
05 |
# configuration example: |
11 |
# pid file = /var/run/rsyncd.pid |
12 |
# exclude = lost+found/ |
13 |
# transfer logging = yes |
15 |
# ignore nonreadable = yes |
16 |
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 |
20 |
# comment = ftp export area |
34 |
pid file = /var/run/rsyncd.pid |
36 |
lock file = /var/run/rsync.lock |
38 |
log file = /var/log/rsyncd.log |
53 |
hosts allow = 192.168.221.0/255 |
55 |
hosts deny = 0.0.0.0/32 |
58 |
auth users = rsync_backup |
60 |
secrets file = /etc/rsync.password |
4. 配置虛擬使用者的密碼檔案
在/etc目錄下建立rsync.password檔案,並添加虛擬帳號和密碼
格式: 帳號:密碼
5. 為密碼檔案rsync.password提權,增加安全性
1 |
chmod 600 /etc/rsync.password |
6. 啟動rsync 服務
1 |
netstat -lnutp |grep rsync |
第三步: 部署資料庫伺服器(inotify-master)
說明: inotify是rsync用戶端安裝和執行的 1. 查看當前系統是否支援inotify
1 |
ll /proc/sys/fs/inotify/ |
如果顯示max_queued_events、max_user_instances、max_user_watches就證明支援inotify
1 |
/proc/sys/fs/inotify/max_queued_evnets |
2 |
表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。 |
3 |
/proc/sys/fs/inotify/max_user_instances |
4 |
表示每一個real user ID可建立的inotify instatnces的數量上限。 |
5 |
/proc/sys/fs/inotify/max_user_watches |
6 |
表示每個inotify instatnces可監控的最大目錄數量。如果監控的檔案數目巨大,需要根據情況,適當增加此值的大小。 |
7 |
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches |
2. 下載inotify源碼包並編譯安裝
1 |
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下載inotify源碼包 |
2 |
ll inotify-tools-3.14.tar.gz |
3 |
tar -zxf inotify-tools-3.14.tar.gz |
5 |
./configure --prefix=/usr/local/inotify-tools-3.14 #配置inotify,並指定安裝路徑為/usr/local/inotify-3.14 |
3. inotify之inotifywait命令常用參數詳解
01 |
cd /usr/local/inotify-tools-3.14/ |
02 |
./bin/inotifywait --help |
03 |
-r|--recursive Watch directories recursively. #遞迴查詢目錄 |
04 |
-q|--quiet Print less (only print events). #列印監控事件的資訊 |
05 |
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始終保持事件監聽狀態 |
06 |
--excludei <pattern> Like --exclude but case insensitive. #排除檔案或目錄時,不區分大小寫。 |
07 |
--timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定時間輸出的格式 |
08 |
--format <fmt> Print using a specified printf-like format string; read the man page for more details. |
10 |
-e|--event <event1> [ -e|--event <event2> ... ] Listen |