Linux命令——rsync
参考:Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux
How to Sync Files/Directories Using Rsync with Non-standard SSH Port
How to Use Rsync to Sync New or Changed/Modified Files in Linux
How to Sync Two Apache Web Servers/Websites Using Rsync
简介
rsync是远程(或本地)复制和同步文件最常用的命令。 借助rsync命令,你可以跨目录,跨磁盘和跨网络远程与本地数据进行复制和同步。举例来说:在两台Linux主机之间进行数据备份和镜像。本文介绍在Linux主机上进行远程和本地传输文件的常见用法,不需要root账户也可以允许rsync。
rsync特性
- 高效地复制同步数据到对端,或者对端到本地
- 支持复制链接、设备、属主、属组、权限
- 比scp(Secure Copy)更快。rsync使用远程更新协议( remote-update protocol ),这允许仅仅传输两组文件之间的差异。对于首次传输,它将文件或目录的全部内容从源复制到目标,但是从下次起,它仅将变化部分复制到目标。
- 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 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.-.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.-.el5.centos.i386.rpm
rpmpkgs/nagios-3.5..tar.gz
rpmpkgs/nagios-plugins-1.4..tar.gz
sent 4.99M bytes received bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
远程拷贝同步文件、目录
本地到远程
[root@tecmint]$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/ root@192.168.0.101's password: sending incremental file list ./ httpd-2.2.-.el5.centos.i386.rpm mod_ssl-2.2.-.el5.centos.i386.rpm nagios-3.5..tar.gz nagios-plugins-1.4..tar.gz sent bytes received bytes 399476.80 bytes/sec total size is speedup is 1.00
远程到本地
[root@tecmint]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms root@192.168.0.100's password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.2.-.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.-.el5.centos.i386.rpm rpmpkgs/nagios-3.5..tar.gz rpmpkgs/nagios-plugins-1.4..tar.gz sent bytes received 4.99M bytes .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 root@192.168.0.100:/backups/ root@192.168.0.100's password: sending incremental file list backup.tar sent 14.71M bytes received bytes 1.28M bytes/sec total size is 16.18M speedup is 1.10
从远程到本地
[root@tecmint]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/ root@192.168.0.100's password: receiving incremental file list install.log sent bytes received .12K bytes .48K bytes/sec total size is .74K speedup is 3.77
传输数据时显示传输过程
使用--progress参数
[root@tecmint]# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs root@192.168.0.100's password: sending incremental file list created directory /root/rpmpkgs rpmpkgs/ rpmpkgs/httpd-2.2.-.el5.centos.i386.rpm 1.02M % .72MB/s :: (xfer#, to-check=/) rpmpkgs/mod_ssl-2.2.-.el5.centos.i386.rpm .04K % .19kB/s :: (xfer#, to-check=/) rpmpkgs/nagios-3.5..tar.gz 1.79M % .56MB/s :: (xfer#, to-check=/) rpmpkgs/nagios-plugins-1.4..tar.gz 2.09M % .47MB/s :: (xfer#, to-check=/) sent 4.99M bytes received bytes .56K bytes/sec total size is 4.99M speedup is 1.00
使用--exclude和--include
传输R开头的文件、目录,派出其他情况的文件、目录
[root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/var/lib/rpm/ /root/rpm root@192.168.0.101's password: receiving incremental file list created directory /root/rpm ./ Requirename Requireversion sent bytes received bytes 7438.04 bytes/sec total size is speedup is 2.59
使用--delete
--delete用于同步时,删除那些DST中有,而SRC没有的文件
再target主机创建test.txt
[root@tecmint]# touch test.txt
[root@tecmint]# rsync -avz --delete root@192.168.0.100:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting test.txt
./
sent bytes received bytes 48.94 bytes/sec
total size is speedup is 108908.55
使用--max-size
--max-size用于限制传输时文件的大小,只有≤max-size的文件才会被传输
[root@tecmint]# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.100:/root/tmprpm root@192.168.0.100's password: sending incremental file list created directory /root/tmprpm ./ Conflictname Group Installtid Name Provideversion Pubkeys Requireversion Sha1header Sigmd5 Triggername __db. sent .79K bytes received bytes .10K bytes/sec total size is 38.08M speedup is 200.43
传输完毕后自动删除源文件、目录
假设你有一个主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 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
使用--dry-run
如果你对rsync不熟悉,贸然使用rsync可能会搞乱对端文件、目录。借助--dry-run可以让你知道会传输些什么东西,但实际上并没有传输任何东西。如果输出结果与你的预期吻合,可以去掉--dry-run,进行实际的传输工作。
root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/ backup.tar sent bytes received bytes 100.00 bytes/sec total size is 16.18M speedup is 323584.00 (DRY RUN)
设置同步时带宽
使用--bwlimit可以设置同步时网络带宽上限
[root@tecmint]# rsync --bwlimit= -avzhe ssh /var/lib/rpm/ root@192.168.0.100:/root/tmprpm/
root@192.168.0.100's password:
sending incremental file list
sent bytes received bytes 61.09 bytes/sec
total size is 38.08M speedup is 113347.05
同步整个文件
rsync由于采用远程更新协议( remote-update protocol ),默认是同步变化的字节或块。使用-W可以取消这种机制,整个文件同步
[root@tecmint]# rsync -zvhW backup.tar /tmp/backups/backup.tar
backup.tar
sent 14.71M bytes received bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Linux命令——rsync的更多相关文章
- Linux命令rsync使用总结
详细用法见:https://www.cnblogs.com/oboth-zl/articles/10334754.html rsync命令简介 主要用于数据同步.备份和镜像,除了本地使用之外,也可以通 ...
- linux命令:rsync, 同步文件和文件夹的命令
Usage: rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or ...
- linux命令:拷贝命令家族(cp、scp、rsync)
Linux命令中:rsync和cp之间的区别 - 小 楼 一 夜 听 春 雨 - 博客园https://www.cnblogs.com/kex1n/p/7008178.html cp,scp,rsyn ...
- Linux命令中:rsync和cp之间的区别
rsync:只拷贝那些更新的文件: cp -u:也可以实现类似效果: 两者都基本可以满足备份的需求: 只是一般情况下,用rsync做这类备份之类的事情,更多见: 在备份的操作中,拷贝,过期文件的删除是 ...
- Linux下Rsync+sersync实现数据实时同步
inotify 的同步备份机制有着缺点,于是看了sersync同步,弥补了rsync的缺点.以下转自:http://www.osyunwei.com/archives/7447.html 前言: 一. ...
- Linux命令整理中...
Linux命令整理中... 最常用命令(我最近最常用的一般放在前面tipsbychsry) clear 清屏 date 显示日期 cal 显示日历 cal 2014 显示2014年的日历 shutdo ...
- Linux命令行技巧
Linux命令行技巧 命令 描述 • apropos whatis 显示和word相关的命令. 参见线程安全 • man -t man | ps2pdf - > man.pdf 生成一个PDF格 ...
- 【Linux】rsync同步文件 & 程序自启动
rsync使用 1. 为什么使用rsync? rsync解决linux系统下文件同步时, 增量同步问题. 使用场景: 线上需要定时备份数据文件(视频资源), 使用rsync完成每天的增量备份. 参见: ...
- 【改造Linux命令之rm - 删除文件或目录-】
用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...
随机推荐
- 【非lodop的】JS和html相关博文索引
JS: eval()方法:JS-JAVASCRIPT的eval()方法. Date()对象.getFullYear() 方法.getDate()方法.console.dir()方法:LODOP打印用J ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Redis哨兵(Sentinel)模式
Redis哨兵(Sentinel)模式 主从切换技术的方法是:当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用.这不是一种推荐的方式 ...
- 【视频开发】EasyIPCamera通过RTSP协议接入海康、大华等摄像机,摒弃私有SDK接入弊端
近期工作中需要开发一套视频监控系统,实现WEB端.手机APP端预览局域网内的道路监控摄像机,我负责一些后台服务的开发工作. 由于之前项目中的程序都是采用私有协议.各摄像机厂商的SDK进行视频监控系统开 ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- tcpdump网络调试
简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...
- 一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)
上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效.但是在生产上,一个服务部署了多台机器,重新启动比 ...
- RPC和RestFul
什么是REST REST是一种架构风格,指的是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是 RESTful.REST规范把所有内容都视为资源,网络上一切皆资源. REST并没有创 ...
- 【C#】上机实验五
.设计一个控制台应用程序,定义一个MyPoint类,该类能表示二维平面空间的点,完成点类及运算符重载等相关功能.具体要求如下: ()MyPoint类中定义2个私有字段x和y及相应的构造函数. ()My ...