rsync详解二

来源:互联网 发布:爱苹果软件 编辑:程序博客网 时间:2024/06/04 01:20

rsync详解二

4、Rsync使用实例

4.1、SSH方式

1、首先在服务端启动ssh服务:

# service sshd start

启动 sshd: [确定]

 

2、使用rsync进行同步

接下来就可以在客户端使用rsync命令来备份服务端上的数据了,SSH方式是通过系统用户来进行备份的,如下:

# rsync -vzrtopg --progress -e ssh --delete work@172.16.78.192:/www/* /databack/experiment/rsync

work@172.16.78.192' s password:

receiving file list ...

5 files to consider

test/

a

0 100% 0.00kB/s 527:35:41 (1, 20.0% of 5)

b

67 100% 65.43kB/s 0:00:00 (2, 40.0% of 5)

c

0 100% 0.00kB/s 527:35:41 (3, 60.0% of 5)

dd

100663296 100% 42.22MB/s 0:00:02 (4, 80.0% of 5)

 

sent 96 bytes received 98190 bytes 11563.06 bytes/sec

total size is 100663363 speedup is 1024.19

上面的信息描述了整个的备份过程,以及总共备份数据的大小。

 

4.2 、后台服务方式

 

1、启动rsync服务

编辑/etc/xinetd.d/rsync文件,将其中的disable=yes改为disable=no,并重启xinetd服务,如下:

# vi /etc/xinetd.d/rsync

#default: off

# description: The rsync server is a good addition to an ftp server, as it \

# allows crc checksumming etc.

service rsync

{

disable = no

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemon

log_on_failure += USERID

}

 

# /etc/init.d/xinetd restart

停止 xinetd: [确定]

启动 xinetd: [确定]

 

2、创建配置文件

默认安装好rsync程序后,并不会自动创建rsync的主配置文件,需要手工来创建,其主配置文件为“/etc/rsyncd.conf”,创建该文件并插入如下内容:

# vi /etc/rsyncd.conf

uid=root

gid=root

max connections=4

log file=/var/log/rsyncd.log

pid file=/var/run/rsyncd.pid

lock file=/var/run/rsyncd.lock

secrets file=/etc/rsyncd.passwd

hosts deny=172.16.78.0/22

 

[www]

comment= backup web

path=/www

read only = no

exclude=test

auth users=work

 

 

3、创建密码文件

采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,最好不要和系统帐户一致,同时要把创建的密码文件权限设置为600,这在前面的模块参数做了详细介绍。

# echo " work:abc123" > /etc/rsyncd.passwd

# chmod 600 /etc/rsyncd.passwd

 

4、备份

完成以上工作,现在就可以对数据进行备份了,如下:

# rsync -avz --progress --delete work@172.16.78.192::www /databack/experiment/rsync

Password:

receiving file list ...

6 files to consider

./ files...

a

0 100% 0.00kB/s 528:20:41 (1, 50.0% of 6)

b

67 100% 65.43kB/s 0:00:00 (2, 66.7% of 6)

c

0 100% 0.00kB/s 528:20:41 (3, 83.3% of 6)

dd

100663296 100% 37.49MB/s 0:00:02 (4, 100.0% of 6)

 

sent 172 bytes received 98276 bytes 17899.64 bytes/sec

total size is 150995011 speedup is 1533.75

 

5、恢复

当服务器的数据出现问题时,那么这时就需要通过客户端的数据对服务端进行恢复,但前提是服务端允许客户端有写入权限,否则也不能在客户端直接对服务端进行恢复,使用rsync对数据进行恢复的方法如下:

# rsync -avz --progress /databack/experiment/rsync/ work@172.16.78.192::www

Password:

building file list ...

6 files to consider

./

a

b

67 100% 0.00kB/s 0:00:00 (2, 66.7% of 6)

c

 

sent 258 bytes received 76 bytes 95.43 bytes/sec

total size is 150995011 speedup is 452080.87

 

 

 

5、示例脚本

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


6、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 tes




更多参考:

AIX mount RedHat的NFS

误删除/dev/dsk 和/dev/rdsk 下的文件怎么办?

unary operator expected

bash: /root/.bash_profile: line 15: syntax error: unexpected end of file

Linux下如何查看文件秒级修改及访问时间

EM乱码解决

linux里端口转发

windows xp 下使用FileZilla密钥

java.net.SocketException: Too many open files 问题的解决办法

UNExcepted inconsistency; run fsck manually

如何修改linux的mac地址?

mysqldump: Got error: 1066: Not unique table/alias

rsync详解一

rsync详解二

更改mysql的默认安装目录

httpd: apr_sockaddr_info_get() failed for centos1113

Real domain name required for sender address

Connection refused by [127.0.0.1]

MySQL bin_log文件占用空间太大

Centos 5 多路径配置步骤

rpc mount export: RPC: Unable to receive; errno = No route to host

nohup和screen的比较

vmware workstation 8 共享磁盘



原创粉丝点击