参考 :http://www.thegeekstuff.com/2010/09/rsync-command-examples/
注:下面所有例子中 – - 之间实际上是没有空格的,使用时请删除空格。
Rsync是一个远程数据同步工具,可通过LAN 或互联网快速同步多台主机间的文件。Rsync 本来是用以取代 rcp的一个工具,它当前由 rsync.samba.org 维护。Rsync 使用所谓的”Rsync演算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
Rsync 的特色:
-
快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。
-
安全:rsync 允许通过 ssh 协议来加密传输数据。
-
更少的带宽:rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
-
特权:安装和执行 rsync 无需特别的权限
基本语法:
rsync options source destination
源和目标都可以是本地或远程,在进行远程传输的时候,需要指定登录名、远程服务器及文件位置
样例:
1 在本地机器上对两个目录进行同步
$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list … done
sva.xml
svB.xml
.
sent 26385 bytes received 1098 bytes 54966.00 bytes/sec
total size is 44867 speedup is 1.63
$
参数:
-
-z 开启压缩
-
-v 详情输出
-
-r 表示递归
2 利用 rsync -a 让同步时保留时间标记
rsync 选项 -a 称为归档模式,执行以下操作
-
递归模式
-
保留符号链接
-
保留权限
-
保留时间标记
-
保留用户名及组名
$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list … done
./
sva.xml
svB.xml
.
sent 26499 bytes received 1104 bytes 55206.00 bytes/sec
total size is 44867 speedup is 1.63
$
3 仅同步一个文件
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys
sent 42 bytes received 12380 bytes 3549.14 bytes/sec
total size is 12288 speedup is 0.99
4 从本地同步文件到远程服务器
$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/
Password:
building file list … done
./
rpm/
rpm/Basenames
rpm/Conflictname
sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec
total size is 45305958 speedup is 2.87
就像你所看到的,需要在远程目录前加上 ssh 登录方式,格式为username@machinename:path
5 同步远程文件到本地
和上面差不多,做个相反的操作
$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list … done
rpm/
rpm/Basenames
.
sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec
total size 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:
receiving file list … done
rpm/
rpm/Basenames
sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec
total size is 45305958 speedup is 2.87
7 不要覆盖被修改过的目的文件
使用 rsync -u 选项可以排除被修改过的目的文件
$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list … done
rpm/
sent 122 bytes received 505 bytes 114.00 bytes/sec
total size is 45305958 speedup is 72258.31
$ ls -lrt
total 39088
-rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
8 仅仅同步目录权(不同步文件)
使用 -d 参数
$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ .
Password:
receiving file list … done
logrotate.status
CAM/
YaST2/
acpi/
sent 240 bytes received 1830 bytes 318.46 bytes/sec
total size is 956 speedup is 0.46
9 查看每个文件的传输进程
使用 – -progress 参数
$ rsync -avz – -progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list …
19 files to consider
./
Basenames
5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19)
Conflictname
12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec
total size 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:
receiving file list … done
deleting new-file.txt
./
sent 26 bytes received 390 bytes 48.94 bytes/sec
total size is 45305958 speedup is 108908.55
11 不要在目的文件夹中创建新文件
有时能只想同步目的地中存在的文件,而排除源文件中新建的文件,可以使用 – -exiting 参数
$ rsync -avz –existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2′s password:
receiving file list … done
./
sent 26 bytes received 419 bytes 46.84 bytes/sec
total size is 88551424 speedup is 198991.96
12 查看源和目的文件之间的改变情况
用 -i 参数
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list … done
>f.st…. Basenames
.f….og. Dirnames
sent 48 bytes received 2182544 bytes 291012.27 bytes/sec
total size 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:
receiving file list … done
./
Packages
Providename
Provideversion
Pubkeys
sent 129 bytes received 10286798 bytes 2285983.78 bytes/sec
total size 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:
receiving file list … done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername
sent 252 bytes received 123081 bytes 18974.31 bytes/sec
total size is 45305958 speedup is 367.35
15 传输所有文件
不管有没有改变,再次把所有文件都传输一遍,用 -W 参数
# rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receiving file list … done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name
sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec
total size is 45305958 speedup is 2.87
分享到:
相关推荐
用rsync从Linux到Windows远程备份.
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步
linux系统备份软件rsync-3.0.4.tar.gz
利用Rsync 3.0.9实现Linux系统间的远程同步和增量备份方案及搭建流程。 本人原创与2011年07月20日
rsync远程同步
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步
linux 下rsync文件夹同步配置,可以定时。
摘要: rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备份,我们还可以把它作为不同主机网络备份工具之用。本文主要讲述的是如何自架rsync服务器,以实现文件传输、备份和镜像。相对tar和wget...
说明 利用cwrsync将 bat脚本,一键传输windows文件到linux,当然反过来也是可以的。 将压缩包解压到windows C盘根目录下即可。... 4.2、其他几个变量请根据你实际操作的目录和远程主机信息来设置。
linux中rsync异步服务器实现文件同步与备份.docx
DuplexRsync - 使用rsync和fswatch进行DropBox / GoogleDrive风格的双向同步
使用Rsync实现linux与linux,linux与windows,windows与windows目录备份,亲测有效!
rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备分,我们还可以把它作为不同主机网络备份工具之用。本文主要讲述的是如何自架rsync服务器,以实现文件传输、备份和镜像。相对tar和wget来说,...
linux发行版中大多都自带rsync,不过版本比较低,一般...备份的时候,rsync大约占用了将近2G的内存,大量内存的占用,造成服务器物理内存不足,进而使用到swap,然后产生更高的iowait(交换内存),进而造成rsync列表更慢
此文档本人总结了linux下如何配置两台linux系统之间快速传递庞大数据的方法,总结其他很简单,一个看做服务端,一个看做客户端。通过配置rsync即可。前提是要安装rsync的rpm包,运维的小伙伴很实用!
Linux下rsync安装配置使用教程
亲测,有疑问可以加我,共同讨论学习!
用Rsync实现Linux文件系统备份.pdf
2.1.1在使用rsync传输文件时,需要指定一个源和一个目的,其中一个可能是远程机器的资源信息。例如: #rsync *.c servivcer:src/ 表示将传输当前目录下所有以.c结尾的文件到机器servivcer的src目录下。如果文件已经...