rsync同步
mall1和mall2、mall3的同步
mall2、3同步mall1上更新的内容:此時:mall2、3為伺服器端,mall1為用戶端
mall2、mall3上的安裝配置:
===========================================================================================
apt-get install -y rsync(安裝rsync)
sudo find / -name rsyncd.conf
/usr/share/doc/rsync/examples/rsyncd.conf(rsync的配置檔案的預設安裝路徑)
sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/ (複制配置檔案到/etc/目錄下)
sudo vi /etc/rsyncd.conf(編輯配置檔案為你需要的内容)
[global]##全局選項
uid = ubuntu#指定該子產品傳輸檔案時守護程序應該具有的uid
gid = sudo#指定該子產品傳輸檔案時守護程序應該具有的gid
use chroot = no##是否讓程序離開工作目錄
max connections = 10#用戶端最大連接配接數,預設0(沒限制)
pid file = /var/run/rsyncd.pid#運作程序的ID寫到哪裡
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log#日志記錄檔案
[mall] # 這裡是認證的子產品名,在client端需要指定
path=/home/ubuntu/data/web/mall.hiigame.com# 需要做備份的目錄
comment = update #備注同步項
ignore errors#可以忽略一些無關的IO錯誤
read only = no#no用戶端可上傳檔案,yes隻讀
list = no #用于設定當客戶請求可以使用的子產品清單,該子產品是否被列出,預設為true
host allow = 10.133.200.125#充許連接配接連接配接的主機(*為任何主機)
auth users = test # 認證的使用者名,如果沒有這行,則表明是匿名
uid = ubuntu
gid = sudo
secrets file = /etc/rsync.pass # 指定認證密碼檔案位置
[statics]
path=/home/ubuntu/data/web/statics.hiigame.com
comment = update
ignore errors
read only = no
list = no
host allow = 10.133.200.125
auth users = test
secrets file = /etc/rsync.pass
編輯認證密碼檔案:檔案名與配置檔案中的路徑一樣
vi /etc/rsync.pass
test:123456789(認證使用者名:認證密碼)
chmod 600 /etc/rsync.pass (修改權限,隻有root使用者才可執行)
(備注)注意:密碼檔案的權限,是由rsyncd.conf裡的參數
strict modes =yes/no 來決定:#是否檢查密碼檔案的權限
rsync的啟動:sudo /etc/init.d/rsync (start| restart| stop)
==========================================================================================
ubuntu@VM-200-27-ubuntu:~$ ps -aux | grep rsync (rsync程序)
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
root 1109 0.0 0.0 11004 732 ? S 2015 0:05 /usr/bin/rsync --no-detach --daemon --config /etc/rsyncd.conf
ubuntu@VM-200-27-ubuntu:~$ netstat -an | grep 873 (rsync端口)
tcp 0 0 10.133.200.27:873 0.0.0.0:* LISTEN
用戶端的配置:
apt-get intall rsync inotify-tools (安裝用戶端的配置)
#sudo vim /etc/rsync.pass(與伺服器端互相對應,密碼檔案 ,隻需要有密碼即可)
123456789
編寫監控腳本并加載到背景執行
vi /home/ubuntu/data/bin/rsync_mall.sh
#!/bin/bash
src=/home/ubuntu/data/web/mall.hiigame.com/
des=mall#子產品标簽
host1="10.133.200.27"#監控主機ip
host2="10.133.200.135"
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib,move $src | while read files
do
rsync -qzrtopg --exclude-from=/home/ubuntu/data/bin/rsync.list --delete --progress --password-file=/etc/rsync.pass $src test@$host1::$des
rsync -qzrtopg --exclude-from=/home/ubuntu/data/bin/rsync.list --delete --progress --password-file=/etc/rsync.pass $src test@$host2::$des
done
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
案例二
腳本路徑/home/ubuntu/
vi gameserver.sh
src=/home/ubuntu/data/nfs/
des1=game
host1="10.133.193.230"
host2="10.105.16.128"
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib,move $src | while read files
rsync -qzrtopg --delete --progress --password-file=/etc/rsync.pass $src test@$host1::$des1
rsync -qzrtopg --delete --progress --password-file=/etc/rsync.pass $src test@$host2::$des1
# rsync -qzrtopg --delete --progress --exclude-from=/home/wolfplus/bin/rsync.list --password-file=/etc/rsync.pass $src test@$host6::$des6
done
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
放在背景執行
./gameserver.sh &
====================================================================================
inotify之inotifywait指令常用參數詳解
[root@inotify-master inotify-tools-3.14]# cd /usr/local/inotify-3.14/
[root@inotify-master 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 #檔案系統被解除安裝
本文轉自 蔡小趙 51CTO部落格,原文連結:http://blog.51cto.com/zhaopeiyan/1731459