亞馬遜EC2服務器使用Rsync+Inotify實時同步

背景:


兩臺亞馬遜服務器之間需要數據同步,只要傳一臺機器上,就可以自動同步到其他機器上。


安裝Rsync:


CentOS 6.7自動就帶有Rsync,不需要安裝。


##實例分析
這裏假設有兩臺服務器:A和B。其中A是主web服務器(155.28.81.0),B服務器是從服務器(155.28.82.0)。我們要將A服務器的/home/test/備份到B服務器的/home/test/目錄下。

##服務器A 配置
####服務器A編譯安裝
rsync的編譯安裝非常簡單,只需要以下簡單的幾步:

[root@www ~]# cd /usr/local/src/
[root@www src]# wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz  
[root@www src]# tar zxvf rsync-3.0.9.tar.gz  
[root@www src]# cd rsync-3.0.9
[root@www rsync-3.0.9]# ./configure --prefix=/usr/local/rsync/
[root@www rsync-3.0.9]# make
[root@www rsync-3.0.9]# make install

但是需要注意的是必須在服務器A和B上都安裝rsync,其中A服務器上是以服務端模式(被動)運行rsync,而B上則以客戶端模式(主動)運行rsync。這樣在web服務器A上運行rsync守護進程,在B上定時運行客戶程序來備份web服務器A上需要備份的內容。

####建立用戶與密碼認證文件

[root@www rsync-3.0.9]# echo "backup:bk_passwd" > /usr/local/rsync/rsyncd.passwd

請記住,在server端建立的密碼文件,包含用戶名與密碼,而在client端建立的密碼文件只有密碼,沒有用戶名。

####設置權限爲只讀

[root@www rsync-3.0.9]# cd /usr/local/rsync
[root@www rsync]# chmod 600 rsyncd.passwd

否則可能會報錯:
@ERROR: auth failed on module ***
rsync error: error starting client-server protocol (code 5) at main.c(1503)

####建立rsync配置文件

[root@www rsync]# vi /usr/local/rsync/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
strict modes = yes
hosts allow = 121.42.46.213    #可以空格,允許多個
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[test]
path = /home/test
ignore errors
read only = true
list = false
auth users = backup
secrets file = /usr/local/rsync/rsyncd.passwd

####以守護進程方式啓動rsync服務器

[root@www rsync]# rsync --daemon --config=/usr/local/rsync/rsyncd.conf

rsync默認服務端口爲873,服務器在該端口接收客戶的匿名或者認證方式的備份請求。

####如果要讓服務設置爲自啓動,可以加入rc.local

編輯/etc/rc.d/rc.local,在最後添加:

/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf


##客戶端B 配置

編譯安裝同上,一般錯誤都會發生在服務器B

####建立訪問服務端A的密碼認證文件

[root@www rsync]# echo "bk_passwd" > /usr/local/rsync/rsync.passwd

####設置權限爲只讀

[root@www rsync]# chmod 0600 rsync.passwd

####在rsync安裝之後,運行以下指令同步備份

[root@www rsync]# rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd [email protected]::test /home/test

其中地址[email protected]::test,backup爲服務器A用戶,115.28.81.0爲服務器A IP地址或者域名,test爲服務器A配置模塊。

上面這個命令行中-vzrtopg裏的v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如屬主、時間的參數,--progress是指顯示出詳細的進度情況,--delete是指如果服務器端刪除了這一文件,那麼客戶端也相應把文件刪除,保持真正的一致。--password-file=/usr/local/rsync/rsync.passwd來指定密碼文件,這樣就可以在腳本中使用而無需交互式地輸入驗證密碼了,這裏需要注意的是這份密碼文件權限屬性要設得只有root可讀。

這裏將備份的內容存放在備份機的/home/test/目錄下。


####安裝inotify

[root@www rsync]# cd /usr/local/src/
[root@www src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  
[root@www src]# tar zxvf inotify-tools-3.14.tar.gz  
[root@www src]# cd inotify-tools-3.14  
[root@www inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify  
[root@www inotify-tools-3.14]# make  
[root@www inotify-tools-3.14]# make install


Rsync同步腳本:


#!/bin/bash
#variables
current_date=$(date +%Y%m%d_%H%M%S)
source_path=/home/www/
log_file=/usr/local/rsync/rsync_client.log

#rsync configuration
rsync_server=115.28.81.0
rsync_module=www
rsync_user=backup
rsync_pwd=/usr/local/rsync/rsync.passwd
INOTIFY_EXCLUDE='(.*/*\.log|.*/*\.swp)$'
INOTIFY_EXCLUDE_LIST='/usr/local/inotify/inotify_exclude.lst'
RSYNC_EXCLUDE='/etc/rsync_exclude.list'

#rsync client pwd check
if [ ! -e ${rsync_pwd} ];then
    echo -e "rsync client passwod file ${rsync_pwd} does not exist!"
    exit 0
fi

#inotify_function
#This function is used to monitor folder(/home/www) files, but exclude subfolders(storage,bootstrape/cache).
inotify_fun(){
    /usr/local/inotify/bin/inotifywait -mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f' \
           --exclude ${INOTIFY_EXCLUDE}  --fromfile ${INOTIFY_EXCLUDE_LIST}  -e modify,delete,create,move,attrib ${source_path} \
          | while read file
      do
          /usr/bin/rsync -auvrtzopgP --exclude-from=${RSYNC_EXCLUDE} --progress --bwlimit=500 --password-file=${rsync_pwd} ${source_path} ${rsync_user}@${rsync_server}::${rsync_module}
      done
}
#inotify log
inotify_fun >> ${log_file} 2>&1


其中INOTIFY_EXCLUDE_LIST='/usr/local/inotify/inotify_exclude.lst'如下:

[ec2-user@ip-172-31-7-248 rsync]$ cat /usr/local/inotify/inotify_exclude.lst
/home/www
@/home/www/www.xxx.com/storage
@/home/www/www.xxx.com/bootstrap/cache
@/home/www/xxx.com/storage


RSYNC_EXCLUDE='/etc/rsync_exclude.list'

[ec2-user@ip-172-31-7-248 rsync]$ cat /etc/rsync_exclude.list
www.xxx.com/storage
www.xxx.com/bootstrap/cache
xxx.com/storage


參考:

http://fanrong33.com/archives/89.html

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