<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,如需转载请自行联系原作者