评论: 使用rsync从linux到linux或到windows对网站进行镜像备份

一、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/

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

Posted by vitter at July 7, 2005 09:36 AM
发表评论













记住个人信息?