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).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...
随机推荐
- nginx自定义错误页面
这里配置注意是在 server 443端口 ,蓝色部分为主要部分.这个server不是全部代码. server{ #监听443端口 listen ; #对应的域名,把baofeidyz.com改成你们 ...
- 用浏览器控制台抓取shodan、搜索引擎、zone-h的结果
0x00 前言 大部分内容来自参考连接的内容,只是一种爬取内容的思路. 在很久以前自己会有爬取zone-h做目标测试的需求,但是总是有各种反爬限制.而且个别网址还有前端自动生成内容的功能,使用Java ...
- Java获取执行进程的dump文件及获取Java stack
转发自https://blog.csdn.net/MCC_MCC_MCC/article/details/80623156 1.Windows/Linux环境下查看Java进程ID方法 使用Java自 ...
- MySQL之表约束
MySQL表约束 约束是一种限制,它通过对表的行或者列的数据做出限制,来确保表数据的完整性和唯一性. 在mysql当中一般有一下这几种约束: 非空约束. 唯一约束. 主键约束. 自增长. 默认约束. ...
- 「中山纪中集训省选组D4T1」折射伤害 高斯消元
题目描述 在一个游戏中有n个英雄,初始时每个英雄受到数值为ai的伤害,每个英雄都有一个技能"折射",即减少自己受到的伤害,并将这部分伤害分摊给其他人.对于每个折射关系,我们用数对\ ...
- [hdu 4841]圆桌问题 | 约瑟夫问题 STL-vector
原题 问题描述: 经典的约瑟夫问题,有2n个人,其中n个好人n个坏人,使得删去n人后,剩下的全为好人.m为每次数的人数. n<=32767 题解: 首先考虑n的范围,暴力肯定行不通,所以会想到线 ...
- 【C++札记】实现C++的string类
C++有了string类使得操作字符串变得很方便.有关string类,面试过程中也经常问到的就是自己实现一个sring类.下边实现个String类供大家参考: String.h #pragma onc ...
- Django框架深入了解_03(DRF之认证组件、权限组件、频率组件、token)
一.认证组件 使用方法: ①写一个认证类,新建文件:my_examine.py # 导入需要继承的基类BaseAuthentication from rest_framework.authentica ...
- Python-12-装饰器
一.定义 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 原则: 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数添加新功能后,不修 ...
- 08-Maps
Maps map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值.Maps类似于python中的字典 Maps定义 maps的key值必须是可hash(就是不可 ...