
Rsync(远程同步)是Linux/Unix系统中用于远程和本地复制及同步文件和目录的常用工具。
利用rsync命令,您可以轻松地在不同目录、硬盘和网络之间进行数据的远程和本地复制与同步,进行数据备份,以及在两台Linux系统间创建镜像。
这篇文章[1]介绍了rsync命令的16个基本和进阶用法,帮助您在Linux系统上实现文件的远程和本地传输。执行rsync命令不需要root权限。
rsync 命令具有多项优点,例如:
rsync命令遵循如下语法:
# rsync [OPTIONS] SOURCE DESTINATION
以下是rsync命令中不同组成部分和选项的说明:
我们可以根据您的 Linux 发行版借助以下软件包管理器安装 rsync 软件包。
$ sudo apt install rsync [On Debian, Ubuntu and Mint]
$ sudo yum install rsync [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/rsync [On Gentoo Linux]
$ sudo apk add rsync [On Alpine Linux]
$ sudo pacman -S rsync [On Arch Linux]
$ sudo zypper install rsync [On OpenSUSE]
若要在同一台计算机内复制或同步文件,您可以执行如下命令,将单个文件从一个目录移动到另一个目录。
例如,假设我们需要将名为 backup.tar 的文件复制或同步到本地的 /tmp/backups/ 目录下。
[root@tecmint]# rsync -zvh backup.tar.gz /tmp/backups/
created directory /tmp/backups
backup.tar.gz
sent 224.54K bytes received 70 bytes 449.21K bytes/sec
total size is 224.40K speedup is 1.00
在上面的示例中,您可以看到,如果目标尚不存在,rsync 将自动为目标创建一个目录。

以下命令将把所有文件从一个目录传输或同步到同一台机器上的另一个目录。
在此示例中,/root/rpmpkgs 包含一些 rpm 软件包文件,并且您希望将该目录复制到 /tmp/backups/ 文件夹中。
[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 3.47M bytes received 96 bytes 2.32M bytes/sec
total size is 3.74M speedup is 1.08

要将目录从本地服务器复制到远程服务器,可以使用以下命令,该命令会将目录从本地计算机同步到远程计算机。
例如,如果本地计算机中有一个文件夹“rpmpkgs”,其中包含一些 RPM 软件包,并且您希望将该本地目录的内容发送到远程服务器,则可以使用以下命令。
# rsync -avzh /root/rpmpkgs root@192.168.0.141:/root/
The authenticity of host '192.168.0.141 (192.168.0.141)' can't be established.
ED25519 key fingerprint is SHA256:bH2tiWQn4S5o6qmZhmtXcBROV5TU5H4t2C42QDEMx1c.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.141' (ED25519) to the list of known hosts.
root@192.168.0.141's password:
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 3.74M bytes received 96 bytes 439.88K bytes/sec
total size is 3.74M speedup is 1.00

此命令将帮助您将远程目录同步到本地目录。在此示例中,远程服务器上的目录 /root/rpmpkgs 被复制到本地计算机的 /tmp/myrpms 中。
# rsync -avzh root@192.168.0.141:/root/rpmpkgs /tmp/myrpms
root@192.168.0.141's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
sent 104 bytes received 3.49M bytes 997.68K bytes/sec
total size is 3.74M speedup is 1.07

通过rsync,我们可以使用SSH(Secure Shell)进行数据传输,在传输数据时使用SSH协议您可以确保您的数据在加密的安全连接中传输,这样在传输过程中没有人可以读取您的数据通过互联网上的电线。
此外,当我们使用 rsync 时,我们需要提供用户/root 密码来完成该特定任务,因此使用 SSH 选项将以加密方式发送您的登录信息,以便您的密码安全。
要通过 SSH 使用 rsync,可以使用 -e 选项指定远程 shell 命令,通常是 ssh,如图所示。
# rsync [OPTIONS] -e ssh /path/to/source user@remote:/path/to/destination
要将文件从远程服务器同步到本地服务器,您可以使用“-e”选项和您要使用的协议名称来指定 rsync 协议。
在此示例中,我们将使用带有“-e”选项的“ssh”并执行数据传输。
# rsync -avzhe ssh root@192.168.0.141:/root/anaconda-ks.cfg /tmp
root@192.168.0.141's password:
receiving incremental file list
anaconda-ks.cfg
sent 43 bytes received 1.10K bytes 325.43 bytes/sec
total size is 1.90K speedup is 1.67

要使用 SSH 将文件从本地服务器同步到远程服务器,您可以利用以下命令,如下所示。
# rsync -avzhe ssh backup.tar.gz root@192.168.0.141:/backups/
root@192.168.0.141's password:
sending incremental file list
created directory /backups
backup.tar.gz
sent 224.59K bytes received 66 bytes 64.19K bytes/sec
total size is 224.40K speedup is 1.00

要显示将数据从一台计算机传输到另一台计算机时的进度,我们可以使用“--progress”选项,该选项显示文件以及完成传输的剩余时间。
# rsync -avzhe ssh --progress /root/rpmpkgs root@192.168.0.141:/root/rpmpkgs
root@192.168.0.141's password:
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
1.47M 100% 31.80MB/s 0:00:00 (xfr#1, to-chk=3/5)
rpmpkgs/mod_ssl-2.4.37-40.module_el8.5.0+852+0aafc63b.x86_64.rpm
138.01K 100% 2.69MB/s 0:00:00 (xfr#2, to-chk=2/5)
rpmpkgs/nagios-4.4.6-4.el8.x86_64.rpm
2.01M 100% 18.45MB/s 0:00:00 (xfr#3, to-chk=1/5)
rpmpkgs/nagios-plugins-2.3.3-5.el8.x86_64.rpm
120.48K 100% 1.04MB/s 0:00:00 (xfr#4, to-chk=0/5)
sent 3.74M bytes received 96 bytes 1.50M bytes/sec
total size is 3.74M speedup is 1.00
