一。下載安裝 php
1.yum安裝。 linux
yum install rsync c++
2編譯安裝。 git
tar -zxvf rsync-3.0.3.tar.gz github
cd rsync-3.0.3 web
./configure express
make centos
make install api
二。服務器配置(1.238) 安全
案例說明,如192.168.1.237和192.168.1.238兩臺服務器,OS(centos6.2),192.168.1.238當服務器,192.168.1.237作client。當1.238文件刪除時1.237同步刪除,當1.238添加/修改時1.237同步修改。
一、採用系統默認安裝的rsync 編輯/etc/rsyncd.conf文件,若是沒有則新建一個
vi /etc/rsyncd.conf
strict modes = yes
port = 873
logfile = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
max connections = 4
[web]
uid = root
gid = root
path = /home/wwwroot/pmedia/images/tmp/ //同步路徑
read only = no
host allow = *
auth users = wwyhy //認證用戶名(不是系統用戶)
secrets file = /etc/rsyncd.scrt //密碼文件
2.添加一個密碼文件
vi /etc/rsyncd.scrt
wwyhy:123456
3.改變權限爲600
chmod 600 /etc/rsyncd.scrt
4.啓動服務(如開有防火牆請容許873端口經過)
rsync --daemon --config=/etc/rsyncd.conf
三。client配置和同步
1.client新建密碼文件
vi /etc/rsyncd.scrt
123456
2.修改權限
chmod 600 /etc/rsyncd.scrt
3.開始同步
rsync -avz --delete --password-file=/etc/rsyncd.scrt wwyhy@192.168.1.238::web /qq
4。創建腳本
vi /root/rsync.sh
#!/bin/bash
while :
do
PATH1=/qq
rsync -avz --delete --password-file=/etc/rsyncd.scrt wwyhy@192.168.1.238::web $PATH1 >> /var/log/rsync_client.log
#5s 循環一次
sleep 5
done
nohup /bin/bash /root/rsync.sh & 啓動腳本
四。注意事項和命令說明。
1.配置文件和密碼文件不要有註釋行和多餘的空格。密碼文件權限統一是600.
一。inotify+rsync同步部署部署環境:
rsync服務器:192.168.0.1
文件服務器and rsync客戶端:192.168.0.2ify-tools安裝1.rsync安裝配置:
http://hi.baidu.com/867358162/item/33f26ceded746919560f1dc0
2.rsync:
介紹 Inotify 是文件系統事件監控機制,做爲 dnotify 的有效替代。dnotify 是較早內核支持的文件監控機制。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) 注:上面所說的文件也包括目錄。
Linux內核的支持
在源主機中安裝inotify-tools-3.13.tar.gz
下載:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@Linux-Master src]# tar zxvf inotify-tools-3.13.tar.gz
[root@Linux-Master src]# cd inotify-tools-3.13
[root@Linux-Master inotify-tools-3.13]# ./configure
[root@Linux-Master inotify-tools-3.13]# make && make install
========================================
命令:
Inotifywait監聽
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,attrib,move,close_write,create,delete /root/ //當/root/目錄后里面的子目錄發生改變時 會打印出來
Inotifywatch監聽統計
#! /bin/bash
PATH1=/home/wwwroot/pmedia/public/
PATH2=/home/wwwroot/pmedia/protected/views/www/website/
IP=192.168.1.235
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,attrib,move,close_write,create,delete $PATH1 $PATH2 | while read file
do
chmod -R 777 $PATH1
chmod -R 777 $PATH2
/usr/bin/rsync -auvzrog --delete --password-file=/etc/rsyncd.client $PATH1 wwyhy@$IP::web >> /var/log/rsync_clinet.log
/usr/bin/rsync -auvzrog --delete --password-file=/etc/rsyncd.client $PATH2 wwyhy@$IP::web2 >> /var/log/rsync_clinet.log
# echo "$PATH1 rsync OK!!!!!!!!! "
done
啓動腳本:
nohup /home/scritp/rsync_inoticy.sh &
測試修改/home/wwwroot/pmedia/public/ ,/home/wwwroot/pmedia/protected/views/www/website/ 目錄文件 查看192.168.1.235是否同步
二。 sersync部署同步
sersync主要用於服務器同步,web鏡像等功能。基於boost1.43.0,inotify api,rsync command.開發。目前使用的比較多的同步解決方案是inotify-tools+rsync ,另一個是google開源項目Openduckbill(依賴於inotify- tools),這兩個都是基於腳本語言編寫的。相比較上面兩個項目,本項目優勢是:
sersync是使用c++編寫,並且對linux系統文件系統產生的臨時文件和重複的文件操做進行過濾(詳細見附錄,這個過濾腳本程序沒有實現),因此在結合rsync同步的時候,節省了運行時耗和網絡資源。所以更快。
相比較上面兩個項目,sersync配置起來很簡單,其中bin目錄下已經有基本上靜態編譯的2進制文件,配合bin目錄下的xml配置文件直接使用便可。
另外本項目相比較其餘腳本開源項目,使用多線程進行同步,尤爲在同步較大文件時,可以保證多個服務器實時保持同步狀態。
本項目有出錯處理機制,經過失敗隊列對出錯的文件從新同步,若是仍舊失敗,則按設定時長對同步失敗的文件從新同步。
本項目自帶crontab功能,只需在xml配置文件中開啓,便可按您的要求,隔一段時間總體同步一次。無需再額外配置crontab功能。
本項目socket與http插件擴展,知足您二次開發的須要。
rsync服務器:192.168.0.1
文件服務器and rsync客戶端:192.168.0.2 安裝sersync
1.rsync安裝配置:
http://hi.baidu.com/867358162/item/33f26ceded746919560f1dc0
下載配置sersync
http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz 64bit
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x86
vi confxml.xml
...................
<sersync>
<localpath watch="/root/ss">
<remote ip="192.168.0.1" name="web"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
// <auth start="false" users="wwyhy" passwordfile="/etc/rsync.client"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
.....................................................
啓動
./sersync2 -r
./sersync2 -r -d 守護進程運行