Linux下的rsync命令介绍

本文主要介绍在 Linux 中 rsync 命令的相关内容。

1 概述

1.1 what

此处选取 rsync man手册的相关介绍:

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 用法

1.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.

1.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.

1.2 用法示例

1.2.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' [email protected]:/opt/liitdar/mydemos/

说明:使用 ssh 程序传输文件时,需要输入对端的用户密码。

1.2.2 测试修改文件后的同步功能

继承上一步的操作,现在我们修改远端主机 (192.168.213.130)的 /opt/liitdar/mydemos/simples 目录下的 semicolon_test1.cpp 文件的内容,然后再使用上一步的 rsync 命令进行文件传输(同步),此时可以看到如下信息:

[root@node1 /opt/liitdar/mydemos]# rsync simples -avzP -e 'ssh -p 22' [email protected]:/opt/liitdar/mydemos/
[email protected]'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 的一个主要特点(优点)。

 

你可能感兴趣的:(Linux,常用软件)