rsync 是一种免费使用的命令行工具,可让您在本地和远程同步文件。 使用它,您可以传输文件和目录。
这使得它对于迁移数据、执行备份和镜像非常有用,非常适合网站管理员和管理员执行重复的数据传输任务。
本文将向我们介绍 Rysnc 的工作原理,并提供一些有用的示例。
rsync 是如何工作的?
rsync 是用于类 Unix 系统的远程同步命令行实用程序。 它使用户能够在机器或两台主机之间无缝传输/同步文件。
在底层,rsync 使用增量传输算法。 该算法涉及跨两个主机(源和目标)复制两个文件之间的差异。 源和目标可以是本地的或远程的。 rsync 命令可以通过两种方式执行复制/同步数据任务。 这些都是:
- 使用 ssh、rsh 在两台主机之间复制/同步。
- 使用 TCP rsync 守护进程进行复制/同步。
由于它设计用于类 Unix 系统,因此它也适用于 Linux。
rsync 使用的分步过程如下:
- Rsync 使用 SSH 连接到远程主机并要求输入密码。
- 连接后,远程主机的 rsync 与源通信。
- 然后这些程序将确定需要同步的文件和目录。 它使用增量传输算法来检查文件之间的差异。
如果 rsync 命令在远程但在主机上找不到文件,它将复制并将其传输到主机系统。 同样,对于已经存在的文件(在主机和远程),它会复制它们之间的差异(即更改的部分)。 最后,它将忽略两个系统上的现有文件而不进行任何修改。
rsync 命令通过在将差异推送到目的地之前将差异存储在临时文件中来实现增量更新。
在你开始之前:
- 你需要有 root 或 sudo 权限
- 可以访问终端/命令行
- 用于安全运行 rsync 命令的 SSH 访问
- 您还需要两台机器来制作 rsync。
对于本教程,我们将使用 Linux Mint 21.1 Vera。 使用任何其他 Linux 发行版,您可以轻松地按照指南进行操作而不会出现问题。
为什么使用 rsync 而不是 scp?
Scp(安全复制)是一种流行的命令行协议,允许用户复制文件。 它遵循线性复制方法。 这意味着它只是将文件从源复制到目标。 为了安全地这样做,它使用 SSH。
scp 命令语法如下:
scp option SOURCE DESTINATION
然而,问题仍然存在,为什么使用 rsync 而不是 scp?
好吧,使用 rsync 有很多好处。 这些好处包括:
- 更快的执行:rsync 比 scp 更快,因为它使用远程更新协议。 这允许它只传输差异而不是整个文件。 因此,如果您设置定期同步,您只需在第一次进行完整复制。 从下一次开始,它仅在增量传输算法的帮助下复制更新的部分。
- 减少带宽消耗:由于 rsync 不必再次复制整个文件,因此减少了带宽消耗。 此外,它还使用压缩/解压缩算法来最小化传输过程中的文件大小。
- 更多命令行选项:rsync 在命令行选项的数量上也胜过 scp。 这意味着您可以比 scp 更精细地调整 rsync。
总之,rsync 是一个优秀的增量同步工具。 scp 工具也有助于安全地传输/同步文件。 在现实场景中,如果您正在寻找更直接的方法,scp 适合日常任务。 但是,对于重复性任务,请使用 rsync。
rsync 命令语法
rsync命令如下:
此处,术语定义如下:
- 选项——这些是 rsync 选项
- SOURCE——源目录
- DEST——目标目录
- USER – 远程用户名
- HOST – 远程主机名或 IP 地址
选项是为命令提供附加功能的参数。 例如,您可以使用 -r 选项递归同步数据。 但是,-r 选项不会同步组和用户的所有权、时间戳、权限或符号链接。 相反,您可以使用 -a 选项,它以存档模式运行命令,确保在复制时保留所有文件的所有权、权限和符号链接。
其他 rsync 选项包括:
- -z:压缩数据以节省空间
- -h:提供人类可读的格式输出
- -b:在数据同步过程中进行备份
- -e:使用SSH协议进行远程数据传输
- -progress:显示数据同步进度
- -v:要求 rsync 显示详细输出
- –n:执行空运行以测试数据同步的设置和设置
- -q:抑制 rsync 命令输出和选项
安装 rsync
大多数 Unix 和 Linux 系统都安装了 rsync。 如果您的系统没有 rsync,您可以使用以下命令安装它。
对于 Debian/Ubuntu 和 Mint
sudo apt-get install rsync
对于 Arch Linux
pacman -S rsync
在 Gentoo 上
emerge sys-apps/rsync
在 CentOS/Fedora/REHL 上
sudo yum install rsync
在 openSUSE 上
sudo zypper install rsync
由于我们有 Linux Mint,因此预装了 rsync。 所以可以在终端运行rsync命令查看是否安装。 如果存在,它将解释其功能、选项和其他重要信息。
nitt ~ $ rsync rsync version 3.2.3 protocol version 31 Copyright (C) 1996-2020 by Andrew Tridgell, Wayne Davison, and others. Web site: https://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, hardlink-specials, symlinks, IPv6, atimes, batchfiles, inplace, append, ACLs, xattrs, optional protect-args, iconv, symtimes, prealloc, stop-at, no crtimes Optimizations: SIMD, no asm, openssl-crypto Checksum list: xxh128 xxh3 xxh64 (xxhash) md5 md4 none Compress list: zstd lz4 zlibx zlib none rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm.
现在让我们看看 rsync 在不同用例中的实际应用。
对于本教程,我们将创建两个目录。
- 源目录
- 目标目录
为此,您需要键入以下命令。
mkdir source-directory mkdir dest-directory
此外,我们需要生成文件以使 rsync 工作。 因此,让我们使用以下命令创建一些测试文件。
touch source-directory/file{1..100}
这会在源目录中创建 100 个空文件。 您可以通过输入以下命令来确认它。
ls source-directory Output: nitt rsync-tutorial $ ls dest-directory source-directory nitt rsync-tutorial $ touch source-directory/file{1..100} nitt rsync-tutorial $ ls source-directory file1 file18 file27 file36 file45 file54 file63 file72 file81 file90 file10 file19 file28 file37 file46 file55 file64 file73 file82 file91 file100 file2 file29 file38 file47 file56 file65 file74 file83 file92 file11 file20 file3 file39 file48 file57 file66 file75 file84 file93 file12 file21 file30 file4 file49 file58 file67 file76 file85 file94 file13 file22 file31 file40 file5 file59 file68 file77 file86 file95 file14 file23 file32 file41 file50 file6 file69 file78 file87 file96 file15 file24 file33 file42 file51 file60 file7 file79 file88 file97 file16 file25 file34 file43 file52 file61 file70 file8 file89 file98 file17 file26 file35 file44 file53 file62 file71 file80 file9 file99
注意:我们将 dest-directory 保持为空。
递归地在本地复制目录
尽管 rsync 的主要目的是在本地和远程服务器之间复制文件,但它同样有助于在本地复制文件。
在这种情况下,我们将使用以下 rsync 语法。
rsync 选项 SOURCE DESTINATION
通过运行以下命令将源目录中的文件复制到目标目录。
rsync -r source-directory/ dest-directory
在这里,命令“递归地”将文件从源目录复制到目标目录。
nitt rsync-tutorial $ ls dest-directory source-directory nitt rsync-tutorial $ ls dest-directory nitt rsync-tutorial $ ls source-directory file1 file18 file27 file36 file45 file54 file63 file72 file81 file90 file10 file19 file28 file37 file46 file55 file64 file73 file82 file91 file100 file2 file29 file38 file47 file56 file65 file74 file83 file92 file11 file20 file3 file39 file48 file57 file66 file75 file84 file93 file12 file21 file30 file4 file49 file58 file67 file76 file85 file94 file13 file22 file31 file40 file5 file59 file68 file77 file86 file95 file14 file23 file32 file41 file50 file6 file69 file78 file87 file96 file15 file24 file33 file42 file51 file60 file7 file79 file88 file97 file16 file25 file34 file43 file52 file61 file70 file8 file89 file98 file17 file26 file35 file44 file53 file62 file71 file80 file9 file99 nitt rsync-tutorial $ rsync -r source-directory/ dest-directory nitt rsync-tutorial $ ls dest-directory file1 file18 file27 file36 file45 file54 file63 file72 file81 file90 file10 file19 file28 file37 file46 file55 file64 file73 file82 file91 file100 file2 file29 file38 file47 file56 file65 file74 file83 file92 file11 file20 file3 file39 file48 file57 file66 file75 file84 file93 file12 file21 file30 file4 file49 file58 file67 file76 file85 file94 file13 file22 file31 file40 file5 file59 file68 file77 file86 file95 file14 file23 file32 file41 file50 file6 file69 file78 file87 file96 file15 file24 file33 file42 file51 file60 file7 file79 file88 file97 file16 file25 file34 file43 file52 file61 file70 file8 file89 file98 file17 file26 file35 file44 file53 file62 file71 file80 file9 file99
这会将源目录中的每个文件复制到目标目录。
本地复制单个文件
现在我们已经学习了如何同步两个目录,我们将学习如何复制单个文件。
为此,我们将编辑源目录中的空文件 file1,然后将其与目标目录中存在的同一文件同步。
要编辑文件,请键入以下命令。
nano source-directory/file1
现在将以下文本复制粘贴到 nano 编辑器中。
We're updating file1 in the source-directory. Once we update, we'll push the new update to the dest-directory. rsync doesn't need to re-copy the file using the delta-transfer algorithm. This algorithm checks the differences and then updates destination files accordingly. So, once you save file1 in the source-directory, open file1 in the dest-directory. It'll be empty. However, if we check it after running rsync, you'll see that file1 in dest-directory is updated.
向文件添加内容
现在,关闭并保存文件。
现在让我们使用 rsync 复制文件。
sudo rsync -v --existing source-directory/file1 /dest-directory
#Output nitt rsync-tutorial $ sudo rsync -v --existing source-directory/file1 dest-directory file1 sent 557 bytes received 35 bytes 1,184.00 bytes/sec total size is 474 speedup is 0.80 nitt rsync-tutorial $
您可能已经注意到,我们使用了 –existing 选项。 这将更新现有文件。 如果您在没有 –existing 的情况下运行该命令,即使它运行成功,它也不会更新。
如果目标文件夹中不存在您要复制的单个文件,则需要运行以下命令。
rsync -v source-directory/newfile dest-directory
#Output nitt rsync-tutorial $ rsync -v source-directory/newfile dest-directory newfile sent 82 bytes received 35 bytes 234.00 bytes/sec total size is 0 speedup is 0.00
复制多个文件
如果您打算复制多个文件,则必须提及两个文件的源路径,然后是目标目录。
为此,我们将在源目录中创建两个新文件:newfile2 和 newfile3。 运行以下命令来执行此操作。
touch source-directory/newfile{2..3}
现在,使用以下命令同步多个文件。
rsync -v source-directory/newfile2 source-directory/newfile3 dest-directory
#Output nitt rsync-tutorial $ rsync -v source-directory/newfile2 source-directory/newfile3 dest-directory newfile2 newfile3 sent 135 bytes received 54 bytes 378.00 bytes/sec total size is 0 speedup is 0.00 nitt rsync-tutorial $
将文件或目录从本地复制到远程
本地到远程传输的 rsync 语法如下。
rsync option SOURCE [email protected]:DEST
如您所见,您需要知道远程机器的 IP 地址才能完成这项工作。 此外,您需要在源文件后添加 IP 地址。
rsync -av /Projects/rsync-tutorial/source-directory 192.168.192.200: /Projects/rsync-tutorial/dest-directory
在开始传输之前,它会要求输入密码。 输入它继续。 另外,如果用户在远程机器上是不同的,那么你需要在 IP 地址前加上 @。
rsync -av /Projects/rsync-tutorial/source-directory [email protected]: /Projects/rsync-tutorial/dest-directory
如果您打算复制多个文件或目录,则需要列出文件或目录路径。 它的语法如下。
rsync -av /Projects/rsync-tutorial/source-directory/newfile1 /Projects/rsync-tutorial/source-directory/newfile2 192.168.192.200: /Projects/rsync-tutorial/dest-directory
远程传输特定协议
rsync 允许您指定特定的文件传输协议。 为此,您需要使用 -e 选项,后跟协议。
例如,如果要使用 ssh 协议,则必须将 -e ssh 附加到 rsync 命令。
rsync -e ssh /Projects/rsync-tutorial/source-directory 192.168.192.200: /Projects/rsync-tutorial/dest-directory
将文件或目录从远程复制到本地
本地到远程传输的 rsync 语法如下。
sync option [email protected]:SRC DEST
这充当拉取请求。 因此,要从远程服务器获取所需的文件/目录到本地计算机,您需要运行以下命令。
rsync -av 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
该命令将远程机器的目标目录中的文件拉到本地机器的源目录中。
同样,您可以通过完整路径提及来提取特定文件。
rsync -av 192.168.192.200: /Projects/rsync-tutorial/dest-directory/newfile4 /Projects/rsync-tutorial/source-directory
要将多个文件或目录从本地复制到远程,您需要在服务器 IP 地址后的大括号(以逗号分隔)内指定路径。
rsync -av 192.168.192.200: {/Projects/rsync-tutorial/dest-directory/, /home/music/2023-music} /Projects/rsync-tutorial/source-directory
同样,您也可以提及文件。
在传输过程中显示实时进度
查看较大数据备份的实时进度是个好主意。 为此,您需要使用 –progress 标志。 将其附加到 rsync 命令,您可以看到传输速度、剩余时间和传输的数据量。
rsync -av --progress 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
传输完成后删除源文件
您可以使用 -remove-source-files 标志在完成传输后删除源文件。 这可以在许多情况下为您提供帮助,尤其是在不留下任何痕迹的情况下保护您的文件或仅使用它们来释放空间时。
rsync -av --remove-source-files 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
rsync 空运行
Rysnc 还允许您在实际运行之前进行评估。 试运行让您看看您是否在做正确的事情。 毕竟,您不想错误地更新文件甚至删除它们。
您需要使用 –dry-run 选项进行试运行。 将它添加到命令中,然后是源和目标。
sudo rsync -v --dry-run source-directory/file1 dest-directory
nitt rsync-tutorial $ sudo rsync -v --dry-run source-directory/file1 dest-directory file1 sent 43 bytes received 19 bytes 124.00 bytes/sec total size is 474 speedup is 7.65 (DRY RUN) nitt rsync-tutorial
输出类似于我们之前运行它的方式。 但是,您会注意到输出末尾提到了 (DRY RUN)。
设置最小和最大文件大小
Rysnc 还允许您在传输过程中设置最小或最大文件大小。
所以,如果你想以最小 15KB 的大小传输,你需要使用 –min-size=15K。
rsync -av --min-size=15k 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
该命令只会复制最小文件大小为 15KB 的文件。 如果文件大小低于它,它将忽略它。
同样,您可以使用 –max-size 标志来设置最大文件大小。
rsync -av --max-size=450k 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
在这里,rsync 将忽略大小超过 450K 的文件。
设置带宽限制
如果您正在执行其他带宽密集型任务,则可以设置 rysnc 来设置传输的带宽限制。 为此,请使用 –bwlimit=KB/s。
rsync -av --bwlimit=100 --progress 192.168.192.200: /Projects/rsync-tutorial/dest-directory /Projects/rsync-tutorial/source-directory
在这里,我们在传输过程中将带宽设置为 100Kb/s。
最后的话
这使我们结束了 rsync 命令指南。 我们学习了如何使用 rysnc 并有效地涵盖了大量命令。
然而,rysnc 提供的方式远不止我们所涵盖的。 这就是为什么您可能还想浏览 rsync 手册页,其中涵盖了 rsync 的各个方面。
接下来,查看适用于 Windows、Linux 和 macOS 的命令行备忘单。