日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

inotify+rsync实现实时同步部署

發布時間:2024/9/30 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 inotify+rsync实现实时同步部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.1.架構規劃

1.1.1架構規劃準備

服務器系統 角色 IP

  • Centos6.7 x86_64 NFS服務器端(NFS-server-inotify-tools) 192.168.1.14
  • Centos6.7 x86_64 rsync服務器端(rsync熱備服務器) 192.168.1.17

1.1.2架構圖

2.1 部署前檢查

2.1.1 檢查rsync熱備服務器daemon是否起來

復制代碼
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]#
復制代碼
2.1.2 NFS服務器相對于rsync服務器來說是客戶端,所以要配置一個客戶端密碼

1 [root@nfs-server ~]# echo "zhurui" >>/etc/rsync.password2 [root@nfs-server ~]# cat /etc/rsync.password 3 zhurui4 [root@nfs-server ~]# chmod 600 /etc/rsync.password 5 [root@nfs-server ~]# ll /etc/rsync.password 6 -rw-------. 1 root root 7 Jan 31 00:49 /etc/rsync.password7 [root@nfs-server ~]#

2.1.3 NFS客戶端手動推/data/目錄下數據到rsync熱備服務器/backup目錄下

-------------------------------------------------------------------- 注:如果你對python感興趣,我這有個學習Python基地,里面有很多學習資料,感興趣的+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 ##推送數據命令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服務器端檢查/backup目錄下數據

3.1 服務部署

3.1.1 NFS服務端安裝inotify工具

inofity-tools下載地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

1)查看當前系統是否支持inotify

1 [root@nfs-server tools]# uname -r ##內核版本,從kernel2.6.13開始支持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 ##顯示當前三個文件表示支持

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.gz

3)編譯安裝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復制代碼1 [root@nfs-server inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools ##創建軟鏈接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

復制代碼
提示編譯成功后會生成4個目錄,分別是:

1 [root@nfs-server /]# cd /usr/local/inotify-tools2 [root@nfs-server inotify-tools]# ll3 total 164 drwxr-xr-x. 2 root root 4096 Jan 31 01:55 bin ##inotify執行命令(二進制)5 drwxr-xr-x. 3 root root 4096 Jan 31 01:55 include ##inotify程序所需用的頭文件6 drwxr-xr-x. 2 root root 4096 Jan 31 01:55 lib ##動態鏈接的庫文件7 drwxr-xr-x. 4 root root 4096 Jan 31 01:55 share ##幫助文檔

工具集合介紹:

一共安裝了2個工具(命令),即inotifywait和inotifywatch

inotifywait:在被監控的文件或目錄上等待特定文件系統事件(open、close、delete等)發生,執行后處于阻塞狀態,適合在shell腳本中使用。

inotifywatch:收集被監視的文件系統使用度統計數據,指定文件系統事件發生的次數統計。

4)inotify命令常用參數詳解

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. ##遞歸查詢目錄7 -q|--quiet Print less (only print events) ##打印很少的信息,僅僅打印監控相關的信息8 -m|--monitor Keep listening for events forever. Without9 this option, inotifywait will exit after one10 event is received. ##始終保持事件監聽狀態11 --excludei <pattern>12 Like --exclude but case insensitive. ##排除文件或目錄時,不區分大小寫13 --timefmt <fmt> strftime-compatible format string for use with14 %T in --format string. ##指定時間的輸出格式

復制代碼

5)測試監控事件

開啟兩個窗口

1.測試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 ##實行當前命令后,界面處于阻塞狀態,只有在另外一個客戶端測試時,才會顯示監控狀態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 克隆一個NFS客戶端切換到/data目錄下,新建ceshi.txt、what文件,觀察監控的變化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、測試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 ##監控/data目錄刪除時間2 3 監控如下: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 ##刪除這兩個文件9 [root@nfs-server data]#

3、同時測試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 監控輸出信息: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]#

復制代碼
一般工作中使用到:[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

工作環境中通過腳本實時同步:

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

測試:

1 腳本屬于開啟狀態: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客戶端/data目錄下創建文件12 [root@nfs-server data]# touch {a..z}13 [root@nfs-server data]# echo i am best >a ##修改a中的內容,查看熱備服務器是否同步14 15 rsync熱備服務器端顯示如下: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 ##顯示已經實時同步過來48 [root@backup data]#

復制代碼

6)優化

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

總結

以上是生活随笔為你收集整理的inotify+rsync实现实时同步部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。