Linux命令rsync

简介

rsync是远程(或本地)复制和同步文件最常用的命令。 借助rsync命令,你可以跨目录,跨磁盘和跨网络远程与本地数据进行复制和同步。举例来说:在两台Linux主机之间进行数据备份和镜像。本文介绍在Linux主机上进行远程和本地传输文件的常见用法,不需要root账户也可以允许rsync。

rsync特性

  1. 高效地复制同步数据到对端,或者对端到本地
  2. 支持复制链接、设备、属主、属组、权限
  3. 比scp(Secure Copy)更快。rsync使用远程更新协议( remote-update protocol ),这允许仅仅传输两组文件之间的差异。对于首次传输,它将文件或目录的全部内容从源复制到目标,但是从下次起,它仅将变化部分复制到目标。
  4. Rsync消耗较少的带宽,因为它使用压缩和解压缩方法,同时发送和接收数据两端。HTTP压缩技术

基本语法

rsync options source destination

-v : 详细模式输出

-r : 递归拷贝数据,但是传输数据时不保留时间戳和权限

-a : 归档模式, 归档模式总是递归拷贝,而且保留符号链接、权限、属主、属组时间戳

-z : 压缩传输

-h : human-readable

--progress: 显示传输过程

--exclude=PATTERN 指定排除传输的文件模式

--include=PATTERN 指定需要传输的文件模式

--delete 同步时,删除那些DST中有,而SRC没有的文件

--max-size:限定传输文件大小的上限

--dry-run:显示那些文件将被传输,并不会实际传输

--bwlimit:限制传输带宽

-W:拷贝文件,不进行增量检测

使用场景

本地拷贝同步文件、目录

同步一个文件从本地一个目录到另一个目录,如果目标目录不纯在,会自动创建

[root@tecmint]# rsync -zvh backup.tar /tmp/backups/
created directory /tmp/backups
backup.tar
sent 14.71M bytes  received 31 bytes  3.27M bytes/sec
total size is 16.18M  speedup is 1.10

再演示同步目录

[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes  received 92 bytes  3.33M bytes/sec
total size is 4.99M  speedup is 1.00

远程拷贝同步文件、目录

[root@tecmint]$ rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes  received 91 bytes  399476.80 bytes/sec

total size is 4991313  speedup is 1.00

远程到本地

[root@tecmint]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes  received 4.99M bytes  322.16K bytes/sec

total size is 4.99M  speedup is 1.00

通过ssh使用rsync

SSH(Secure Shell)以加密方式传输数据时,接获数据并破解很难。rsync同ssh一起使用可以增强传输按权限,注意可能需要用户密码。

从本地到远程

[root@tecmint]# rsync -avzhe ssh backup.tar [email protected]:/backups/

[email protected]'s password:

sending incremental file list

backup.tar

sent 14.71M bytes  received 31 bytes  1.28M bytes/sec

total size is 16.18M  speedup is 1.10

远程到本地

[root@tecmint]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes  received 8.12K bytes  1.48K bytes/sec

total size is 30.74K  speedup is 3.77

传输完毕后自动删除源文件、目录

假设你有一个主Web Server和一个数据备份Sever,你创建了每日备份任务并与备份服务器同步备份,你不希望在Web Server中保留该本地备份副本。

那么,你是否会等待传输完成然后手动删除这些本地备份文件? 当然不。 可以使用--remove-source-files选项完成此自动删除。

[root@tecmint]# rsync --remove-source-files -zvh backup.tar /tmp/backups/

backup.tar

sent 14.71M bytes  received 31 bytes  4.20M bytes/sec

total size is 16.18M  speedup is 1.10

[root@tecmint]# ll backup.tar

ls: backup.tar: No such file or directory
发布了87 篇原创文章 · 获赞 186 · 访问量 64万+

猜你喜欢

转载自blog.csdn.net/qq_33326449/article/details/103934608
今日推荐