Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629435
  • 博文数量: 197
  • 博客积分: 4858
  • 博客等级: 上校
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-06 22:46
文章分类

全部博文(197)

文章存档

2011年(30)

2010年(21)

2009年(25)

2008年(80)

2007年(41)

分类: BSD

2007-11-04 15:21:19

1、什么是Rsync
Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。


  Rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配置文件。Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。


  Rsync支持大多数的类Unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。此外,它在windows平台下也有相应的版本,如cwRsync和Sync2NAS等工具。


  Rsync的基本特点如下:


  1.可以镜像保存整个目录树和文件系统;
  2.可以很容易做到保持原来文件的权限、时间、软硬链接等;
  3.无须特殊权限即可安装;
  4.优化的流程,文件传输效率高;
  5.可以使用rsh、ssh等方式来传输文件,当然也可以通过直接的socket连接;
  6.支持匿名传输。


2、Rsync同步算法
Rsync只所以同步文件的速度相当快,是因为“Rsync同步算法”能在很短的时间内计算出需要备份的数据,关于Rsync的同步算法描述如下:

假定在1号和2号两台计算机之间同步相似的文件A与B,其中1号对文件A拥有访问权,2号对文件B拥有访问权。并且假定主机1号与2号之间的网络带宽很小。那么rsync算法将通过下面的五个步骤来完成:

1、2号将文件B分割成一组不重叠的固定大小为S字节的数据块,最后一块可能会比S 小。

2、2号对每一个分割好的数据块执行两种校验:一种是32位的滚动弱校验,另一种是128位的MD4强校验

3、2号将这些校验结果发给1号。

4、1号通过搜索文件A的所有大小为S的数据块(偏移量可以任选,不一定非要是S的倍数),来寻找与文件B的某一块有着相同的弱校验码和强校验码的数据块。这项工作可以借助滚动校验的特性很快完成。

5、1号发给2号一串指令来生成文件A在2号上的备份。这里的每一条指令要么是对文件B经拥有某一个数据块而不须重传的证明,要么是一个数据块,这个数据块肯定是没有与文件B的任何一个数据块匹配上的。

3、Rsync参数说明
3.1 rsyncd.conf配置文件
现在以freebsd6.2平台来安装测试:

cd /usr/port/net/rsync

make install clean

添加开机启动:ee /etc/rc.conf

添加:rsyncd_enable="YES"

启动后编辑/usr/local/etc/rsync.conf文件。

这个文件是rsync安装后在目录生成的配置文件,

做一个简单的修改:# rsyncd.conf - Example file, see rsyncd.conf(5)
#

# Set this if you want to stop rsync daemon with rc.d scripts
pid file = /var/run/rsyncd.pid

# Edit this file before running rsync daemon!!

uid = nobody
gid = nobody
use chroot = no
max connections = 4
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
locak file = /var/run/rsyncd.lock
secrets file = /etc/rsyncd.passwd

#[ftp]
#       path = /var/ftp/pub
#       comment = whole ftp area (approx 6.1 GB)

#[sambaftp]
#       path = /var/ftp/pub/samba
#       comment = Samba ftp area (approx 300 MB)

#[rsyncftp]
#       path = /var/ftp/pub/rsync
#       comment = rsync ftp area (approx 6 MB)

#[sambawww]
#       path = /public_html/samba
#       comment = Samba WWW pages (approx 240 MB)

#[cvs]
#       path = /data/cvs
#       comment = CVS repository (requires authentication)
#       auth users = tridge, susan
#       secrets file = /usr/local/etc/rsyncd.secrets

[home]
comment = backup home
path = /home
read only = no
auth users = work
strict modes = true

然后保存,不退出系统,使更改生效:

cd /etc && sh rc,然后就会重新加载/etc/rc.conf文件。

netstat -ant

看一下有没有873端口,这是rsyncd的监听端口。

然后配置密码文件放入/etc 目录

echo "zbhdpx:zbhdpx" > /etc/rsyncd.passwd

chmod 600 /etc/rsyncd.passwd

改为600的权限,只有文件的拥有者才有读写权限。如果不加600权限,客户端连接时会提示错主,后边会说到。

然后到客户端,Centos 4.4.执行了rsync,哈哈,安装好了,省了我一大把力气。

先测试一下:

rsync -avzP --password-file=/rsyncd.passwd zbhdpx /home/work

把系统home这个目录备份到本地/home/work这个目录。

为了自动执行,先创建脚本:

vi /home/zbhdpx/rsync.sh

写入以下内容:

#!/bin/sh
rsync -avzP --delete --password-file=/etc/rsyncd.passwd zbhdpx /home/work

保存,退出,

chmod +x /home/zbhdpx/rsync.sh

加执行权限。

然后创建客户端的密码文件。上边我已经写好了文件的名字,就用这个了,

vi /etc/rsyncd.passwd

然后输入:(只输入密码就可以)不要再输入:“用户名:密码”这样的格式了,否则会执行产生:

@ERROR: auth failed on module home
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)

这个原因查找资料如下:这是别人的劳动成果,不是我的。借用一下:

  1. 经过测试终于明白了这个问题,我把它写出来,供大家参考:
    在Server端rsyncd.conf我是这样设置的:
    [vika]
    comment=”test”
    path=/data/test/rsync
    max connections=10
    syslog facility=local0
    read only=false
    writ only=true
    uid=nobody
    gid=nobody
    auth users=log,test
    secrets file=/data/test/rsync-2/etc/secert_file
    strict modes=false

    查看secert_file权限:
    -rw——- 1 root wheel 18 Dec 26 17:24 secert_file
    文件格式: log:yao
    test:test

    Linux Client的密码保存文件secert_file跟Server端的一样:
    看看它的权限:
    -rw——- 1 root root 4 12月 26 17:40 secert_file
    在Linux Client执行:
    rsync -vzrt0 filename –password-file=/opt/shell/secert_file
    还是需要密码提示:”password file must not be other-accessible”

    strict modes=false 这个选项在BSD上没什么作用哦!在官方是这么写道的:
    The “strict modes” option determines whether or not the permissions on the secrets file will be checked. If “strict modes” is true, then the secrets file must not be readable by any user ID other than the one that the rsync daemon is running under. If “strict modes” is false, the check is not performed. The default is true. This option was added to accommodate rsync running on the Windows operating system.

    于是我重新修改了rsyncd.conf中vika模块的配置:
    auth users=log
    strict modes=true

    Server和Client端的 secert_file 文件保持属主的600权限。文件只保存log的密码,格式”password”,而不需要这样 “user:password”。

    在执行 rsync -vzrt0 filename –password-file=/opt/shell/secert_file ,就不会提示 需要密码了。

    Comment by — Wednesday, December 27, 2006 @

  2. 对于RedHat Enterprise 4.0之间的两台进行测试的时候,由于rsync的版本低于官方rsync version 2.6.9,man rsyncd.conf中我们可以看到它没有address和port两个选项。
    它们的 密码保存方式不同于BSD,在BSD中,我们可以看到Server端secert_file的格式是直接保存密码,而不需要user:password 这样,否则在Client提示password file must not be other-accessible
    continuing without password file。
    而在RedHat Enterprise 4.0恰恰相反,Server端的secert_file 需要 user:password这样的格式,而Client直接password就行。

记得创建文件后,把权限改为600

chmod 600 /etc/rsyncd.passwd

否则公出现:

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

好了,创建OK了。

crontab -e

添加任务,进行自动备份。rsync功能强大,支持ssh加密传输。现在只是很小一部分,其它见官方文件。

 

阅读(2079) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~