您的位置:首页 > 运维架构 > Linux

Linux远程备份工具Rsync使用案例

2011-11-16 16:53 876 查看
转自:
http://os.51cto.com/art/201009/225962.htm
Linux远程备份工具Rsync使用案例

Rsync是一个远程数据同步工具,可通过LAN或互联网快速同步多台主机间的文件。Rsync具有快速、安全、占用带宽少以及无需特权等特色。本文举例介绍了15种使用Rsync进行远程数据同步的方法,帮助大家更好的学习Rsync的使用。
AD:
参考
:http://www.thegeekstuff.com/2010/09/rsync-command-examples/
注:下面所有例子中 –
-之间实际上是没有空格的,使用时请删除空格。

Rsync是一个远程数据同步工具,可通过LAN或互联网快速同步多台主机间的文件。Rsync本来是用以取代
rcp的一个工具,它当前由rsync.samba.org
维护。Rsync使用所谓的”Rsync演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

Rsync的特色:
快速:第一次同步时
rsync会复制全部内容,但在下一次只传输修改过的文件。
安全:rsync允许通过
ssh协议来加密传输数据。
更少的带宽:rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
特权:安装和执行
rsync无需特别的权限

基本语法:
rsyncoptions source destination

源和目标都可以是本地或远程,在进行远程传输的时候,需要指定登录名、远程服务器及文件位置
样例:
1在本地机器上对两个目录进行同步
$rsync -zvr /var/opt/installation/inventory/ /root/temp
buildingfile list … done
sva.xml
svB.xml
.
sent26385 bytes received 1098 bytes 54966.00 bytes/sec
totalsize is 44867 speedup is 1.63
$

参数:
-z开启压缩
-v详情输出
-r表示递归

2利用
rsync -a让同步时保留时间标记

rsync选项
-a 称为归档模式,执行以下操作
递归模式
保留符号链接
保留权限
保留时间标记
保留用户名及组名
$rsync -azv /var/opt/installation/inventory/ /root/temp/
buildingfile list … done
./
sva.xml
svB.xml
.
sent26499 bytes received 1104 bytes 55206.00 bytes/sec
totalsize is 44867 speedup is 1.63
$

3仅同步一个文件
$rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys

sent42 bytes received 12380 bytes 3549.14 bytes/sec
totalsize is 12288 speedup is 0.99

4从本地同步文件到远程服务器
$rsync -avz /root/temp/thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/
Password:
buildingfile list … done
./
rpm/
rpm/Basenames
rpm/Conflictname

sent15810261 bytes received 412 bytes 2432411.23 bytes/sec
totalsize is 45305958 speedup is 2.87

就像你所看到的,需要在远程目录前加上ssh
登录方式,格式为username@machinename:path

5同步远程文件到本地

和上面差不多,做个相反的操作
$rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/
rpm/Basenames
.
sent406 bytes received 15810230 bytes 2432405.54 bytes/sec
totalsize is 45305958 speedup is 2.87

6同步时指定远程
shell

用 -e参数可以指定远程
ssh,比如用
rsync -e ssh来指定为 ssh
$rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/
rpm/Basenames

sent406 bytes received 15810230 bytes 2432405.54 bytes/sec
totalsize is 45305958 speedup is 2.87

7不要覆盖被修改过的目的文件

使用
rsync-u 选项可以排除被修改过的目的文件
$ls -l /root/temp/Basenames
total39088
-rwxr-xr-x1 root root 4096 Sep 2 11:35 Basenames

$rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receivingfile list … done
rpm/

sent122 bytes received 505 bytes 114.00 bytes/sec
totalsize is 45305958 speedup is 72258.31

$ls -lrt
total39088
-rwxr-xr-x1 root root 4096 Sep 2 11:35 Basenames

8仅仅同步目录权(不同步文件)

使用
-d参数
$rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ .
Password:
receivingfile list … done
logrotate.status
CAM/
YaST2/
acpi/

sent240 bytes received 1830 bytes 318.46 bytes/sec
totalsize is 956 speedup is 0.46

9查看每个文件的传输进程

使用 –
-progress参数
$rsync -avz – -progress thegeekstuff@192.168.200.10:/var/lib/rpm//root/temp/
Password:
receivingfile list …
19files to consider
./
Basenames
5357568100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19)
Conflictname
12288100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent406 bytes received 15810211 bytes 2108082.27 bytes/sec
totalsize is 45305958 speedup is 2.87

10删除在目的文件夹中创建的文件

用 –
-delete参数
#Source and target are in sync. Now creating new file at the target.
$> new-file.txt

$rsync -avz – -delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .
Password:
receivingfile list … done
deletingnew-file.txt
./

sent26 bytes received 390 bytes 48.94 bytes/sec
totalsize is 45305958 speedup is 108908.55

11不要在目的文件夹中创建新文件

有时能只想同步目的地中存在的文件,而排除源文件中新建的文件,可以使用–
-exiting 参数
$rsync -avz –existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2′spassword:
receivingfile list … done
./

sent26 bytes received 419 bytes 46.84 bytes/sec
totalsize is 88551424 speedup is 198991.96

12查看源和目的文件之间的改变情况

用 -i参数
$rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
>f.st….Basenames
.f….og.Dirnames

sent48 bytes received 2182544 bytes 291012.27 bytes/sec
totalsize is 45305958 speedup is 20.76

输出结果中在每个文件最前面会多显示9
个字母,分别表示为

>已经传输

f表示这是一个文件

d表示这是一个目录

s表示尺寸被更改

t时间标记有变化

o用户被更改

g用户组被更改

13在传输时启用包含和排除模式
$rsync -avz – -include ‘P*’ – -exclude ‘*’thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
./
Packages
Providename
Provideversion
Pubkeys

sent129 bytes received 10286798 bytes 2285983.78 bytes/sec
totalsize is 32768000 speedup is 3.19

14不要传输大文件

使用 –
-max-size 参数
$rsync -avz – -max-size=’100K’thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receivingfile list … done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername

sent252 bytes received 123081 bytes 18974.31 bytes/sec
totalsize is 45305958 speedup is 367.35

15传输所有文件

不管有没有改变,再次把所有文件都传输一遍,用-W
参数
#rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receivingfile list … done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name

sent406 bytes received 15810211 bytes 2874657.64 bytes/sec
totalsize is 45305958 speedup is 2.87
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: