inotify+rsync实现实时同步部署
1.1.架構(gòu)規(guī)劃
1.1.1架構(gòu)規(guī)劃準(zhǔn)備
服務(wù)器系統(tǒng) 角色 IP
- Centos6.7 x86_64 NFS服務(wù)器端(NFS-server-inotify-tools) 192.168.1.14
- Centos6.7 x86_64 rsync服務(wù)器端(rsync熱備服務(wù)器) 192.168.1.17
1.1.2架構(gòu)圖
2.1 部署前檢查
2.1.1 檢查rsync熱備服務(wù)器daemon是否起來(lái)
復(fù)制代碼
1 [root@backup data]# lsof -i:873
2 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
3 rsync 3071 root 3u IPv4 18684 0t0 TCP *:rsync (LISTEN)
4 rsync 3071 root 5u IPv6 18685 0t0 TCP *:rsync (LISTEN)
5 [root@backup data]# ps -ef|grep rsync|grep -v grep
6 root 3071 1 0 Dec02 ? 00:00:00 rsync --daemon
7 [root@backup data]#
復(fù)制代碼
2.1.2 NFS服務(wù)器相對(duì)于rsync服務(wù)器來(lái)說(shuō)是客戶(hù)端,所以要配置一個(gè)客戶(hù)端密碼
2.1.3 NFS客戶(hù)端手動(dòng)推/data/目錄下數(shù)據(jù)到rsync熱備服務(wù)器/backup目錄下
-------------------------------------------------------------------- 注:如果你對(duì)python感興趣,我這有個(gè)學(xué)習(xí)Python基地,里面有很多學(xué)習(xí)資料,感興趣的+Q群:895817687 --------------------------------------------------------------------1 [root@nfs-server data]# showmount -e localhost ##檢查共享目錄2 Export list for localhost:3 /data 192.168.1.0/244 [root@nfs-server ~]# cd /data/5 [root@nfs-server data]# ll6 total 127 -rw-r--r--. 1 nfsnobody nfsnobody 0 Jan 30 03:05 beautiful.txt8 -rw-r--r--. 1 nfsnobody nfsnobody 9 Jan 30 04:33 f11.txt9 -rw-r--r--. 1 root root 0 Jan 30 04:41 fs.txt10 drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 03:05 huang11 -rw-r--r--. 1 nfsnobody nfsnobody 0 Jan 30 03:17 reew.txt12 -rw-r--r--. 1 root root 0 Jan 30 02:51 test1.txt13 -rw-r--r--. 1 nfsnobody nfsnobody 0 Jan 30 04:41 wd.txt14 drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Jan 30 02:57 zhurui15 -rw-r--r--. 1 nfsnobody nfsnobody 0 Jan 30 02:47 zhurui1.txt16 -rw-r--r--. 1 nfsnobody root 0 Jan 30 02:10 zhurui.txt17 [root@nfs-server /]# rsync -avz /data/ rsync_backup@192.168.1.17::backup/ --password-file=/etc/rsync.password ##推送數(shù)據(jù)命令18 sending incremental file list19 ./20 beautiful.txt21 f11.txt22 fs.txt23 reew.txt24 test1.txt25 wd.txt26 zhurui.txt27 zhurui1.txt28 huang/29 zhurui/30 31 sent 572 bytes received 171 bytes 114.31 bytes/sec32 total size is 9 speedup is 0.0133 [root@nfs-server /]#2.1.4 rsync服務(wù)器端檢查/backup目錄下數(shù)據(jù)
3.1 服務(wù)部署
3.1.1 NFS服務(wù)端安裝inotify工具
inofity-tools下載地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
1)查看當(dāng)前系統(tǒng)是否支持inotify
1 [root@nfs-server tools]# uname -r ##內(nèi)核版本,從kernel2.6.13開(kāi)始支持2 2.6.32-573.el6.x86_64 3 [root@nfs-server tools]# ls -l /proc/sys/fs/inotify/ ##查看是否支持4 total 05 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_queued_events6 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_instances7 -rw-r--r-- 1 root root 0 Jan 31 01:34 max_user_watches8 ##顯示當(dāng)前三個(gè)文件表示支持2)下載inotify源碼包
1 [root@nfs-server tools]# ll2 total 3523 -rw-r--r--. 1 root root 358772 Mar 14 2010 inotify-tools-3.14.tar.gz3)編譯安裝inotify
1 [root@nfs-server tools]# tar zxf inotify-tools-3.14.tar.gz 2 [root@nfs-server tools]# cd inotify-tools-3.143 [root@nfs-server inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.144 [root@nfs-server inotify-tools-3.14]# make&&make install復(fù)制代碼1 [root@nfs-server inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools ##創(chuàng)建軟鏈接2 [root@nfs-server inotify-tools-3.14]# cd ..3 [root@nfs-server local]# ll4 total 445 drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin6 drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc7 drwxr-xr-x. 2 root root 4096 Sep 23 2011 games8 drwxr-xr-x. 2 root root 4096 Sep 23 2011 include9 lrwxrwxrwx. 1 root root 30 Jan 31 01:58 inotify-tools -> /usr/local/inotify-tools-3.14/10 drwxr-xr-x. 6 root root 4096 Jan 31 01:55 inotify-tools-3.1411 drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib12 drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib6413 drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec14 drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin15 drwxr-xr-x. 5 root root 4096 Nov 25 02:40 share16 drwxr-xr-x. 2 root root 4096 Sep 23 2011 src17 [root@nfs-server local]# cd inotify-tools18 [root@nfs-server inotify-tools]# ll19 total 16復(fù)制代碼
提示編譯成功后會(huì)生成4個(gè)目錄,分別是:
工具集合介紹:
一共安裝了2個(gè)工具(命令),即inotifywait和inotifywatch
inotifywait:在被監(jiān)控的文件或目錄上等待特定文件系統(tǒng)事件(open、close、delete等)發(fā)生,執(zhí)行后處于阻塞狀態(tài),適合在shell腳本中使用。
inotifywatch:收集被監(jiān)視的文件系統(tǒng)使用度統(tǒng)計(jì)數(shù)據(jù),指定文件系統(tǒng)事件發(fā)生的次數(shù)統(tǒng)計(jì)。
4)inotify命令常用參數(shù)詳解
1 [root@nfs-server inotify-tools]# ./bin/inotifywait --help2 inotifywait 3.143 Wait for a particular event on a file or set of files.4 Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]5 Options:6 -r|--recursive Watch directories recursively. ##遞歸查詢(xún)目錄7 -q|--quiet Print less (only print events) ##打印很少的信息,僅僅打印監(jiān)控相關(guān)的信息8 -m|--monitor Keep listening for events forever. Without9 this option, inotifywait will exit after one10 event is received. ##始終保持事件監(jiān)聽(tīng)狀態(tài)11 --excludei <pattern>12 Like --exclude but case insensitive. ##排除文件或目錄時(shí),不區(qū)分大小寫(xiě)13 --timefmt <fmt> strftime-compatible format string for use with14 %T in --format string. ##指定時(shí)間的輸出格式復(fù)制代碼
5)測(cè)試監(jiān)控事件
開(kāi)啟兩個(gè)窗口
1.測(cè)試create
1 第一步:2 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data ##實(shí)行當(dāng)前命令后,界面處于阻塞狀態(tài),只有在另外一個(gè)客戶(hù)端測(cè)試時(shí),才會(huì)顯示監(jiān)控狀態(tài)3 31/01/16 10:23 /data/ceshi.txt4 31/01/16 10:24 /data/what5 31/01/16 10:27 /data/a6 31/01/16 10:27 /data/b7 31/01/16 10:27 /data/c8 31/01/16 10:27 /data/d9 31/01/16 10:27 /data/e10 31/01/16 10:27 /data/f11 12 第二步:13 克隆一個(gè)NFS客戶(hù)端切換到/data目錄下,新建ceshi.txt、what文件,觀察監(jiān)控的變化14 [root@nfs-server data]# touch ceshi.txt15 [root@nfs-server data]# mkdir /who16 [root@nfs-server data]# touch what17 [root@nfs-server data]# 18 [root@nfs-server data]# touch {a..f}2、測(cè)試delete
1 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete /data ##監(jiān)控/data目錄刪除時(shí)間2 3 監(jiān)控如下:4 5 31/01/16 10:31 /data/reew.txt6 31/01/16 10:31 /data/test1.txt7 8 [root@nfs-server data]# rm -f reew.txt test1.txt ##刪除這兩個(gè)文件9 [root@nfs-server data]#3、同時(shí)測(cè)試create、delete
1 第一步:2 [root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /data3 監(jiān)控輸出信息:4 31/01/16 10:35 /data/hero.sh5 31/01/16 10:35 /data/a6 31/01/16 10:35 /data/f7 31/01/16 10:36 /data/b8 31/01/16 10:36 /data/c9 31/01/16 10:36 /data/d10 31/01/16 10:36 /data/e11 第二步:12 [root@nfs-server data]# touch hero.sh13 [root@nfs-server data]# ll14 [root@nfs-server data]# rm -f [a..f]15 [root@nfs-server data]# rm -f {a..f}16 [root@nfs-server data]#復(fù)制代碼
一般工作中使用到:[root@nfs-server inotify-tools]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M’ --format ‘%T %w%f’ -e delete,create,close_write /data
工作環(huán)境中通過(guò)腳本實(shí)時(shí)同步:
1 #!/bin/bash2 inotify=/usr/local/inotify-tools/bin/inotifywait3 $inotify -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data4 \5 |while read file6 do7 cd / &&8 rsync -az /data --delete rsync_backup@192.168.1.17::backup \9 --password-file=/etc/rsync.password10 done測(cè)試:
1 腳本屬于開(kāi)啟狀態(tài):2 [root@nfs-server scripts]# sh -x inotify.sh 3 + inotify=/usr/local/inotify-tools/bin/inotifywait4 + read file5 + /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data6 + cd /7 + rsync -az /data --delete rsync_backup@192.168.1.17::backup --password-file=/etc/rsync.password8 + read file9 + cd /10 + rsync -az /data --delete rsync_backup@192.168.1.17::backup --password11 接著在NFS客戶(hù)端/data目錄下創(chuàng)建文件12 [root@nfs-server data]# touch {a..z}13 [root@nfs-server data]# echo i am best >a ##修改a中的內(nèi)容,查看熱備服務(wù)器是否同步14 15 rsync熱備服務(wù)器端顯示如下:16 [root@backup //]# cd /backup/17 [root@backup backup]# cd data/18 [root@backup data]# ll19 total 020 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 a21 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 b22 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 c23 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 d24 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 e25 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 f26 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 g27 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 h28 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 i29 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 j30 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 k31 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 l32 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 m33 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 n34 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 o35 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 p36 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 q37 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 r38 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 s39 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 t40 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 u41 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 v42 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 w43 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 x44 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 y45 -rw-r--r--. 1 rsync rsync 0 Mar 4 2016 z46 [root@backup data]# cat a47 i am best ##顯示已經(jīng)實(shí)時(shí)同步過(guò)來(lái)48 [root@backup data]#復(fù)制代碼
6)優(yōu)化
1 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches 2 [root@nfs-server scripts]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events總結(jié)
以上是生活随笔為你收集整理的inotify+rsync实现实时同步部署的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。