rsync 端口更换(默认873)

一般使用默认端口的话, 在服务端的启动命令为: 
/usr/bin/rsync --address=192.168.1.23 --daemon 

如果在客户端需要换另外的端口侦听, 则使用 
/usr/bin/rsync --address=172.18.16.89 --port=8081 --daemon 

客户端命令: 
/usr/bin/rsync -vzrc --progress --bwlimit=300 /usr/local/tomcat/webapps/.gif 172.18.16.89::appLogo/ 

rsync: failed to connect to 172.18.16.89: Connection refused (111) 
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8] 


此时在客户端也必须指定端口, 不然会报错。 
改为 
/usr/bin/rsync -vzrc --port=8081 --progress --bwlimit=300 /usr/local/tomcat/webapps/.gif 172.18.16.89::appLogo/ 
就OK

三、FAQ 

Q:如何通过ssh进行rsync,而且无须输入密码? 
A:可以通过以下几个步骤 

1. 通过ssh-keygen在server A上建立SSH keys,不要指定密码,你会在~/.ssh下看到identity和identity.pub文件 
2. 在server B上的home目录建立子目录.ssh 
3. 将A的identity.pub拷贝到server B上 
4. 将identity.pub加到~[user b]/.ssh/authorized_keys 
5. 于是server A上的A用户,可通过下面命令以用户B ssh到server B上了 
e.g. ssh -l userB serverB 
这样就使server A上的用户A就可以ssh以用户B的身份无需密码登陆到server B上了。 

Q:如何通过在不危害安全的情况下通过防火墙使用rsync? 
A:解答如下: 

这通常有两种情况,一种是服务器在防火墙内,一种是服务器在防火墙外。 
无论哪种情况,通常还是使用ssh,这时最好新建一个备份用户,并且配置sshd仅允许这个用户通过RSA认证方式进入。 
如果服务器在防火墙内,则最好限定客户端的IP地址,拒绝其它所有连接。 
如果客户机在防火墙内,则可以简单允许防火墙打开TCP端口22的ssh外发连接就ok了。 

Q:我能将更改过或者删除的文件也备份上来吗? 
A:当然可以: 

你可以使用如:rsync -other -options -backupdir = ./backup-2000-2-13 ...这样的命令来实现。 
这样如果源文件:/path/to/some/file.c改变了,那么旧的文件就会被移到./backup-2000-2-13/path/to/some/file.c,这里这个目录需要自己 

手工建立起来 

Q:我需要在防火墙上开放哪些端口以适应rsync? 
A:视情况而定 

rsync可以直接通过873端口的tcp连接传文件,也可以通过22端口的ssh来进行文件传递,但你也可以通过下列命令改变它的端口: 

rsync --port 8730 otherhost:: 
或者 
rsync -e 'ssh -p 2002' otherhost: 

Q:我如何通过rsync只复制目录结构,忽略掉文件呢? 
A:rsync -av --include '*/' --exclude '*' source-dir dest-dir 

Q:为什么我总会出现"Read-only file system"的错误呢? 
A:看看是否忘了设"read only = no"了 

Q:为什么我会出现'@ERROR: invalid gid'的错误呢? 
A:rsync使用时默认是用uid=nobody;gid=nobody来运行的,如果你的系统不存在nobody组的话,就会出现这样的错误,可以试试gid = 

nogroup或者其它 

Q:绑定端口873失败是怎么回事? 
A:如果你不是以root权限运行这一守护进程的话,因为1024端口以下是特权端口,会出现这样的错误。你可以用--port参数来改变。 

Q:为什么我认证失败? 
A:从你的命令行看来: 

你用的是: 
> bash$ rsync -a 144.16.251.213::test test 
> Password: 
> @ERROR: auth failed on module test 

> I dont understand this. Can somebody explain as to how to acomplish this. 
> All suggestions are welcome. 

应该是没有以你的用户名登陆导致的问题,试试rsync -a max@144.16.251.213::test test 

四、一些可借鉴的脚本 

这里这些脚本都是rsync网站上的例子: 

1、每隔七天将数据往中心服务器做增量备份 

#!/bin/sh 

# This script does personal backups to a rsync backup server. You will end up 
# with a 7 day rotating incremental backup. The incrementals will go 
# into subdirectories named after the day of the week, and the current 
# full backup goes into a directory called "current" 
# tridge@linuxcare.com 

# directory to backup 
BDIR=/home/$USER 

# excludes file - this contains a wildcard pattern per line of files to exclude 
EXCLUDES=$HOME/cron/excludes 

# the name of the backup machine 
BSERVER=owl 

# your password on the backup server 
export RSYNC_PASSWORD=XXXXXX 


######################################################################## 

BACKUPDIR=`date +%A` 
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES 
--delete --backup --backup-dir=/$BACKUPDIR -a" 

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin 

# the following line clears the last weeks incremental directory 
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir 
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/ 
rmdir $HOME/emptydir 

# now the actual transfer 
rsync $OPTS $BDIR $BSERVER::$USER/current 

2、备份至一个空闲的硬盘 

#!/bin/sh 

export PATH=/usr/local/bin:/usr/bin:/bin 

LIST="rootfs usr data data2" 

for d in $LIST; do 
mount /backup/$d 
rsync -ax --exclude fstab --delete /$d/ /backup/$d/ 
umount /backup/$d 
done 

DAY=`date "+%A"` 

rsync -a --delete /usr/local/apache /data2/backups/$DAY 
rsync -a --delete /data/solid /data2/backups/$DAY 

3、对vger.rutgers.edu的cvs树进行镜像 

#!/bin/bash 

cd /var/www/cvs/vger/ 
PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin 

RUN=`lps x | grep rsync | grep -v grep | wc -l` 
if [ "$RUN" -gt 0 ]; then 
echo already running 
exit 1 
fi 

rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog $HOME/ChangeLog 

sum1=`sum $HOME/ChangeLog` 
sum2=`sum /var/www/cvs/vger/CVSROOT/ChangeLog` 

if [ "$sum1" = "$sum2" ]; then 
echo nothing to do 
exit 0 
fi 

rsync -az --delete --force vger.rutgers.edu::cvs/ /var/www/cvs/vger/ 
exit 0 

4、利用find的一种巧妙方式 

rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/ 

可以用这种方法列出需要备份的文件列表——这种方法似乎比较少人用到。 

五、参考资料: 

1、http://rsync.samba.org/ 
2、rsync examples 
3、rsync FAQ

原文地址:https://www.cnblogs.com/wangmo/p/7238733.html