在這裏介紹下,rsync結合inotify配置文件實時同步的方法,有需要的朋友參考下。關於軟件我就不多介紹了,請參考度娘!
注意:1.在配置的時候分清楚主服務器和備份服務器。
2.在使用別人的腳本的時候,注意自己的腳本里引用配置文件的名稱。
3.關閉防火牆,先測試手動推送,推送成功後再來測試實時同步。
一、部署環境
服務器IP地址 | 主機名 | 服務器系統 |
192.168.1.13 主服務器 | 13server | Centos 6.5 X64 |
192.168.1.14 備份服務器 | localhost | Centos 6.5 X64 |
二、備份服務器安裝rsync
2.1因爲我這裏是兩臺新安裝的服務器所以就直接安裝了,就沒有去檢查上面有沒殘留的rsync的文件。rsync安裝包我是從官網下載的,看你喜歡哪個版本,提供個官網地址http://rsync.samba.org/。
#cd /usr/local/src
#tar -zxvf rsync-3.0.9.tar.gz //解壓文件
#cd rsync-3.0.9 //進入該文件目錄
#./configure --prefix=/usr/local/rsync
/主要的作用是對即將安裝的軟件進行配置,檢查當前的環境是否滿足要安裝軟件的依賴關係
#make //編譯 (如果報錯的話就請仔細檢查下是不是缺失了什麼庫文件,可以用我這個)
(yum -y install make gcc-c++ cmake bison-devel ncurses-devel gcc \
autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel*)
#make install //安裝
2.2新建rsync用戶及模塊目錄並更改其用戶組
[root@13server]# useradd rsync -s /sbin/nologin -M #添加rsync用戶
[root@13server]# grep rsync /etc/passwd
rsync:x:2004:2004::/home/rsync:/sbin/nologin
[root@13server]# mkdir /data/backup #創建rsync daemon工作模式的模塊目錄
[root@13server]# ll -d /data/backup/
drwxr-xr-x. 2 rsync rsync 4096 8月 11 13:06 /data/backup/
[root@13server]# chown rsync.rsync /data/backup/ #更改模塊目錄的用戶組
[root@13server]# ll -d /data/backup/
drwxr-xr-x. 2 rsync rsync 4096 8月 11 13:06 /data/backup/
2.3編寫rsync daemon配置文件/etc/rsyncd.conf
[root@13server /]# cat /etc/rsyncd.conf
##rsyncd.conf start## #工作中指定用戶(需要指定用戶) uid = rsync gid = rsync #相當於黑洞.出錯定位 use chroot = no #有多少個客戶端同時傳文件 max connections = 200 #超時時間 timeout = 300 #進程號文件 pid file = /var/run/rsyncd.pid #日誌文件 lock file = /var/run/rsync.lock #日誌文件 log file = /var/log/rsyncd.log #模塊開始 #這個模塊對應的是推送目錄 #模塊名稱隨便起 [backup] #需要同步的目錄 path = /data/backup/ #表示出現錯誤忽略錯誤 ignore errors #表示網絡權限可寫(本地控制真正可寫) read only = false #這裏設置IP或讓不讓同步 list = false #指定允許的網段 hosts allow = 192.168.1.0/24 #拒絕鏈接的地址,下面表示沒有拒絕的鏈接。 hosts deny = 0.0.0.0/32 #不要動的東西(默認情況) #虛擬用戶 auth users = rsync_backup #虛擬用戶的密碼文件 secrets file = /etc/rsync.password #配置文件的結尾 #rsync_config_______________end
2.4配置虛擬用戶的密碼文件
[root@13server /]# echo "rsync_backup:p@ssword" >/etc/rsync.password
[root@13server /]# cat /etc/rsync.password
rsync_backup:p@ssword #注:rsync_backup爲虛擬用戶,p@ssword爲這個虛擬用戶的密碼
[root@13server /]# chmod 600 /etc/rsync.password #爲密碼文件提權,增加安全性
[root@13server /]# ll /etc/rsync.password
-rw-------. 1 root root 22 8月 10 19:27 /etc/rsync.password
2.5啓動rsync
[root@13server /]# rsync --daemon #啓動rsync服務
[root@13server /]# ps -ef |grep rsync
root 9871 1 0 Aug10 ? 00:00:00 rsync --daemon
root 10532 10500 0 13:30 pts/0 00:00:00 grep rsync
[root@localhost backup]# netstat -lnutp |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9871/rsync
tcp 0 0 :::873 :::* LISTEN 9871/rsync
三、主服務器部署
3.1通過root@localhost測試推送
root@localhost配置密碼文件,測試推送
[root@localhost ~]# echo "p@ssword" >/etc/rsync.password
[root@localhost ~]# cat /etc/rsync.password
p@ssword #注意:這裏只要寫密碼即可,切記。
[root@localhost ~]# chmod 600 /etc/rsync.password
[root@localhost ~]# ll /etc/rsync.password
-rw------- 1 root root 7 8月 22 20:32 /etc/rsync.password
[root@localhost ~]# echo "hello leesir">test.txt
[root@localhost ~]# cat test.txt
hello leesir
[root@localhost ~]# rsync -avz test.txt [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
test.txt
sent 82 bytes received 27 bytes 72.67 bytes/sec
total size is 13 speedup is 0.12
備份服務器檢查:
[root@13server /]# ll /data/backup/
總用量 4
-rw-r--r--. 1 rsync rsync 13 4月 22 14:34 test.txt
[root@3server /]# cat /data/backup/test.txt
hello leesir
3.2查看當前系統是否支持inotify
[root@localhost work]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 8月 11 13:35 max_queued_events
-rw-r--r-- 1 root root 0 8月 11 13:35 max_user_instances
-rw-r--r-- 1 root root 0 8月 11 13:35 max_user_watches
#顯示這三個文件則證明支持。
拓展:
/proc/sys/fs/inotify/max_queued_evnets
表示調用inotify_init時分配給inotify instance中可排隊的event的數目的最大值,超出這個值的事件被丟棄,但會觸發IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances
表示每一個real user ID可創建的inotify instatnces的數量上限。
/proc/sys/fs/inotify/max_user_watches
表示每個inotify instatnces可監控的最大目錄數量。如果監控的文件數目巨大,需要根據情況,適當增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
3.3下載安裝inotify源碼包
[root@localhost ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz #下載inotify源碼包
..................................
[root@localhost ~]# cd inotify-tools-3.14
[[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-3.14 #配置inotify,並指定安裝路徑爲/usr/local/inotify-3.14
................................
[[root@localhost ~] inotify-tools-3.14]# make && make install
................................
3.4
inotify之inotifywait命令常用參數詳解
[root@localhost inotify-tools-3.14]# cd /usr/local/inotify-3.14/
[root@localhost inotify-3.14]# ./bin/inotifywait --help
-r|--recursive Watch directories recursively. #遞歸查詢目錄
-q|--quiet Print less (only print events). #打印監控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始終保持事件監聽狀態
--excludei <pattern> Like --exclude but case insensitive. #排除文件或目錄時,不區分大小寫。
--timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定時間輸出的格式
--format <fmt> Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的輸出類似格式字符串
-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. #通過此參數可以指定需要監控的事件,如下所示:
Events:
access file or directory contents were read #文件或目錄被讀取。
modify file or directory contents were written #文件或目錄內容被修改。
attrib file or directory attributes changed #文件或目錄屬性被改變。
close file or directory closed, regardless of read/write mode #文件或目錄封閉,無論讀/寫模式。
open file or directory opened #文件或目錄被打開。
moved_to file or directory moved to watched directory #文件或目錄被移動至另外一個目錄。
move file or directory moved to or from watched directory #文件或目錄被移動另一個目錄或從另一個目錄移動至當前目錄。
create file or directory created within watched directory #文件或目錄被創建在當前目錄
delete file or directory deleted within watched directory #文件或目錄被刪除
unmount file system containing file or directory unmounted #文件系統被卸載
3.5編寫監控腳本並加載到後臺執行
#!/bin/bash src=/data/backup/ #本地監控的目錄 des=backup #13server的rsync服務的模塊名 host=192.168.1.13 #備份服務器的ip地址 /usr/local/bin/inotifywait(#這個就要主要到你的這個可執行文件放在哪個位置) -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files do for hostip in $host do rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password $src rsync_backup@$hostip::$des #注意紅色字體部分,根據你自己配置的時候的名稱來更改 done echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
3.6啓動nohup
nohup /bin/bash //usr/local/inotify/inotify.sh & #將腳本加入後臺執行注意你自己的腳本路徑
echo "nohup /bin/bash //usr/local/inotify/inotify.sh &" >> /etc/rc.local #加入開機啓動
當然 這些做晚了就等你測試咯!@~@
四測試及部分故障現象
問題一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端的目錄不存在或無權限,創建目錄並修正權限可解決問題。
問題二:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器端該模塊(backup)需要驗證用戶名密碼,但客戶端沒有提供正確的用戶名密碼,認證失敗。
提供正確的用戶名密碼解決此問題。
問題三:
@ERROR: Unknown module ‘backup'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服務器不存在指定模塊。提供正確的模塊名或在服務器端修改成你要的模塊以解決問題。
如有問題請聯繫[email protected],更多內容盡在碎片論壇www.debris.cn,歡迎各位蒞臨指導!!!