参考: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
简介
rsync是远程(或本地)复制和同步文件最常用的命令。 借助rsync命令,你可以跨目录,跨磁盘和跨网络远程与本地数据进行复制和同步。举例来说:在两台Linux主机之间进行数据备份和镜像。本文介绍在Linux主机上进行远程和本地传输文件的常见用法,不需要root账户也可以允许rsync。
rsync特性
- 高效地复制同步数据到对端,或者对端到本地
- 支持复制链接、设备、属主、属组、权限
- 比scp(Secure Copy)更快。rsync使用远程更新协议( remote-update protocol ),这允许仅仅传输两组文件之间的差异。对于首次传输,它将文件或目录的全部内容从源复制到目标,但是从下次起,它仅将变化部分复制到目标。
- Rsync消耗较少的带宽,因为它使用压缩和解压缩方法,同时发送和接收数据两端。HTTP压缩技术
基本语法
rsync options source destination
-v : 详细模式输出
-r : 递归拷贝数据,但是传输数据时不保留时间戳和权限copies data recursively (but don’t preserve timestamps and permission while transferring data
-a : 归档模式, 归档模式总是递归拷贝,而且保留符号链接、权限、属主、属组时间戳
-z : 压缩传输
-h : human-readable
--progress: 显示传输过程
使用场景
扫描二维码关注公众号,回复:
4426899 查看本文章
本地拷贝同步文件、目录
同步一个文件从本地一个目录到另一个目录,如果目标目录不纯在,会自动创建
[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/ root@192.168.0.101:/home/ root@192.168.0.101'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 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.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