Chinaunix首页 | 论坛 | 博客
  • 博客访问: 306358
  • 博文数量: 79
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 848
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-11 15:29
文章分类

全部博文(79)

文章存档

2012年(1)

2011年(5)

2010年(19)

2009年(54)

我的朋友

分类: LINUX

2009-09-25 11:03:16

A Brief Introduction of Rsync

一个观点:通过sshtar为文件系统的某部分建立远程副本是非常理想的,而rsync则更适合用来使两台机器之间保持文件系统的同步。一般来说,tar用于最初的副本,而rsync则用于获取最后一份副本以来的所发生的更改。因为不存在任何文件系统时,tar可能比rysnc更快,但当两个文件系统之间的差异很小时,rysnc会比tar快得多。

下面介绍Rsync的一些特性,其中重要的三个:

  1. Diffs----它被设计成只传送文件的不同部分,而非传送整个文件,以加速文件传送。

  2. Compression----可选项。压缩文件的不同部分,进一步节省传输时间和减少网络负担。

  3. Secure Shell----可选项。通过ssh协议进行会话加密并进行文件传输。

其他一些特性:

  1. 支持符号链接,设备节点的拷贝,并保留文件的所有权信息(owners, groups and permissions)

  2. 类似GUN tar exclude exclude-from选项

  3. 支持类似CVS忽略相同文件的CVS exclude模式

  4. 不需要root权限

  5. 文件传输流水线操作,减少延迟

  6. 支持匿名和认证

Server

注:SERVER上只要安装好rsyncsshserver即可,此处以DEBIAN5.0为例子

root@Aurora ~# uname -a
Linux Aurora 2.6.27.6 #2 SMP PREEMPT Mon Nov 17 16:51:54 CST 2008 i686 GNU/Linux

安装openssh

root@Aurora ~#apt-get install ssh
root@Aurora ~# ssh -V
OpenSSH_5.1p1 Debian-4, OpenSSL 0.9.8g 19 Oct 2007

安装rsync

root@Aurora ~#apt-get install rsync
root@Aurora ~# rsync --version
rsync  version 3.0.4  protocol version 30
Copyright (C) 1996-2008 by Andrew Tridgell, Wayne Davison, and others.
Web site:
Capabilities:
    64-bit files, 64-bit inums, 32-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes
rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU General Public Licence for details.

 

Client


安装好sshrsync:请用源码安装的方式(略写)

bash-2.0.4#./configure –prefix=/usr && make && make install

或者把相应的库和可执行文件拷贝到相应的目录下

bash-2.0.4#ldd /usr/bin/ssh
       libdl.so.2        =>   /lib/libdl.so.2
       libnsl.so.1       =>   /lib/libnsl.so.1
       libz.so.1  =>   /usr/lib/libz.so.1
       libutil.so.1       =>   /lib/libutil.so.1
       libpam.so.1     =>   /lib/libpam.so.0
       libcrypto.so.0  =>   /usr/lib/libcrypto.so.0
       libc.so.6  =>   /lib/libc.so.6
       /lib/ld-linux.so.2     =>   /lib/ld-linux.so.2
bash-2.0.4#ldd /usr/bin/ssh-keygen(add,agent)
       libdl.so.2        =>   /lib/libdl.so.2
       libnsl.so.1       =>   /lib/libnsl.so.1
       libz.so.1  =>   /usr/lib/libz.so.1
       libutil.so.1       =>   /lib/libutil.so.1
       libpam.so.1     =>   /lib/libpam.so.0
       libcrypto.so.0  =>   /usr/lib/libcrypto.so.0
       libc.so.6  =>   /lib/libc.so.6
       /lib/ld-linux.so.2     =>   /lib/ld-linux.so.2
bash-2.0.4#ldd /usr/bin/rsync
       libc.so.6  =>   /lib/libc.so.6
       /lib/ld-linux.so.2     =>   /lib/ld-linux.so.2

安装后:

bash-2.0.4# ssh -V
SSH Version OpenSSH_2.1.1. Protocol 1.5/2.0
Compile with SSL (0x0090581f)
bash-2.0.4#rsync –version
rsync version 2.4.4 protocol version 24
Written with Andrew Tridgell and Paul Mackerras

Implement

Server

