<code>#/opt/web</code>
<code>SITE=</code><code>"正式web位址"</code>
<code>USER=</code><code>"root"</code>
<code>read</code> <code>-p </code><code>"please input password:"</code> <code>PASSWORD</code>
<code>RSYNC_OPTS=</code><code>"-e \\\"ssh -p22 -o StrictHostKeyChecking=no\\\" -lprRztWavzopg --log-file=/var/log/rsync.log"</code>
<code>SDIR=</code><code>"/opt/ued"</code>
<code>DDIR=</code><code>"/opt/ued"</code>
<code>filelist=</code><code>"/home/tongbu/filelist"</code>
<code>auto_rsync() {</code>
<code> </code><code>expect -c "</code><code>eval</code> <code>spawn -noecho </code><code>rsync</code> <code>--exclude .git $RSYNC_OPTS $1 $2</code>
<code> </code><code>expect \"*?assword:*\"</code>
<code> </code><code>send -- \"$PASSWORD\r\"</code>
<code> </code><code>expect eof"</code>
<code>}</code>
<code>sync</code><code>() {</code>
<code> </code><code>SRC=$1</code>
<code> </code><code>DEST=$2 </code>
<code> </code><code>auto_rsync $SRC $USER@$SITE:$DEST </code>
<code>cd</code> <code>$SDIR</code>
<code>for</code> <code>i </code><code>in</code> <code>`</code><code>cat</code> <code>$filelist`</code>
<code>do</code>
<code>src=`</code><code>echo</code> <code>$i|</code><code>sed</code> <code>'s%^\/%%g'</code><code>`</code>
<code>sync</code> <code>$src $DDIR</code>
<code>done</code>
嘗試着脫離xinetd的托管模式,直接以daemon的形式啟動rsync服務
A server端配置
cat /etc/rsyncd.conf
uid = root
gid = root
user chroot = no
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/run/rsyncd.log
[mysql]
path = /opt/xmysqlbak/
ignore errors
read only = no
list = no
hosts allow = x.0/255.255.255.0
auth users = xmysqlbak
secrets file = /etc/rsyncd.password
cat /etc/rsyncd.password
mysqlbak:mysqlbak
B client
使用指令同步即可
/usr/bin/rsync -avz --delete --progress /opt/mysqlbak/ mysqlbak@x::mysql --password-file=/etc/rsyncd.password
/usr/bin/rsync -vzrtopg --delete --progress
--delete參數 主删除,備份不會删除
rsync --exclude .git -azuvrtopg --log-file=/var/log/rsync.log src root@ip:dst
rsync --exclude .git -vzrtopg --delete --progress --log-file=/var/log/rsync.log src root@ip:dst
上面-azuvrtopg和-vzrtopg --delete --progress 使用對比
實際應用中,注意上面的參數
參考
http://www.ilanni.com/?p=8499
<a href="http://ilanni.blog.51cto.com/526870/1605200" target="_blank">http://ilanni.blog.51cto.com/526870/1605200</a>
rsync認證方式
rsync有兩種常用的認證方式,一種是rsync-daemon方式,另外一種是ssh方式。在平時使用過程,我們使用最多的是rsync-daemon方式
注意:在使用rsync時,伺服器和用戶端都必須安裝rsync程式
rsync-daemon認證
rsync在rsync-daemon認證方式下,預設監聽TCP的873端口。
rsync-daemon認證方式是rsync的主要認證方式,這個也是我們經常使用的認證方式。并且也隻有在此種模式下,rsync才可以把密碼寫入到一個檔案中。
注意:rsync-daemon認證方式,需要伺服器和用戶端都安裝rsync服務,并且隻需要rsync伺服器端啟動rsync,同時配置rsync配置檔案。用戶端啟動不啟動rsync服務,都不影響同步的正常進行。
rsync -avz /root/test -e ‘ssh -p1234’ [email protected]:/root/
配置
server端
在啟動xinetd服務之前,我們還需要配置檔案/etc/xinetd.d/rsync,如下:
vi /etc/xinetd.d/rsync
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = –daemon –config=/etc/rsyncd.conf
log_on_failure += USERID
}
cat << EOF >/etc/rsyncd.conf
EOF
vi /etc/rsyncd.conf
[backup]
path = /home/backup/
hosts allow = 192.168.12.0/255.255.255.0
auth users = test
[www]
path = /home/www/
auth users = apache
vi /etc/rsyncd.password
test:test
apache:apache
配置完畢後,我們還需要安裝xinetd軟體包,否則無法啟動xinetd服務。如下:
yum -y install xinetd
/etc/init.d/xinetd start
chkconfig xinetd on
netstat -tunlp |grep 873
參數
-v, –verbose詳細模式輸出。
-a, –archive歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性不變。
-z, –compress對備份的檔案在傳輸時進行壓縮處理。
–delete:删除那些DST中存在而在SRC中沒有的檔案。
3)rsync [OPTION]… SRC [SRC]… [USER@]HOST::DEST
rsync -avz /data [email protected]::backup –password-file=/etc/rsyncd.password
推送就是在用戶端上執行rsync指令,目的是把用戶端需要同步的檔案推送到伺服器上。
拉取也是在用戶端上執行rsync指令,目的是把伺服器上的檔案拉取到本地。
注意:無論是推送和拉取,rsync指令都是在用戶端執行,隻是指令的格式不同而已
如果是源碼方式安裝的rsync,我們可以使用rsync –daemon來啟動rsync。如下:
echo PATH=$PATH:/usr/local/bin/>>/etc/profile
source /etc/profile
rsync –daemon
ps aux |grep rsync
注意:上述指令行中,隻有rsync –daemon才是啟動rsync的指令。并且該指令啟動時,會預設加載/etc/rsyncd.conf檔案。
用戶端
apache
rsync -avz /home/back/* apache@xx::www --password-file=/etc/rsyncd.password xx代表server ip
另外ssh rsync
rsync.sh
#!/bin/bash
ROOT="/data/www/wwwroot/bbs.linuxtone.org/"
SITE="xx"
USER="root"
#PASSWORD="xxx"
read -p "please input password:" PASSWORD
RSYNC_OPTS="-e \\\"ssh -p22 -o StrictHostKeyChecking=no\\\" -azuv --bwlimit=150 --timeout=1200"
auto_rsync() {
expect -c "eval spawn -noecho rsync --exclude .git $RSYNC_OPTS $1 $2
expect \"*?assword:*\"
send -- \"$PASSWORD\r\"
expect eof"
sync() {
FILE=$(basename $1)
DEST=$(dirname $1)
SRC=$1
# download remote site file to current location
#auto_rsync $USER@$SITE:$ROOT$FILE $DEST
auto_rsync $SRC $USER@$SITE:$DEST
# update remote site file if newer than backup
#auto_rsync $1 $USER@$SITE:$ROOT
# Remote file Directory
sync "/opt/cdn"
實際操作中發現,此腳本,不能同步空目錄,如果備份機器沒有目錄,它隻會同步成一個檔案。結果就是檔案夾變成檔案了。需要修改
注意
chmod 600 /etc/rsyncd.password
yum -y install inotify-tools
vim /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon –config=/etc/rsyncd.conf
log_on_failure += USERID
最終的腳本
#使用,往filelist添加目錄或者檔案,比如/opt
VR=$1
#read -p "please input branch:" VR
rm -rf /opt/fabu/cdn
cd /opt/fabu/
git clone http://xx -b $VR
[ ! -d /opt/fabu/xx ] && exit 1
chown -R nginx.nginx /opt/fabu/
SITE="ip"
#PASSWORD="xx"
#RSYNC_OPTS="-e \\\"ssh -p22 -o StrictHostKeyChecking=no\\\" -azuvr --bwlimit=150 --timeout=1200 --log-file=/var/log/rsync.log"
#RSYNC_OPTS="-e \\\"ssh -p22 -o StrictHostKeyChecking=no\\\" -azuvrtopg --bwlimit=150 --timeout=1200 --log-file=/var/log/rsync.log"
RSYNC_OPTS="-e \\\"ssh -p22 -o StrictHostKeyChecking=no\\\" -vzrtopg --delete --progress --log-file=/var/log/rsync.log"
#og format = %t %a %m %f %b %t: time %f: file %b: transfered bytes
#DEST=$(dirname $1)
DEST=$2
sync "/opt/fabu/xx/" "/opt/xx/xx"
基于分支的手動同步
Date=`date +"%Y-%m-%d %H:%M:%S"`
git clone url -b $VR
[ ! -d /opt/fabu/cdn ] && exit 1
echo "$Date,版本$VR" >> /var/log/VR.log
sync "/opt/fabu/cdn/" "/opt/ued/cdn"
#使用方法 sh rsync.sh git分支
rsync -vzrtopg --delete --progress "-e ssh -p $PARTS" resin@$REMOTE_IP:$2 $3
更新
<code>rsync</code><code>+inotify或者sersync或者svn+puppet 300s</code>
<code>rsync</code><code>-vzrtopgl --progress --delete --exclude=.svn </code><code>/data/web/a</code><code>.</code><code>test</code><code>.cn</code><code>/test</code><code>@172.31.0.15::a.</code><code>test</code><code>.cn --password-</code><code>file</code><code>=</code><code>/shell/rsync-passwd/rsync</code><code>.</code><code>passwd</code>
<code>rsync</code> <code>-az --delete --exclude </code><code>"file10"</code> <code>/null/</code> <code>/xx/</code>
<code>将dirA的所有檔案同步到dirB内,并保留檔案的屬主,屬組,檔案權限等資訊</code>
<code>rsync</code> <code>-avz dirA/ dirB/</code>
<code>将dirA的所有檔案同步到dirB内,并删除dirB内多餘的檔案</code>
<code>rsync</code> <code>-avz --delete </code><code>/dirA</code> <code>dirB/</code>
<code>将dirA的所有檔案同步到dirB,但是在dirB内除了fileB3.txt這個檔案不删之外,其他的都删除</code>
<code>rsync</code> <code>-avz --delete --exclude </code><code>"fileB3.txt"</code> <code>dirA/ dirB/</code>
<code>将dirA目錄内的fileA1.txt和fileA2.txt不同步到dirB目錄内</code>
<code>rsync</code> <code>-avz --exclude=</code><code>"fileA1.txt"</code> <code>--exclude=</code><code>"fileA2.txt"</code> <code>dirA/ dirB/</code>
<code>将dirA目錄内的fileA1.txt和fileA2.txt不同步到dirB目錄内,并且在dirB目錄内删除多餘的檔案</code>
<code>rsync</code> <code>-avz --exclude=</code><code>"fileA1.txt"</code> <code>--exclude=</code><code>"fileA2.txt"</code> <code>--delete dirA/ dirB/</code>
<code>将dirA目錄内的fileA1.txt和fileA2.txt不同步到dirB目錄内,并且在dirB目錄内删除多餘的檔案,同時,如果dirB内有fileA2.txt和fileA1.txt這兩個被排除同步的檔案,仍然将其删除</code>
<code>rsync</code> <code>-avz --exclude=</code><code>"fileA1.txt"</code> <code>--exclude=</code><code>"fileA2.txt"</code> <code>--delete-excluded dirA/ dirB/</code>
本文轉自 liqius 51CTO部落格,原文連結:http://blog.51cto.com/szgb17/1838342,如需轉載請自行聯系原作者