参考: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特性

  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 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的更多相关文章

  1. Linux命令rsync使用总结

    详细用法见:https://www.cnblogs.com/oboth-zl/articles/10334754.html rsync命令简介 主要用于数据同步.备份和镜像,除了本地使用之外,也可以通 ...

  2. linux命令:rsync, 同步文件和文件夹的命令

    Usage: rsync [OPTION]... SRC [SRC]... DEST  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST  or ...

  3. linux命令:拷贝命令家族(cp、scp、rsync)

    Linux命令中:rsync和cp之间的区别 - 小 楼 一 夜 听 春 雨 - 博客园https://www.cnblogs.com/kex1n/p/7008178.html cp,scp,rsyn ...

  4. Linux命令中:rsync和cp之间的区别

    rsync:只拷贝那些更新的文件: cp -u:也可以实现类似效果: 两者都基本可以满足备份的需求: 只是一般情况下,用rsync做这类备份之类的事情,更多见: 在备份的操作中,拷贝,过期文件的删除是 ...

  5. Linux下Rsync+sersync实现数据实时同步

    inotify 的同步备份机制有着缺点,于是看了sersync同步,弥补了rsync的缺点.以下转自:http://www.osyunwei.com/archives/7447.html 前言: 一. ...

  6. Linux命令整理中...

    Linux命令整理中... 最常用命令(我最近最常用的一般放在前面tipsbychsry) clear 清屏 date 显示日期 cal 显示日历 cal 2014 显示2014年的日历 shutdo ...

  7. Linux命令行技巧

    Linux命令行技巧 命令 描述 • apropos whatis 显示和word相关的命令. 参见线程安全 • man -t man | ps2pdf - > man.pdf 生成一个PDF格 ...

  8. 【Linux】rsync同步文件 & 程序自启动

    rsync使用 1. 为什么使用rsync? rsync解决linux系统下文件同步时, 增量同步问题. 使用场景: 线上需要定时备份数据文件(视频资源), 使用rsync完成每天的增量备份. 参见: ...

  9. 【改造Linux命令之rm - 删除文件或目录-】

    用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...

随机推荐

  1. Qt编写控件属性设计器10-导出xml

    一.前言 能够导出控件布局和属性设置数据到xml文件或者其他文件,也是一个非常实用的功能,类似于QtDesigner中把页面设计好以后生成的.ui结尾的文件,其实就是xml文件,按照约定的规则存储好控 ...

  2. Tengine的说明

    什么是Tengine 官方帮助文档:http://tengine.taobao.org/changelog_cn.html

  3. k8s记录-不同集群服务互联

    1.外部访问内部服务 添加nodePort 修改type:ClusteIP为type:NodePort 2.内部服务访问外部服务 kubectl create -f test.yaml apiVers ...

  4. openstack虚拟机获取不到ip

    一.现象描述: openstack平台中创建虚拟机后,虚拟机在web页面中显示获取到了ip,但是打开虚拟机控制台后查看网络状态,虚拟机没有ip地址,下图为故障截图: 二.分析思路: (1)查看neut ...

  5. django 表操作

    添加表纪录 # Create your models here. class Book(models.Model): id = models.AutoField(primary_key=True) t ...

  6. [LeetCode] 276. Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  7. Kubernetes 服务质量 Qos 解析 - Pod 资源 requests 和 limits 如何配置?

    QoS是 Quality of Service 的缩写,即服务质量.为了实现资源被有效调度和分配的同时提高资源利用率,kubernetes针对不同服务质量的预期,通过 QoS(Quality of S ...

  8. 下载youtube视频到本地

    https://www.clipconverter.cc/ 先通过上面的网站对youtube视频的url 进行解析获得下载链接地址 获得链接地址后 可通过阿里云香港服务器去下载 , 速度比较快 在阿里 ...

  9. Linux下用dd命令测试硬盘的读写速度

    一.测试写速度:   time dd if=/dev/zero of=/tmp/test bs=8k count=1000000 测试结果:565 MB/s 二.测试读速度:   time dd if ...

  10. linux查看磁盘是否SSD盘

    命令: cat /sys/block/sda/queue/rotational 注意: 命令中的sba是你的磁盘名称,可以通过df命令查看磁盘,然后修改成你要的 结果: 返回0:SSD盘 返回1:SA ...