root@Aurora ~# cat /etc/ssh/sshd_config
# Package generated configuration file
# See the sshd(8) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

 
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

 
# Logging
SyslogFacility AUTH
LogLevel INFO

 
#Allowusers root netst netst

 
# Authentication:
LoginGraceTime 120
#PermitRootLogin yes //这个可以写成NO,保证其安全性
PermitRootLogin without-password //这个可以不开
StrictModes yes
RSAAuthentication yes //确认其为yes
PubkeyAuthentication yes //确认其位yes
AuthorizedKeysFile %h/.ssh/authorized_keys
#AuthorizedKeysFile /root/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no

 
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

 
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

 
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

 
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no //这里写成NO,这样不会要求密码认证

 
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

 
# GSSAPI options
#GSSAPIAuthentication no //把这个关掉
#GSSAPICleanupCredentials yes //把这个关掉

 
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

 
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes



Client

root@Aurora ~# cat /etc/ssh/ssh_config
# This is the ssh client systemwide configuration file. This file provides
# defaults for users, and the values can be changed in per-user configuration
# files or on the command line.
# Configuration data is parsed as follows:
# 1. command line options
# 2. user-specific file
# 3. system-wide file

 
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.
# Site-wide defaults for various options.
# Host *
# ForwardAgent no
# ForwardX11 yes
# RhostsAuthentication yes
# RhostsRSAAuthentication yes
# RSAAuthentication yes
# PasswordAuthentication yes
# FallBackToRsh no
# UseRsh no
# BatchMode no
# CheckHostIP yes
# StrictHostKeyChecking no
# IdentityFile ~/.ssh/identity
# Port 22
# Protocol 2,1
# Cipher 3des
# EscapeChar ~

 
# Be paranoid by default
Host *
ForwareAgent no
ForwareX11 yes
FallBackToRsh no
Protocol 2

在客户端生成dsa key

bash-2.0.4# ssh-keygen -d
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):

 
Enter passphrase (empty for no passphrase): <-- 此处不打passphrase..下次才不会询问password
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
11:22:33:44:55:66:77:88:99:00:11:22:33:44:55:66 root@firewall

并将id_dsa.pub拷贝到服务器上(不管采用什么方法),比如

DST=/home/backup/
 
rsync –ave  ssh  –progress –delete SRC DST

添加crontab: 命令:$ crontab –e,添加如下语句后保存

0 2  * * *  /home/bak/backupdata.sh

以后就每天凌晨2点执行

只要KEY安装正确,就能无人守候的情况下自动运行,know_hosts问题可在预先设定好。



Crontab格式

 

【基本参数原型】

