rsync在同步文件夹内容这个工作上应用非常广泛,但是rsync本身命令还是比较复杂,本文总结一下:

rsync = remote sync的简称 ,它 被用于在linux/unix系统中执行备份操作。rsnync用于从一个位置到另外一个位置同步文件和文件夹。备份的地址可以是本地也可以是remote server。

rsync的重要功能:

  • speed

首次使用时,rsync在source和destination folder之间复制全部内容。下次使用时,rsync只传输变更的块或字节到目的地,而这个机制将大大提升传输速度

  • security

rsync允许对数据使用ssh协议加密

  • less bandwidth

rsync使用对数据块压缩和解压缩的办法降低带宽需求。

  • privileges

无需特殊的特权来运行rsync

语法

$ rsync options source destination

source和destination可以是本地或者远程目录。对于远程的情况,需要指定login name, remote server name and location

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

在本地机器上同步两个目录,使用rsync -zvr命令

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

上述命令中:

-z  打开压缩功能

-v verbose更多打印信息

-r recursive

执行上述命令后,你会发现rsync copy会影响到文件的timestamp信息,这时因为默认rsync并不保护timestamp信息

例2:在sync时,保留时间戳 -a(achive mode:recursive mode, 保留符号链接,保留权限信息,时间戳,以及owner,group信息)

$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent bytes received bytes 55206.00 bytes/sec
total size is speedup is 1.63
$注意这时你会发现source,dest文件的时间戳等信息是不变的
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- root bin Jun /var/opt/installation/inventory/sva.xml
-r--r--r-- root bin Jun /root/temp/sva.xml

例3:只同步一个文件

只要在rsync命令中指定文件名称即可:

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

例4:从本地到远端

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

当执行和remote server同步的动作时,你需要指定username,ip。也要指定远程服务器上的目的地目录,格式是: username@machineIP:Path

这个过程中,rsync会要求输入密码。但是如果你有一个脚本自动运行这个备份动作,你可能希望不要手动输入密码,这时可以参考: http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/

例5:从远程到本地

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

例6:remote shell for synchronization

rsync允许你指定你想使用的remote shell,你可以使用rsync -e ssh来enable the secured remote connection

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

例7:不覆盖目的地址上已经修改过的文件

典型情况下,如果一个文件在destination被修改的话,我们可能并不希望使用来自source的老文件去覆盖修改

使用rsync -u选项达到这个目的(即:如果目的地上修改过,那么不要覆盖它)在下面的例子中,Basenames文件在destination上做了修改,因此如果使用-u选项,则不会被修改

$ ls -l /root/temp/Basenames
total
-rwxr-xr-x root root Sep : Basenames $ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/ sent bytes received bytes 114.00 bytes/sec
total size is speedup is 72258.31 $ ls -lrt
total
-rwxr-xr-x root root Sep : Basenames

例8:只同步目录tree structure(而不同步文件)

使用-d想想将只从source到destination同步文件夹的tree structure,下面的例子,只会递归同步目录树,而目录中的文件不会同步

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

例9:查看rsnync传输进度

当使用rsync来做备份时,你可能希望知道backup的进度,比如有多少个文件已经copy了,以及copy的速度等信息, rsync -progress将会打印rsync执行中的详细信息:

$ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ...
files to consider
./
Basenames
% .98MB/s :: (xfer#, to-check=/)
Conflictname
% .09kB/s :: (xfer#, to-check=/)
.
.
.
sent bytes received bytes 2108082.27 bytes/sec
total size is speedup is 2.87

例10:删除在Targe上创建的文件

如果在source这一侧并不存在一个文件,而这个文件本身又在destination上存在,那么你可以指定删除这个文件,-delete选项完成这个功能

# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt $ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./ sent bytes received bytes 48.94 bytes/sec
total size is speedup is 108908.55
注意:new-file.txt文件将在rsync过程中被删除

例11:在destination(target)上不创建新文件

如果你喜欢,你可以只update(sync)那些在target上已经存在的文件。如果source有新的文件,而这个文件本身在target上并不存在,那么你可以通过-existing选项避免在destination上创建这些新文件

首先在source上创建一个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:查看source/destination之间的变更

At source:
$ ls -l /var/lib/rpm
-rw-r--r-- root root -- : Basenames
-rw-r--r-- root root -- : Conflictname
-rw-r--r-- root root -- : Dirnames
At destination:
$ ls -l /root/temp
-rw-r--r-- root root Sep : Basenames
-rw-r--r-- root root May Conflictname
-rw-r--r-- bin bin Jun : Dirnames
在这里source和destination有两个不同。owner/group,以及size不同。
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames sent bytes received bytes 291012.27 bytes/sec
total size is speedup is 20.76

在上面的例子中,在Basenames, Dirnames文件的前面有一些奇怪的信息,其实它非常重要:

> specifies that a file is being transferred to the local host.
f represents that it is a file.
s represents size changes are there.
t represents timestamp changes are there.
o owner changed
g group changed.

例13:在文件传输中包含和排除Pattern

rsync允许你给一个pattern,指定你希望在做同步过程中包含或者排除的文件或者目录

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

上面的例子中它将仅仅包含那些以P打头的文件或者文件夹并且排除所有其他文件

例14:不传输大的文件

你可以告诉rsync不要传输大于指定大小尺寸的文件,使用-max-size选项

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

上面的例子使得rsync只传输那些小于100K大小的文件。你也可以指定M或G

例15:传输整个文件

rsync的一个重要功能是它只传输一个文件的变更的块到目的地,而不是传输文件本省。如果网络带宽本身并不是什么问题,你可以传输整个文件,通过-Wxuanxiang 这将加速rsync的处理速度,因为他不需要再在source和destination做checksum的运算了。

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

本文原文来自于: http://www.thegeekstuff.com/2010/09/rsync-command-examples/

参考:https://rsync.samba.org/how-rsync-works.html

rsync常用命令及格式的更多相关文章

  1. rsync常用命令和使用方法

    rsync是一个远程数据同步工具,可以实现数据的增量备份,这点比scp要好,scp只能全量备份.同步可以保持文件原有属性,传输过程加密,数据传输全. rsync 的传输模式有:        1. 本 ...

  2. linux常用命令 print格式输出

    格式化输出命令 printf '输出类型 输出格式' 输出内容 输出类型: %ns 输出字符串,n是数字指代输出的几个字符 %ni 输出整数,n是数字指代输出几个数字 %m.nf 输出浮点数.m和n是 ...

  3. nmap十条常用命令行格式

    1) 获取远程主机的系统类型及开放端口 nmap -sS -P0 -sV -O <target> 这里的 < target > 可以是单一 IP, 或主机名,或域名,或子网 - ...

  4. rsync 常用命令

    rsync -auvrtzopgP --progress --delete --exclude-from=exclude.list SRC DST \\保留原文件属性并详细输出 删除那些DST中SRC ...

  5. ffmpg常用命令解析

    1 相关学习官网地址 官网地址:https://www.ffmpeg.org 安装步骤:https://www.johnvansickle.com/ffmpeg/faq/ 2 涉及的常用命令 视频格式 ...

  6. 7z常用命令行&7z检测压缩包完整性&7z压缩包错误不执行rsync同步

    7Z简介&常用命令 7Z脚本使用说明 7Z检测压缩包完整性脚本 7Z压缩包错误不执行Rsync脚本 1.7Z简介&常用命令 ⑴简介: 7z,全称7-Zip, 是一款开源软件.是目前公认 ...

  7. 运维工作中常用到的几个rsync同步命令

    作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync. 下面结合本人近几年运维工作中对这一 ...

  8. 十条常用nmap命令行格式

    十条常用nmap命令行格式 ) 获取远程主机的系统类型及开放端口 nmap -sS -P0 -sV -O <target> 这里的 < target > 可以是单一 IP, 或 ...

  9. linux介绍、命令(基本命令、常用命令、使用方法、基本格式)

    操作系统(科普章节) 目标 了解操作系统及作用 1. 操作系统(Operation System,OS) 一个例子说明操作系统 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如 ...

随机推荐

  1. BZOJ 1088

    真是智商不够, 智商题:.... 假如:第1,2个格子已知,然后根据第二列的情况,就可以把所有满足的情况推出来,又萌萌哒.. 无耻攒字数: #include<stdio.h> using ...

  2. linux shell的输出效果修改方法(界面颜色)

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例: echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置背景成为蓝色,前景白色, ...

  3. 社交APP经典死法18种,听野路子产品菜狗怎么说

    点这里 社交APP经典死法18种,听野路子产品菜狗怎么说 时间 2015-04-06 11:24:53  虎嗅网相似文章 (4)原文  http://www.huxiu.com/article/112 ...

  4. .htaccess的基本作用及相关语法介绍

    .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. .htaccess主要的作用有:URL重写.自定义错误页面.MIME类型配置以及访问权限控制等.主要体现在伪静态的应 ...

  5. ElasticSearch使用IK中文分词---安装步骤记录

    提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...

  6. CDOJ 1259 昊昊爱运动 II 线段树+bitset

    昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...

  7. python FTP上传和下载文件

    1. 连接FTP server import ftplib ftp = ftplib.FTP(ftpserver, user, passwd) 等同于 import ftplib ftp = ftpl ...

  8. 点击UITableView的cell展开收缩

    在项目中有个需求,点击表视图的单元格展开,再点击另外一个单元格或者本身又收缩,经过一段时间尝试,实现了该功能,现在记录分享总结下.   首先要理解UITableView代理方法调用的先后顺序.   当 ...

  9. iOS开发swift--函数

    函数返回值 ---函数 // 自加函数 func Add(num : Int)->Int{ } //自减函数 func zj(num : Int)->Int{ } //定义返回函数类型 f ...

  10. FreePascal经典资料

    ------------------------------------------------------------------------ 这是每个版本的changelog: http://bu ...