导读 Rsync(remote sync) 是用于同步某一位置文件和目录到另一位置的有效方法。备份的位置可以在本地服务器或远程服务器。本站之前亦有介绍rsync的安装配置和教程,详看《rsync的安装和配置》一文。
rsync特征

速度:第一次的rsync同步源的整个内容目录到目标位置。之后,rsync只传递改变的块或字节到目标位置,使传输非常快。
安全:rsync在传输中允许使用ssh协议加密数据。
更少的带宽:rsync分别在发送和接收端对数据进行压缩和解压缩。所以在使用rsync传输数据时使用的带宽总是比使用其他文件传输协议来的少。
特权:不需要特殊权限来安装和执行rsync

rsync语法
$ rsync options source destination 

source和destination可以是本地或远程。在远程的情况下,需要指定登录名、远程服务器的名称和位置。

rsync使用示例

1. 在本地服务器上同步两个目录

用rsync – zvr在本地服务器上同步两个目录

$ rsync -zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 bytes received 1098 bytes 54966.00 bytes/sec total size is 44867 speedup is 1.63 

在上面rsync例子中:
• –z:允许压缩
• –v:冗长
• –r:递归
注:Rsync不会同步源文件或目录的时间戳

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 bin bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Sep 2 2009 /root/temp/sva.xml 

2. 用rsync –a同步时间戳

rsync的-a选项是archive的意思,加了之后有以下作用:
• 递归模式
• 同步软链接
• 同步权限
• 同步时间戳
• 同步属主和属组
现在,我们执行与例1一样的命令,不同的是使用-a选项。

$ rsync -azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 bytes received 1104 bytes 55206.00 bytes/sec total size is 44867 speedup is 1.63 

我们可以看到,rsync将源目录下文件的时间戳也同步过来了。

$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml 

3. 只同步一个文件

指定文件名称,只同步一个文件

$ rsync -v /var/lib/rpm/Pubkeys /root/temp/ Pubkeys sent 42 bytes received 12380 bytes 3549.14 bytes/sec total size is 12288 speedup is 0.99 

4. 将本地文件同步到远程服务器

当同步到远程服务器时,我们需要指定用户名、远程服务器的IP地址和目标路径。格式为:username@remote_server_ip:path

$ rsync -avz /root/temp/ linuxprobe@192.168.200.10:/home/linuxprobe/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictname sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec total size is 45305958 speedup is 2.87 

5. 将远程文件同步到本地服务器

$ rsync -avz linuxprobe@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames . sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87 

6. 指定使用某一shell同步

远程同步时,Rsync可以指定使用某一shell进行安全传输。如:使用rsync –e ssh选择ssh进行安全连接。

$ rsync -avz -e ssh linuxprobe@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87 

7. 不覆盖目标位置已改变的文件

如果目标位置的文件已被修改,而我们不希望旧文件覆盖它时,可以使用-u选项。下面例子中,目标位置的文件Basenames已经被修改,使用-u 选项是它不被旧文件覆盖。

$ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ rsync -avzu linuxprobe@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ sent 122 bytes received 505 bytes 114.00 bytes/sec total size is 45305958 speedup is 72258.31 $ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames 

8. 只同步目录树结构(不包括文件)

使用rsync - d选项只将目录树从源位置同步至目标位置。下面的示例中,以递归方式只同步目录树而不同步目录中的文件。

$ rsync -v -d linuxprobe@192.168.200.10:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/ sent 240 bytes received 1830 bytes 318.46 bytes/sec total size is 956 speedup is 0.46 

9. 查看rsync进度

使用rsync –progress选项可以查看rsync进度,如多少文件已经被拷贝,完成率是多少等

$ rsync -avz --progress linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec total size is 45305958 speedup is 2.87 

10. 删除目标位置创建的文件

若我们希望rsync时删除在源位置不存在而在目标位置存在的文件,可以使用-delete选项。
此例中,我们在目标位置创建了一个新文件名为new-file.txt.,在同步过程中,-delete选项删除了new-file.txt文件

# Source and target are in sync. Now creating new file at the target. $ > new-file.txt $ rsync -avz --delete linuxprobe@192.168.200.10:/var/lib/rpm/ . Password: receiving file list ... done deleting new-file.txt ./ sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55 

11. 在目标位置不创建新文件

使用-existing选项使得在同步时只同步目标位置存在的文件,而不创建新文件。

#在源位置创建新文件new-file.txt [/var/lib/rpm ]$ > new-file.txt #同步目录 $ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2's password: receiving file list ... done ./ sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96 

12.查看源和目标的不同文件或目录

源位置:

$ ls -l /var/lib/rpm -rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames -rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname -rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames 

目标位置:

$ ls -l /root/temp -rw-r--r-- 1 root root 12288 May 28 2008 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames 

源和目标之间有两个不同之处:1. 文件Dirnames属主和属组不同 2. 文件Basenames的大小不同
使用-i选项来显示不同之处,如下所示

$ rsync -avzi linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done >f.st.... Basenames .f....og. Dirnames sent 48 bytes received 2182544 bytes 291012.27 bytes/sec total size is 45305958 speedup is 20.76 In the output it displays some 9 letters in front of the file name or directory name indicating the changes. 

可以看到,在文件名或目录名前有9个字符来代表不同之处。此例中,Basenames和Dirnames前的字符代表含义如下:

