數據鏡像備份實驗inotify+rsync實時備份
本文是基於VM搭建的虛擬集羣環境,全部虛擬機網卡模式爲NAT方式相連,處於同一網段。
搭建環境:
使用redhead 2.6.32-431.el6.x86_64版本的linux,創建兩臺,配置相關網絡信息,確保同一網段下。
安裝配置rsync+inotify
Rsync一般系統默認安裝了,如果沒有安裝的去相關網站下載安裝包解壓安裝即可
Inotify登陸https://github.com/rvoicilas/inotify-tools/wiki#getting下載安裝即可
配置備份服務器
配置/etc/rsyncd.conf
1 uid = nobody
2 gid = nobody
3 use chroot = no
4 max connections = 10
5 strict modes = yes
6 pid file = /var/run/rsyncd.pid
7 lock file = /var/run/rsync.lock
8 log file = /var/log/rsyncd.log
9
10 [www]
11 comment = www file
12 path = /var/www/ #你要備份的目錄
13 ignore errors #忽略一些IO錯誤
14 read only = no
15 write only = no
16 hosts allow = 192.168.153.131 #服務器的IP
17 hosts deny = *
18 list = false
19 uid = root
20 gid = root
21 auth users = webuser #此用戶名隨便設置跟系統用戶沒關係,方便自己記憶即可
22 secrets file = /etc/rsync.password
配置密碼文件/etc/rsync.password
webuser:123 #注意格式 用戶名:密碼
配置服務器
配置inotifyrsync.sh放置在要備份的目錄下
#!/bin/bash
host1=192.168.153.136 #配置備份服務器的相關信息
src=/var/www/
dst=www
user=webuser
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib $src | while read files #定義同步觸發的條件
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password $src $user@$host1::$dst
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
Done
Chmod 755 inotifyrsync.sh
配置密碼文件/etc/rsync.password
123 #注意格式 只需要對應密碼
Chmod 600 /etc/rsync.password
啓動
服務器:/var/www/inotifyrsync.sh &
備份服務器:/usr/bin/rsync --daemon
測試:
在服務器上修改index.html
修改前備份服務器的index.html
修改後:
修改過程服務器的提示信息: