如何在Linux中使用Rsync命令:16个实用示例

Rsync远程同步)是在Linux/Unix系统中远程和本地复制和同步文件和目录最常用的命令。

借助rsync命令,您可以在目录、磁盘和网络之间远程和本地复制和同步数据,执行数据备份,并在两台Linux机器之间进行镜像

本文介绍16rsync命令的基本和高级用法,用于在基于Linux的机器中远程和本地传输文件。您无需是root用户即可运行rsync命令。

Rsync 命令的优势

它提供了几个优点,包括:

  • 高效的文件传输 – rsync 使用增量传输算法,这意味着它只传输源文件和目标文件之间的差异,大大减少了传输的数据量,使其在同步大文件或目录时效率高。
  • 远程文件同步 – rsync 支持通过 SSH 进行本地和远程文件传输,这允许在本地和远程系统之间进行同步,或在多台机器之间镜像目录。
  • 增量备份 – rsync 非常适合增量备份,因为它通过仅传输新文件或修改过的文件来高效地创建和更新备份。
  • 保留文件权限 – rsync 可以保留各种文件属性,例如权限、所有权、时间戳和符号链接,确保复制的文件在目标位置保持其原始特性。
  • 带宽控制 – rsync 允许您在文件传输过程中限制带宽使用,因为它在发送和接收数据时使用压缩和解压缩方法。
  • 更快 – rsync 在传输文件时可以比 scp (安全复制) 更快,特别是在同步大型目录或处理已经部分传输或已存在于目标位置的文件时。

rsync 命令语法

rsync 命令的语法如下:

# rsync [OPTIONS] SOURCE DESTINATION

下面是使用 rsync 命令时的不同组件和选项的解释:

  • -v – 显示详细的传输信息。
  • -r – 递归复制数据(但在传输数据时不保留时间戳和权限)。
  • -a – 存档模式,允许递归复制文件,同时保留符号链接、文件权限、用户和组所有权以及时间戳。
  • -z – 在传输过程中压缩文件以减少网络使用量。
  • -h – 以人类可读的格式输出数字。
  • -P – 在传输过程中显示进度。
  • SOURCE – 指定要传输的源文件或目录,可以是本地位置或远程位置。
  • DESTINATION – 指定要复制文件或目录的目标路径。与源文件类似,可以是本地路径或远程位置。

在 Linux 系统中安装 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]    

1. 本地复制/同步文件

要在本地复制或同步文件,您可以使用以下命令,该命令将在本地机器上从一个位置同步单个文件到另一个位置。

在这个例子中,一个名为 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 将自动为目标创建一个目录。

Rsync Local Files

2. 将目录本地复制/同步

以下命令将在同一台机器上将一个目录中的所有文件传输或同步到另一个目录。

在这个例子中,/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
Rsync Local Directory

3. 将目录从本地复制到远程服务器

要将目录从本地服务器复制到远程服务器,您可以使用以下命令,该命令将从本地同步一个目录到远程机器。

例如,如果您的本地计算机中有一个名为“rpmpkgs”的文件夹,其中包含一些RPM包,如果您希望将该本地目录的内容发送到远程服务器,您可以使用以下命令。

# rsync -avzh /root/rpmpkgs [email protected]:/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.
[email protected]'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
Rsync Directory Remote System

4. 将目录从远程复制到本地服务器

此命令将帮助您将远程目录同步到本地目录。在这个例子中,位于远程服务器上的一个目录/root/rpmpkgs正在被复制到您的本地计算机的/tmp/myrpms中。

# rsync -avzh [email protected]:/root/rpmpkgs /tmp/myrpms

[email protected]'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 Remote Directory to Local

5. 通过 SSH 进行 Rsync 同步

使用rsync,我们可以使用SSH(安全外壳协议)进行数据传输,使用SSH协议来传输数据,确保您的数据在安全连接中进行加密传输,以便在互联网上传输数据时,没有人能够读取您的数据。

此外,当我们使用rsync时,我们需要提供用户/root密码来完成特定任务,因此使用SSH选项将以加密方式发送您的登录信息,从而确保您的密码安全。

要使用rsync通过SSH,您可以使用-e选项来指定远程shell命令,通常是ssh,如下所示。

# rsync [OPTIONS] -e ssh /path/to/source user@remote:/path/to/destination

6. 通过SSH从远程服务器复制文件到本地服务器

要将文件从远程服务器同步到本地服务器,可以使用rsync并使用“-e”选项指定要使用的协议名称。

在这个例子中,我们将使用“ssh”和“-e”选项进行数据传输。

# rsync -avzhe ssh [email protected]:/root/anaconda-ks.cfg /tmp

[email protected]'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
Rsync Copy Remote File to Local

7. 使用SSH从本地服务器复制文件到远程服务器

要使用SSH将文件从本地服务器同步到远程服务器,您可以使用以下命令:

# rsync -avzhe ssh backup.tar.gz [email protected]:/backups/

[email protected]'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
Rsync Copy Local File to Remote

8. 在使用Rsync传输数据时显示进度

要在从一台计算机传输数据到另一台计算机时显示进度,我们可以使用’–progress’选项,该选项会显示文件以及完成传输所需的剩余时间。

# rsync -avzhe ssh --progress /root/rpmpkgs [email protected]:/root/rpmpkgs

[email protected]'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
Rsync Progress While Copying Files

9. 使用Rsync包含特定扩展名的文件

在rsync操作期间包含特定文件或模式,您可以使用–include选项,并使用与所有文件匹配的扩展名。