> 代表文件已被传输到本地主机。 f:代表这是个文件 s:代表文件大小发生变化 t:代表时间戳发生变化 o:属主发生变化 g:属组发生变化 

13. 在传输时指定包括或排除某些文件
只传输’P’开头的文件或目录(使用rsync include),不传输其他任何文件(使用rsync exclude)

$ rsync -avz --include 'P*' --exclude '*' linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Packages Providename Provideversion Pubkeys sent 129 bytes received 10286798 bytes 2285983.78 bytes/sec total size is 32768000 speedup is 3.19 

14. 不传输大文件

使用rsync –max-size选项后,rsync将不传输大于指定大小的文件

$ rsync -avz --max-size='100K' linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggername sent 252 bytes received 123081 bytes 18974.31 bytes/sec total size is 45305958 speedup is 367.35 

15. 传输整个文件

rsync的主要特征之一是它只传输改变的块到目标位置,而不是传输整个文件。如果网络带宽对你不是问题(CPU有),您可以使用-w选项来传输整个文件。它会加速rsync过程,因为它不用在源和目标位置执行校验和。

# rsync -avzW linuxprobe@192.168.200.10:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Name sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec total size is 45305958 speedup is 2.87

本文转载自:http://www.linuxprobe.com/rsync-use-sample.html

更多Linux干货请访问:http://www.linuxprobe.com/

rsync 使用示例的更多相关文章

  1. 第2章 rsync(二):inotify+rsync详细说明和sersync

    本文目录: inotify+rsync 1.1 安装inotify-tools 1.2 inotifywait命令以及事件分析 1.3 inotify应该装在哪里 1.4 inotify+rsync示 ...

  2. man rsync翻译(rsync命令中文手册)

    本文为命令rsync的man文档翻译,几乎所有的选项都翻译了,另外关于筛选规则部分只翻译了一部分.由于原文很多地方都比较啰嗦,所以译文中有些内容可能容易让国人疑惑,所以我个人在某些地方加上了注释.若有 ...

  3. Rsync同步、Rsync+Lsync实时同步

    Rsync同步.Rsync+Lsync实时同步 原创博文http://www.cnblogs.com/elvi/p/7658049.html #!/bin/sh #Myde by Elven @ #c ...

  4. [转帖]rsync简介

    rsync用法详细解释 https://www.cnblogs.com/noxy/p/8986164.html 之前一直使用 scp 现在发现这个命令更好一些. 提要 熟悉 rsync 的功能及其特点 ...

  5. rsync用法详细解释

    提要 熟悉 rsync 的功能及其特点 掌握 rsync 语法及常用选项的功能 掌握 rsync 命令的三种基本使用方法 掌握如何筛选 rsync 的传输目标 掌握使用 rsync 进行镜像和增量备份 ...

  6. Linux rsync 命令学习

    Rsync命令和cp命令很像,但是功能似乎更加复杂点,主要用来备份数据.看了网上一堆介绍的文章,感觉不是很通俗易懂.下面按照我的理解,做一些笔记: 同步方式 之前接触过一些同步软件,例如坚果云.百度云 ...

  7. rsync 精确同步文件用法 (转载)

    -- include-from 指定目录下的部分目录的方法: include.txt: aa bb ss Command: rsync -aSz  --include-from=/home/inclu ...

  8. rsync(六)命令中文手册

    rsync(1) rsync(1) 名称 rsync - 一个快速.多功能的远程(和本地)文件拷贝工具 摘要 Local: rsync [OPTION...] SRC... [DEST] Access ...

  9. rsync(二):inotify+rsync详细说明和sersync

    以下是rsync系列篇: inotify+rsync 如果要实现定时同步数据,可以在客户端将rsync加入定时任务,但是定时任务的同步时间粒度并不能达到实时同步的要求.在Linux kernel 2. ...

随机推荐

  1. 为什么你总是学不好Linux技术?这是我的答案。

    摘要: 我们为什么要学习Linux,最近几年Linux发展迅速,特别服务器领域,带来了很多新技术,云计算,虚拟化,大数据等技术,还有安全方面都有了很大的发展同时也给了Linux运维工作带来了,更多的要 ...

  2. redis sort

    redis sort命令用法 1.命令描述返回或保存给定列表.集合.有序集合key中经过排序的元素.排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较. 2.一般sort用法最简单的sort ...

  3. NOIP 2006 解题报告

    第一题: 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...

  4. numpy 总结

    1.array.sum() from numpy import * import operator group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) ...

  5. Rhel6-lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 相关网址:http://zh.linuxvirtualserver.org/ yum仓库配置: [rh ...

  6. 宏定义#define和typedef的区别和典型范例题目辨析

    宏定义#define pStr char*  ,是直接把程序中出现pStr的地方替换成char* ,直接替换: typedef  char * pStr; 是给char*定义一个别名叫做 pStr; ...

  7. ansible quick start

    1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...

  8. ODI 12c 安装

    软件下载地址: http://www.oracle.com/technetwork/middleware/data-integrator/downloads/index.html 下载这个版本: Or ...

  9. python 绘图工具 matplotlib 入门

    转自: http://www.cnblogs.com/kaituorensheng/p/3440273.html matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的 ...

  10. 关于struts2拦截器获取页面参数

    package InterCeptor; import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;impor ...