rsync+inotify實時備份

rsync+inotify實時備份

爲什麼要使用inotify

遠程同步方式,我們一般採用rsync命令、或者守護進程的方式。

優點:具有安全性高、備份迅速、支持增量備份。

缺點:1、rsync同步數據時,需要掃描所有文件後進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。

2、rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,實際是異步同步,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。

 

inotify 是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,當文件有任何變動時,就觸發rsync同步,解決了同步數據的實時性問題。

意思:

inotify可以支持一對一、一對多的實時同步,支持單機實時備份、災難切換,也可以實現在負載均衡LB下同時更新、上線多臺RS服務器或者保持數據一致性。

實時同步實例:

同步順序:青島監控機10.32.232.238(監控主機)——> 10.32.232.239(監控備機)

Web1:10.32.232.238(rsyncclient,inotify)

Web2:10.32.232.239(rsyncserver)

同步的目錄是 /usr/local/nagios/etc , 實現監控備機配置文件與主機實時同步。

一、配置 rsync服務器端(238

1.先使用 rpm –qa 命令查看系統中是否已經安裝了 rsync 軟件包,如果有我們可以不用進行安裝,如果沒有我們可以掛載上 CentOS 的系統光盤進行安裝(當然,我們也可以選擇使用源碼包編譯安裝)

[root@232-239-qd-monitor~]# rpm -qa | grep rsync

rsync-3.0.6-9.el6_4.1.x86_64

[root@232-239-qd-monitor~]#

2. Rsync安裝好之後沒自己的配置文件,創建 rsync 的配置文件

[root@232-239-qd-monitortmp]# cat /etc/rsyncd.conf

##rsyncd.confstart##

uid= root

gid= root

usechroot = no

maxconnections = 200     ##最大連接數

timeout= 300             ##超時時間

pidfile = /var/run/rsyncd.pid

lockfile = /var/run/rsync.lock

logfile = /var/log/rsyncd.log

ignoreerrors

readonly = false        ##只讀爲假,可以上傳

list= false

hostsallow = 10.32.0.0/24     ##允許網段

hostsdeny = 0.0.0.0/32

authusers = rsync_backup      ##用戶

secretsfile = /etc/rsync.password   ##密碼文件

[nagios]                       ##模塊

path= /usr/local/nagios/etc   ##模塊目錄

[wangxu]

path= /tmp

#rsync_config_______________end

3.建立 rsync 用戶名和密碼文件

[root@232-238-qd-monitor~]#  echo "rsync_backup:monitor " >>  /etc/rsync.password

4.爲 /etc/rsync.password 授權爲 600(這個文件的權限必須是 600)

[root@232-238-qd-monitor~]# chmod -R 600 /etc/rsync.password

二、客戶端(239)配置:

1.設置 rsync 客戶端的密碼文件,客戶端只需要設置 rsync 同步的密碼即可,不用設置用戶名

[root@232-239-qd-monitor~]# echo "monitor" >>  /etc/rsync.password

4.   爲 /etc/rsync.password 授權爲 600(這個文件的權限必須是 600)

[root@232-239-qd-monitor~]# chmod -R 600 /etc/rsync.password

三、測試rsync守護進程。

[root@232-239-qd-monitortmp]# rsync --daemon    ####啓動

[root@232-239-qd-monitortmp]# netstat -lntp | grep rsync

tcp        0     0 0.0.0.0:873                0.0.0.0:*                   LISTEN      36834/rsync        

tcp        0     0 :::873                     :::*                        LISTEN      36834/rsync        

[root@232-239-qd-monitortmp]#

[root@232-239-qd-monitortmp]# pwd

/tmp

[root@232-239-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt  sms.log

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt

[root@232-239-qd-monitortmp]#

——————————————————————————————————————————————————

[root@232-238-qd-monitortmp]# pwd

/tmp

[root@232-238-qd-monitortmp]# rsync -avz  [email protected]::wangxu//tmp/ --password-file=/etc/rsync.password            #### 同步

receivingincremental file list

./

10.32.231.177_mongodb_cacti_stats.txt

10.32.231.186_mongodb_cacti_stats.txt

10.32.232.222_mongodb_cacti_stats.txt

10.32.232.224_mongodb_cacti_stats.txt

10.32.232.225_mongodb_cacti_stats.txt

10.32.232.226_mongodb_cacti_stats.txt

 

sent220 bytes  received 12073 bytes  24586.00 bytes/sec

totalsize is 170894  speedup is 13.90

[root@232-238-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt 

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt 

[root@232-238-qd-monitortmp]#

Delete 用法:

本地爲空,遠端目錄直接清空。有使遠端rsync服務端丟數據的風險。

[root@232-238-qd-monitortmp]# ls

[root@232-238-qd-monitortmp]# rsync -avz --delete /tmp/ [email protected]::wangxu/  --password-file=/etc/rsync.password

sendingincremental file list

./

deletingsms.log

deletingmonitor_bak.sh

deleting10.32.232.226_mongodb_cacti_stats.txt

deleting10.32.232.225_mongodb_cacti_stats.txt

deleting10.32.232.224_mongodb_cacti_stats.txt

deleting10.32.232.222_mongodb_cacti_stats.txt

deleting10.32.231.186_mongodb_cacti_stats.txt

deleting10.32.231.177_mongodb_cacti_stats.txt

 

sent55 bytes  received 12 bytes  134.00 bytes/sec

totalsize is 0  speedup is 0.00

[root@232-238-qd-monitortmp]# ls

[root@232-238-qd-monitortmp]#

 

[root@232-239-qd-monitortmp]# ls

10.32.231.177_mongodb_cacti_stats.txt  10.32.232.222_mongodb_cacti_stats.txt  10.32.232.225_mongodb_cacti_stats.txt  monitor_bak.sh

10.32.231.186_mongodb_cacti_stats.txt  10.32.232.224_mongodb_cacti_stats.txt  10.32.232.226_mongodb_cacti_stats.txt  sms.log

[root@232-239-qd-monitortmp]# ls

[root@232-239-qd-monitortmp]#

四、配置initify(web1)

wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar  -zxf inotify-tools-3.14.tar.gz

cdinotify-tools-3.14

 ./configure && make && makeinstall

1、詳解inotify

root@232-238-qd-monitorbin]# pwd

/usr/local/bin

[root@232-238-qd-monitorbin]# ls inotifywa*

inotifywait  inotifywatch

[root@232-238-qd-monitorbin]#

inotifywait用於等待文件或文件集上的一個特定事件,它可以監控任何文件和目錄設置,並且可以遞歸地監控整個目錄樹。
     inotifywatch用於收集被監控的文件系統統計數據,包括每個inotify事件發生多少次等信息。

Inotify 可以監視的文件系統事件包括:

IN_ACCESS,即文件被訪問

IN_MODIFY,文件被 write

IN_ATTRIB,文件屬性被修改,如 chmod、chown、touch 等

IN_CLOSE_WRITE,可寫文件被 close

IN_CLOSE_NOWRITE,不可寫文件被 close

IN_OPEN,文件被 open

IN_MOVED_FROM,文件被移走,如 mv

IN_MOVED_TO,文件被移來,如 mv、cp

IN_CREATE,創建新文件

IN_DELETE,文件被刪除,如 rm

IN_DELETE_SELF,自刪除,即一個可執行文件在執行時刪除自己

IN_MOVE_SELF,自移動,即一個可執行文件在執行時移動自己

IN_UNMOUNT,宿主文件系統被 umount

IN_CLOSE,文件被關閉,等同於(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)

IN_MOVE,文件被移動,等同於(IN_MOVED_FROM | IN_MOVED_TO)

備註:上面的文件也包括目錄

 

Inotifywait是一個監控等待事件,可以配合shell腳本使用它,下面介紹一下常用的一些參數:

 -m,即--monitor,表示始終保持事件監聽狀態。
 -r,即--recursive,表示遞歸查詢目錄。
 -q,即--quiet,表示打印出監控事件。
 -e,即--event,通過此參數可以指定要監控的事件,常見的事件有modify、delete、create、attrib等。
更詳細的請參看man  inotifywait。

inotify相關參數

inotify定義了下列的接口參數,可以用來限制inotify消耗kernel memory的大小。由於這些參數都是內存參數,因此,可以根據應用需求,實時的調節其大小。
   /proc/sys/fs/inotify/max_queued_evnets     
      表示調用inotify_init時分配給inotifyinstance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。
   /proc/sys/fs/inotify/max_user_instances
       表示每一個real user ID可創建的inotifyinstatnces的數量上限。
   /proc/sys/fs/inotify/max_user_watches
       表示每個inotify instatnces可監控的最大目錄數量。如果監控的文件數目巨大,需要根據情況,適當增加此值的大小

[[email protected]]# cat /proc/sys/fs/inotify/max_queued_events

16384

2inotify監控腳本

[root@232-238-qd-monitor wangxu]# catrsync_inotify.sh

#!/bin/bash

#para

host01=10.32.232.239        ##同步IP,可以設置多個

src=/usr/local/nagios/etc     ##同步的本地目錄

dst=nagios                  ##rsync服務端模塊

user=rsync_backup           ##rsync用戶

rsync_passfile=/etc/rsync.password

inotify_home=/usr/local

#judge

if[ ! -e "$src" ] \

||[ ! -e "${rsync_passfile}" ] \

||[ ! -e "${inotify_home}/bin/inotifywait" ] \

||[ ! -e "/usr/bin/rsync" ];

then

  echo "Check File and Folder"

  exit 9

fi

${inotify_home}/bin/inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -eclose_write,delete,create,attrib $src \

|while read file

        do

        #rsync -avzP --delete --timeout=100--password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null2>&1

         cd $src && rsync -aruz -R--delete ./  --timeout=100$user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

        done

exit0

[root@232-238-qd-monitorwangxu]#

[

2、執行腳本。

[root@232-238-qd-monitoretc]# sh /root/wangxu/rsync_inotify.sh &

[root@232-238-qd-monitorwangxu]# ps -ef | grep inotify

root      8534 26546  0 09:56 pts/0    00:00:00 grep inotify

root     30432 26546  0 09:48 pts/0    00:00:00 sh /root/wangxu/rsync_inotify.sh

root     30433 30432  0 09:48 pts/0    00:00:00 /usr/local/bin/inotifywait -mrq--timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib/usr/local/nagios/etc

root     30434 30432  0 09:48 pts/0    00:00:00 sh /root/wangxu/rsync_inotify.sh

[root@232-238-qd-monitorwangxu]#

3、檢查測試。

[root@232-238-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server

[root@232-238-qd-monitor etc]#

 

[root@232-239-qd-monitor etc]# pwd

/usr/local/nagios/etc

[root@232-239-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server

[root@232-239-qd-monitoretc]#

 

[root@232-238-qd-monitoretc]# mkdir test

[root@232-238-qd-monitoretc]# echo "123" >> wangxu.txt

[root@232-238-qd-monitoretc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg  test

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server        wangxu.txt

[root@232-238-qd-monitoretc]#

 

[root@232-239-qd-monitor etc]# ls

cgi.cfg   htpasswd.bak.20140326  htpasswd.users      nagios.cfg       nrpe.cfg objects.yuan  resource.cfg  test

htpasswd  htpasswd.bk            htpasswd.users.bak  nagios.cfg.yuan  objects  pnp           server        wangxu.txt

[root@232-239-qd-monitoretc]#

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章