Centos 7.0 通過rsync和inotify實現即時同步

來源:互聯網
上載者:User

以前寫過博文通過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

1 rpm -qa|grep rsync

2. 添加rsync使用者以及模組目錄並更改其使用者組

1 useradd rsync -s /sbin/nologin  -M #添加rsync使用者
1 grep rsync /etc/passwd
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
02  
03 # See rsyncd.conf man page for more options.
04  
05 # configuration example:
06  
07 # uid = nobody
08 # gid = nobody
09 # use chroot = yes
10 # max connections = 4
11 # pid file = /var/run/rsyncd.pid
12 # exclude = lost+found/
13 # transfer logging = yes
14 # timeout = 900
15 # ignore nonreadable = yes
16 # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
17  
18 # [ftp]
19 #        path = /home/ftp
20 #        comment = ftp export area
21  
22 #工作中指定使用者(需要指定使用者)
23 uid = rsync
24 gid = rsync
25  
26 #相當於黑洞.出錯定位
27 use chroot = no
28 #有多少個用戶端同時傳檔案
29 max connections = 200
30  
31 #逾時時間
32 timeout = 300
33 #進程號檔案
34 pid file = /var/run/rsyncd.pid
35 #記錄檔
36 lock file = /var/run/rsync.lock
37 #記錄檔
38 log file = /var/log/rsyncd.log
39  
40 #模組開始
41 #這個模組對應的是推送目錄
42 #模組名稱隨便起
43 [backup]
44 #需要同步的目錄
45 path = /jhonse/back/
46 #表示出現錯誤忽略錯誤
47 ignore errors
48 #表示網路許可權可寫(本地控制真正可寫)
49 read only = false
50 #這裡設定IP或讓不讓同步
51 list = false
52 #指定允許的網段
53 hosts allow = 192.168.221.0/255
54 #拒絕連結的地址,以下表示沒有拒絕的連結。
55 hosts deny = 0.0.0.0/32
56 #不要動的東西(預設情況)
57 #虛擬使用者
58 auth users = rsync_backup
59 #虛擬使用者的密碼檔案
60 secrets file = /etc/rsync.password

4. 配置虛擬使用者的密碼檔案

在/etc目錄下建立rsync.password檔案,並添加虛擬帳號和密碼

格式: 帳號:密碼

1 rsync_backup:jhonse

5. 為密碼檔案rsync.password提權,增加安全性

1 chmod 600 /etc/rsync.password

6. 啟動rsync 服務

1 rsync --daemon
1 ps -ef |grep 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
4 cd inotify-tools-3.14
5 ./configure --prefix=/usr/local/inotify-tools-3.14 #配置inotify,並指定安裝路徑為/usr/local/inotify-3.14
6 make && make install

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.
09 #列印使用指定的輸出類似格式字串
10 -e|--event <event1> [ -e|--event <event2> ... ] Listen
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.