Linux下rsync服务器的安装和配置2

六、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-on
ly file system"的错误呢?

  A:看看是否忘了设"read only = no"了

  Q:为什么我会出现'@ERROR: invalid gid'的错误呢?

  A:rsync使用时默认是用uid=nobody;gid=nobody来运行的,如果你的系统不存在nobody组的话,就会出现这样的错误,可以试试gid = ogroup或者其它

  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

  Q: 出现以下这个讯息, 是怎么一回事?
  @ERROR: auth failed on module xxxxx
  rsync: connection unexpectedly closed (90 bytes read so far)
  rsync error: error in rsync protocol data stream (code 12) at io.c(150)

  A: 这是因为密码设错了, 无法登入成功, 请再检查一下 rsyncd.secrets 中的密码设定, 二端是否一致?

  Q: 出现以下这个讯息, 是怎么一回事?

  password file must not be other-accessible 
  continuing without password file 
  Password:

  A: 这表示 rsyncd.secrets 的档案权限属性不对, 应设为 600。请下 chmod 600 rsyncd.secrets

  Q: 出现以下这个讯息, 是怎么一回事?

  @ERROR: chroot failed
  rsync: connection unexpectedly closed (75 bytes read so far)
  rsync error: error in rsync protocol data stream (code 12) at io.c(150)

  A: 这通常是您的 rsyncd.conf 中的 path 路径所设的那个目录并不存在所致.请先用 mkdir开设好备份目录. 

  七、一些可借鉴的脚本

  这里这些脚本都是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"
  # [email=tridge@linuxcare.com]tridge@linuxcare.com[/email]

  # 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/

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

  http://www.linuxsir.org/main/?q=node/256
  http://www.backup.idv.tw/good_doc/li...0726/rsync.htm
  http://blog.chinaunix.net/u2/79180/s...php?id=1859608
  http://www.xfocus.net/articles/200107/214.html
posted @ 2010-01-21 14:57  飞鸿无痕  阅读(168)  评论(0编辑  收藏  举报