# rsync -avz --include='*.txt' /path/to/source/ user@remote:/path/to/destination/

在提供的示例中,rsync将仅在传输过程中从路径到源目录中包含具有.txt扩展名的文件。

10. 使用Rsync排除特定扩展名的文件

类似地,要在rsync操作期间排除特定扩展名,您可以使用–exclude选项以及通配符模式。

# rsync -avz --exclude='*.ext' /path/to/source/ user@remote:/path/to/destination/

在rsync传输过程中,rsync将排除指定扩展名(*.ext)的文件,同时包含所有其他文件和目录。

11. 使用Rsync包含和排除文件

在rsync操作期间包含和排除特定文件或模式,您可以同时使用--include--exclude选项以适当的通配符模式。

这两个选项允许我们通过指定参数来包含排除文件,这些选项帮助我们指定那些您希望在同步中包含的文件或目录,并排除您不希望传输的文件和文件夹。

在这个例子中,rsync命令将仅包含以‘R’开头的文件和目录,并排除所有其他文件和目录。

# rsync -avze ssh --include 'R*' --exclude '*' [email protected]:/var/lib/rpm/ /root/rpm
Rsync Include and Exclude Files

12. 使用Rsync的–delete选项

如果源目录中不存在文件或目录,但在目标目录中已存在,您可能希望在同步时删除目标目录中已存在的文件/目录。

我们可以使用‘--delete’选项来删除源目录中不存在的文件。

源和目标已同步。现在在目标创建一个新文件test.txt

[root@tecmint:~]# cd /root/rpm/
[root@tecmint:~/rpm]# touch test.txt
[root@tecmint:~/rpm]# rsync -avz --delete [email protected]:/var/lib/rpm/ /root/rpm/

[email protected]'s password: 
receiving incremental file list
deleting test.txt
./
.dbenv.lock
.rpm.lock
Basenames
Conflictname
Dirnames
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
Packages
Providename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.001
__db.002
__db.003

sent 445 bytes  received 18,543,954 bytes  2,472,586.53 bytes/sec
total size is 71,151,616  speedup is 3.84

目标使用“–delete”选项与源同步时,删除了文件test.txt

Rsync Delete Option

13. 使用Rsync设置文件传输限制

您可以指定要传输或同步的最大文件大小。您可以使用“--max-size”选项完成。在此示例中,最大文件大小为200k,因此此命令将仅传输大小等于或小于200k的文件。

# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ [email protected]:/root/tmprpm

[email protected]'s password: 
sending incremental file list
created directory /root/tmprpm
./
.dbenv.lock
.rpm.lock
Conflictname
Enhancename
Filetriggername
Group
Installtid
Name
Obsoletename
Recommendname
Requirename
Sha1header
Sigmd5
Suggestname
Supplementname
Transfiletriggername
Triggername
__db.002

sent 129.52K bytes  received 396 bytes  28.87K bytes/sec
total size is 71.15M  speedup is 547.66
Rsync Set Max File Transfer Size

14. 在传输后自动删除源文件

现在,假设您有主要Web服务器和数据备份服务器,您创建了每日备份并将其与备份服务器同步,但现在您不想在Web服务器中保留该备份的本地副本。

那么,您会等待传输完成,然后手动删除该本地备份文件吗?当然不会。可以使用“--remove-source-files”选项进行自动删除。

# rsync --remove-source-files -zvh backup.tar.gz [email protected]:/tmp/backups/

[email protected]'s password: 
backup.tar.gz

sent 795 bytes  received 2.33K bytes  894.29 bytes/sec
total size is 267.30K  speedup is 85.40

[root@tecmint:~]# ls -l backup.tar.gz

ls: cannot access 'backup.tar.gz': No such file or directory
Rsync Delete Source File After Transfer

15. 使用Rsync进行试运行

如果您是使用rsync的新手,并不知道您的命令将要执行什么。Rsync可能会在目标文件夹中弄乱事情,然后撤消可能是一项繁琐的工作。

使用此选项不会对文件进行任何更改,并显示命令的输出,如果输出与您要执行的操作完全相同,则可以从命令中删除“–dry-run”选项并在终端上运行。

# rsync --dry-run --remove-source-files -zvh backup.tar.gz [email protected]:/tmp/backups/

[email protected]'s password: 
backup.tar.gz

sent 50 bytes  received 19 bytes  19.71 bytes/sec
total size is 267.30K  speedup is 3,873.97 (DRY RUN)
Rsync Dry Run

16. 设置带宽限制和传输文件

您可以使用“–bwlimit”选项在从一台机器传输数据到另一台机器时设置带宽限制。此选项帮助我们限制I/O带宽。

# rsync --bwlimit=100 -avzhe ssh  /var/lib/rpm/  [email protected]:/root/tmprpm/
[email protected]'s password:
sending incremental file list
sent 324 bytes  received 12 bytes  61.09 bytes/sec
total size is 38.08M  speedup is 113347.05

此外,默认情况下,rsync仅同步更改的块和字节,如果您明确希望同步整个文件,则可以使用“-W”选项。

# rsync -zvhW backup.tar /tmp/backups/backup.tar
backup.tar
sent 14.71M bytes  received 31 bytes  3.27M bytes/sec
total size is 16.18M  speedup is 1.10
结论

以上是关于rsync及其功能的概述。如果您想进一步了解其广泛的选项和功能,请参考详尽的手册页面(man页面)。

Source:
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/