本文主要介绍Linux操作系统中rsync工具的常见用法。
1 概述
1.1 What
此处引用Manual中对于rsync的介绍,内容如下:
rsync - a fast, versatile, remote (and local) file-copying tool.
Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
Rsync finds files that need to be transferred using a "quick check" algorithm (by default) that looks for files that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.
2 安装
可以通过yum命令直接安装rsync工具。命令如下:
yum install rsync.x86_64
3 用法
3.1 语法介绍
rsync传输文件的方式有三种:本地传输、通过远端shell传输、通过rsync守护进程传输。
三种传输方式的语法信息如下:
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Usages with just one SRC arg and no DEST arg will list the source files instead of copying.
3.2 命令选项介绍
“-e”选项(设置用于文件传输的远端shell程序):
-e, --rsh=COMMAND
This option allows you to choose an alternative remote shell program to use for
communication between the local and remote copies of rsync. Typically, rsync is configured
to use ssh by default, but you may prefer to use rsh on a local network.
“-a”选项(归档模式):
-a, --archive
This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and
want to preserve almost everything (with -H being a notable omission). The only exception
to the above equivalence is when --files-from is specified, in which case -r is not
implied.
Note that -a does not preserve hardlinks, because finding multiply-linked files is
expensive. You must separately specify -H.
“-v”选项(详情显示):
-v, --verbose
This option increases the amount of information the daemon logs during its startup
phase. After the client connects, the daemon’s verbosity level will be controlled by the
options that the client used and the "max verbosity" setting in the module’s config section.
“-z”选项(压缩模式):
-z, --compress
With this option, rsync compresses the file data as it is sent to the destination
machine, which reduces the amount of data being transmitted -- something that is useful
over a slow connection.
“-P”选项(传输中断后保留已传输文件):
-P The -P option is equivalent to --partial --progress. Its purpose is to make it much
easier to specify these two options for a long transfer that may be interrupted.
3.3 用法示例
3.3.1 使用远端ssh程序传输文件
使用远端(remote-shell)ssh程序,将本地“/opt/liitdar/mydemos/”目录下的simples文件夹以及其中的文件传输到远端主机(192.168.213.130)的“/opt/liitdar/mydemos/”目录下,命令如下:
[root@node1 /opt/liitdar/mydemos]# rsync simples -avzP -e 'ssh -p 22' root@192.168.213.130:/opt/liitdar/mydemos/
说明:使用ssh程序传输文件时,需要输入对端的用户密码。
3.3.2 测试修改文件后的同步功能
承接“3.3.1”节操作,现在修改远端主机(192.168.213.130)的“/opt/liitdar/mydemos/simples”目录下的semicolon_test1.cpp文件的内容,然后再使用上一步的rsync命令进行文件传输(同步),此时可以看到如下信息:
[root@node1 /opt/liitdar/mydemos]# rsync simples -avzP -e 'ssh -p 22' root@192.168.213.130:/opt/liitdar/mydemos/
root@192.168.213.130's password:
sending incremental file list
simples/
simples/semicolon_test1.cpp
235 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=7/12)
sent 441 bytes received 46 bytes 51.26 bytes/sec
total size is 110,344 speedup is 226.58
[root@node1 /opt/liitdar/mydemos]#
通过上述传输信息可知,rsync只将(本地与远端之间)存在差异的文件(semicolon_test1.cpp)传输(同步)到了远端,这是rsync工具的一个主要特点。