i.e:  Rsync –avvve ssh –progress –delete –include “dowload/” –exclude “/*” blueelf@$SERVERIP:$SRC $DST

【注】此命令只下载服务器 dowload目录下的文件及文件夹


Client

 

dd命令创建一个需要限制的最大字节数的文件
然后用该文件创建loopback设备,然后将该设备挂接到你需要限制的目录就行了

root@Aurora /home/ftp/test# mkdir point
root@Aurora /home/ftp/test# l point/
总计 0
drwxr-xr-x 2 root root  48 12-29 15:53 .
drwxr-xr-x 5 root root 248 12-25 11:06 ..
 

比如:创建一个大小为8291k的文件(8291,不是8192)

root@Aurora /home/ftp/test# dd if=/dev/zero of=./file.img bs=1k count=8291
8291+0 records in
8291+0 records out
8489984 bytes (8.5 MB) copied0.0915864 秒,92.7 MB/
root@Aurora /home/ftp/test# ls -l file.img
-rw-r--r-- 1 root root 8489984 12-17 15:43 file.img

 
设置循环设备

root@Aurora /home/ftp/test# losetup /dev/loop1 file.img

将/dev/loop1格式化为一个ext3的磁盘

root@Aurora /home/ftp/test# mkfs -t ext3 /dev/loop1 8291
mke2fs 1.41.3 (12-Oct-2008)
warning: 98 blocks unused.
Filesystem label=
OS type: Linux
 
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2072 inodes, 8193 blocks
414 blocks (5.05%) reserved for the super user
First data block=1
Maximum filesystem blocks=8650752
1 block group
 
8192 blocks per group, 8192 fragments per group
2072 inodes per group
 
Writing inode tables: done                           
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

LOOP设备mountpoint目录,这样就将文件夹point限制到8291k

root@Aurora /home/ftp/test# mount -t ext3 /dev/loop1 point/
root@Aurora /home/ftp/test# cd point/
root@Aurora /home/ftp/test/point# l
总计 13
drwxr-xr-x 3 root root  1024 12-29 15:59 .
drwxr-xr-x 5 root root   248 12-29 15:58 ..
drwx------ 2 root root 12288 12-29 15:59 lost+found

【实验】错误如下

root@Aurora /home/ftp/test/point# rsync -avPe ssh blueelf@172.16.1.254:test3 ./
receiving file list ...
1 file to consider
test3
           0   0%    0.00kB/s    0:00:00 
rsync: write failed on "/home/ftp/test/point/test3": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(298) [receiver=3.0.4]
rsync: connection unexpectedly closed (71 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(632) [generator=3.0.4]

 

 

传输终止,结果与没传输之前完全一样

【注】:这些命令可以写成一个脚本,开机自动运行

server

可以同客户端

也可以单独建立一个分区

 

 

serverclient建立相同名字的用户,如果同为Linux,传输时加上-a选项,所有这些信息都一成不变的拷贝过来。

【注】:参数请看Rsync详细参数段

 

 

经过多次实验,总结如下:

(1)   同等条件下,用FTP传输,持续稳定在150kb/s左右;而Rsync的速度会在100kb/s-150kb/s之间波动。

(2)   有一种条件下会导致Rsync处于一直等待中,看起来无任何反应:正在传输一个超过263M的文件,此时把服务器端的相应文件删除。而此时只有按ctrl+c结束Rsync                    

(3)   Rsync工作在文件列表的一个时间点上,这个时间点上服务器的文件是什么样子,那写到客户端的文件就是什么样子,除非出现传输中断之类的错误。

(4)   多层目录,上千个小文件,亦或其中包含些大文件,对于rsync都不是问题。

 

 

详细参数

-v, --verbose 详细模式输出
-q, --quiet
精简输出模式
-c, --checksum
打开校验开关,强制对文件传输进行校验
-a, --archive
归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive
对子目录以递归模式处理
-R, --relative
使用相对路径信息
-b, --backup
创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir
将备份文件(~filename)存放在在目录下。
-suffix=SUFFIX
定义备份文件前缀
-u, --update
仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links
保留软链结
-L, --copy-links
想对待常规文件一样处理软链结
--copy-unsafe-links
仅仅拷贝指向SRC路径目录树以外的链结
--safe-links
忽略指向SRC路径目录树以外的链结
-H, --hard-links
保留硬链结
-p, --perms
保持文件权限
-o, --owner
保持文件属主信息
-g, --group
保持文件属组信息
-D, --devices
保持设备文件信息
-t, --times
保持文件时间信息
-S, --sparse
对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run
现实哪些文件将被传输
-W, --whole-file
拷贝文件,不进行增量检测
-x, --one-file-system
不要跨越文件系统边界
-B, --block-size=SIZE
检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND
指定替代rshshell程序
--rsync-path=PATH
指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude
使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing
仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete
删除那些DSTSRC没有的文件
--delete-excluded
同样删除接收端那些被该选项指定排除的文件
--delete-after
传输结束以后再删除
--ignore-errors
及时出现IO错误也进行删除
--max-delete=NUM
最多删除NUM个文件
--partial
保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force
强制删除目录,即使不为空
--numeric-ids
不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP
超时时间,单位为秒
-I, --ignore-times
不跳过那些有同样的时间和长度的文件
--size-only
当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM
决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR
DIR中创建临时文件
--compare-dest=DIR
同样比较DIR中的文件来决定是否需要备份
-P
等同于 --partial
--progress
显示备份过程
-z, --compress
对备份的文件在传输时进行压缩处理
--exclude=PATTERN
指定排除不需要传输的文件模式
--include=PATTERN
指定不排除而需要传输的文件模式
--exclude-from=FILE
排除FILE中指定模式的文件
--include-from=FILE
不排除FILE指定模式匹配的文件
--version
打印版本信息
--address
绑定到特定的地址
--config=FILE
指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT
指定其他的rsync服务端口
--blocking-io
对远程shell使用阻塞IO
-stats
给出某些文件的传输状态
--progress
在传输时现实传输过程
--log-format=FORMAT
指定日志文件格式
--password-file=FILE
FILE中得到密码
--bwlimit=KBPS
限制I/O带宽,KBytes per second
-h, --help
显示帮助信息

 

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