rsync中文手册之使用rsync实现网站镜像和备份linux

 更新时间:2008年09月17日 13:04:44   作者:  
用rsync实现网站镜像和备份 虽然是linux下的操作,但原理和windows下类似

服务器配置实例
那么在www.linuxaid.com.cn上创建rsyncd的配置文件/etc/rsyncd.conf,内容如下:
uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path = /www/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/backserver.pas
[web_user1]
path = /home/web_user1/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
uid = web_user1
gid = web_user1
auth users = backup
secrets file = /etc/backserver.pas
[web_user2]
path = /home/web_user2/
ignore errors
read only = true
list = false
hosts allow = 202.99.11.121
hosts deny = 0.0.0.0/32
uid = web_user2
gid = web_user2
auth users = backup
secrets file = /etc/backserver.pas
这里定义有四个三个模块,分别对应于三个需要备份的目录树。这里只允许202.99.11.121备份本机的数据,并且需要认证。三个模块授权的备份用户都为backup,并且用户信息保存在文件/etc/backserver.pas中,其内容如下:
backup:bk_passwd
并且该文件只能是root用户可读写的,否则rsyncd启动时会出错。这些文件配置完毕以后,就需要在A服务器上启动rsyncd服务器:
rsync --daemon
客户命令示例
/usr/local/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@202.99.11.120::www /backup/www/ --password-file=/etc/rsync.pass
上面这个命令行中-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数。-- progress是指显示出详细的进度情况,--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。-- exclude "logs/" 表示不对/www/logs目录下的文件进行备份。--exclude "conf/ssl.*/"表示不对/www/conf/ssl.*/目录下的文件进行备份。
backup@202.99.11.120::www 表示对该命令是对服务器202.99.11.120中的www模块进行备份,backup表示使用backup来对该模块进行备份。
--password-file=/etc/rsync.pass来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有root可读。
这里将备份的内容存放在备份机的/backup/www/目录下。
[root@linuxaid /]# /usr/local/bin/rsync -vzrtopg --delete --exclude "logs/" --exclude "conf/ssl.*/" --progress backup@202.99.11.120::www /backup/www/ --password-file=/etc/rsync.pass
receiving file list ... done
./
1
785 (100%)
1.py
4086 (100%)
2.py
10680 (100%)
a
0 (100%)
ip
3956 (100%)
./
wrote 2900 bytes read 145499 bytes 576.34 bytes/sec
total size is 2374927 speedup is 45.34
对其它两个模块操作的命令分别为:
/usr/local/bin/rsync -vzrtopg --delete --progress backup@202.99.11.120::web_user1 /backup/web_user1/ --password-file=/etc/rsync.pass
/usr/local/bin/rsync -vzrtopg --delete --progress backup@202.99.11.120::web_user2 /backup/web_user2/ --password-file=/etc/rsync.pass
可以将客户命令通过crontab -e命令来实现自动备份,如crontab -e:
 
一些示例脚本
这里这些脚本都是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
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

相关文章

最新评论