一、下载、安装rsync

下载http://rsync.samba.org/ftp/rsync/

解包rsync-2.6.9.tar.gz

#tar zxvf rsync-2.6.9.tar.gz

#cd rsync-2.6.9

编译安装

#./configure

#make

#make install

 

二、启动rsync server

启动rsync服务端

#rsync –daemon

rsync服务端启动两种方式

1.daemon 2.xinetd inetd启动后绑在 TCP/873 端口

a、加入inetd.conf

编辑/etc/services,加入rsync 873/tcp,指定rsync的服务端口是873

#vi /etc/services

rsync 873/tcp

编加/etc/inetd.conf,加入rsync stream tcp nowait root /bin/rsync rsync –daemon

#vi /etc/inetd.conf

rsync stream tcp nowait root /bin/rsync rsync –daemon

b、加入rc.local

在各种操作系统中,rc文件存放位置不尽相同,可以修改使系统启动时rsync –daemon加载进去。

#rsync –daemon

 

三、配置rsync server

1、建立备份目录区

在你的备份服务器上划分一块空间用来备份数据

#mkir backup

2、配置/etc/xinetd.d/rsync

#vi /etc/xinetd.d/rsync

修改disable = yes

disable = no

打开rsync daemon,让配置生效

#service xinetd restart

检查rsync

#netstat -a | grep rsync

3、配置/etc/rsyncd.conf(需要手动生成)

rsyncd.conf分全局参数和模块参数

#vi /etc/rsyncd.conf

全局参数

uid = nobody

gid = nobody

use chroot = no //不使用chroot

max connections = 4 // 最大连接数为4

pid file = /var/run/rsyncd.pid //pid文件的存放位置

lock file = /var/run/rsync.lock //锁文件的存放位置

log file = /var/log/rsyncd.log //日志记录文件的存放位置

模块参数

[web_test] // 这里是认证的模块名,在client端需要指定

path = /home/web_test/ // 需要做镜像的目录,不可缺少!

comment = This is a test //这个模块的注释信息

ignore errors // 可以忽略一些无关的IO错误

read only = yes // 只读

list = no // 不允许列文件

auth users = test // 认证的用户名,如果没有这行,则表明是匿名,此用户与系统无关

secrets file = /etc/backserver.pas // 认证文件名

hosts allow = 1.1.1.1,2.2.2.2 //允许主机

hosts deny = 0.0.0.0/0 //禁止主机

transfer logging = yes

log format = "%a %f %l"

#log format = "%o %h [%a] %m (%u) %f %l"

4、配置rsync密码 /etc/backserver.pas

格式(一行一个用户)

账号:密码

如以下例子:

test:ILoveOLS3

权限:要将rsyncd.secrets设置为root拥有, 且权限为600。

#chown root.root rsyncd.secrets

#chmod 600 rsyncd.secrets

 

四、配置rsync client

1、设定密码rsyncd.secrets

#vi rsyncd.secrets

ILoveOLS3

只需包含一行密码,且权限为600

2、测试指令

/usr/bin/rsync -vzrtopg –progress –delete test@192.168.0.217::web_test /home/quack/backup/$DATE –password-

file=/etc/rsync.pass > /var/log/rsync.$DATE

3、放入crontab

#crontab –e

 

五、rsync命令参数

-v表示verbose详细显示

-z表示压缩

-r表示recursive递归

-t表示保持原文件创建时间

-o表示保持原文件属主

-p表示保持原文件的参数

-g表示保持原文件的所属组

-a存档模式

-P表示代替-partial和-progress两者的选项功能

-e ssh建立起加密的连接。

–partial阻止rsync在传输中断时删除已拷贝的部分(如果在拷贝文件的过程中,传输被中断,rsync的默认操作是撤消前操作,即从目标机上

删除已拷贝的部分文件。)

–progress是指显示出详细的进度情况

–delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。

–exclude不包含/ins目录

–size-only 这个参数用在两个文件夹中的差别仅是源文件夹中有一些新文件,不存在重名且被修改过的文件,因为这种文件有可能会因为内

容被修改可大小一样,而被略过。这个参数可以大大地提高同步的效率,因为它不需要检查同名文件的内容是否相同。

–password-file来指定密码文件,内容包含server端指定认证用户的密码。

这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

test@192.168.0.217::web_test

test是指server端指定认证的用户

192.168.0.217是指服务器端的ip

::web_test 表示服务器端需要同步的模块名称;

/home/quack/backup/$DATE是同步后的文件指存放在本机的目录地址。

/var/log/rsync.$DATE是同步后的日志文件存放在本机的目录地址。

注意

不放/ 则目录名也包含mirror,放 / 则只有目录里面的东西mirror了

 

六、安全性

建议使用 ipchains 或 iptables 指令, 来限制 rsync client 的联机范围, 例子如下:

ipchains -A input -p tcp -s ! 11.22.33.44 –dport 873 -j DENY

iptables -A INPUT -p tcp -s ! 11.22.33.44 –dport 873 -j DROP

如此, 只有 11.22.33.44 这个 client IP 能连入这台 rsync server.

ipchains 及 iptables 的用法, 可参考小弟写的这份讲义: Linux防火墙入门

另, rsync 可以结合 ssh 或 ssh2, 使安全性更加提高. 详情请自行查阅 rsync / ssh / ssh2 的 manpage.

 

七、远程使用

rsync 1.1.1.1:: 只列list

rsync user@1.1.1.1::

rsync user@1.1.1.1::module

rsync user@1.1.1.1::module/path

如果没有配置rsync server 可以通过 ssh 方式使用

rsync -e ssh -auvPz 1.1.1.1:/etc ./

 

八、这里这些脚本都是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/

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

 

九、FQA

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

Q:当rsync遇到ssh不是标准端口时…

If ssh port is not 22, how does rsync specify this in command line?

A:rsync要借助ssh来运行,那么当ssh的端口不是默认的22怎么办?

把选项中的-e ssh 换成 -e ssh -p 12345

这里12345是ssh的端口号。

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 开设好备份目录.