8

使用Rsync在 Linux 上传输文件的示例

 2 years ago
source link: https://blog.51cto.com/sharepointchina/5945623
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用Rsync在 Linux 上传输文件的示例

精选 原创

低代码布道者 2022-12-15 17:58:39 博主文章分类:架构设计 ©著作权

文章标签 ide hg bash 文章分类 软件架构 软件研发 yyds干货盘点 阅读数134

在 Linux 操作系统上,“rsync”代表远程同步同步。它是用于将文件和目录从源 (SRC) 同步(复制)到目标 (DEST) 的实用程序。

使用Rsync在 Linux 上传输文件的示例_bash

文件和目录可以在本地主机上同步,也可以在远程主机上同步。在 SRC 上,文件和目录将被同步。而在 DEST 上,将发生同步的文件和目录。

主要分享低代码、微服务、容器化、SAAS‬、系统架构方面的的‬内容‬‬,希望‬大家‬点赞‬,评论,关注‬。

此外,rsync 命令可以递归地复制文件和目录,复制符号链接,保留(权限、组、修改时间和所有权)文件身份。

Rsync 可以使用两种不同的方式来同步/复制文件或目录:

  • 使用远程 shell:ssh 或 rsh
  • 直接通过 TCP 使用 rsync 守护进程

更重要的是,rsync 使用增量传输算法,它只在 SRC 和 DEST 之间复制/同步不同的数据。

本教程将通过动手实践和示例来介绍使用 rsync 命令的多种方法。我们还将详细解释 rsync 命令最常用的选项。

rsync 命令语法

在深入研究 rsync 命令的使用方法之前,让我们先看看它的语法:

rsync [options] <SRC_PATH> <DEST_PATH>

1.在本地机器上复制/同步文件

rsync 命令可用于在本地机器上复制/同步文件。

假设您要将名为“/home/linoxide”的用户“linoxide”的主目录中的文件复制到文件夹“/opt/test”。

$ sudo rsync -zvh /home/linoxide/computer-networking.pdf /opt/test/
computer-networking.pdf

sent 1.01K bytes received 35 bytes 2.10K bytes/sec
total size is 3.03K speedup is 2.89

参数说明:

-z: 压缩

-v: 详细输出

-h: 人类可读

2.在本地机器上复制/同步目录

要将所有文件从 SRC 本地目录传输到 DEST 目录,请运行带有选项的 rsync 命令-zavh。

假设您要将名为“/home/linoxide”的用户“linoxide”的主目录复制到文件夹“/opt/backup”。

$ sudo rsync -zavh /home/linoxide/ /opt/backup/

sending incremental file list
./
.bash_history
.bash_logout
.bashrc
.lesshst
.profile
.sudo_as_admin_successful
.viminfo
computer-networking.pdf
.ssh/
.ssh/known_hosts

sent 4.85K bytes received 198 bytes 10.10K bytes/sec
total size is 9.59K speedup is 1.90

参数说明:

-a:在复制/同步过程中存档数据

您可以验证 rsync 命令是否成功?运行以下命令:

$ ls /opt/backup/ -la
total 40
drwxr-xr-x 3 linoxide linoxide 4096 Thg 6 15 16:02 .
drwxr-xr-x 4 root root 4096 Thg 6 15 16:25 ..
-rw------- 1 linoxide linoxide 817 Thg 6 15 16:01 .bash_history
-rw-r--r-- 1 linoxide linoxide 220 Thg 6 11 15:58 .bash_logout
-rw-r--r-- 1 linoxide linoxide 3771 Thg 6 11 15:58 .bashrc
-rw-rw-r-- 1 linoxide linoxide 3028 Thg 2 25 2017 computer-networking.pdf
-rw------- 1 linoxide linoxide 40 Thg 6 15 15:51 .lesshst
-rw-r--r-- 1 linoxide linoxide 807 Thg 6 11 15:58 .profile
drwx------ 2 linoxide linoxide 4096 Thg 6 13 17:58 .ssh
-rw-r--r-- 1 linoxide linoxide 0 Thg 6 15 16:02 .sudo_as_admin_successful
-rw------- 1 linoxide linoxide 680 Thg 6 14 16:50 .viminfo

3. 使用特定端口通过 ssh 进行 Rsync

您可以使用 ssh(安全外壳)复制/同步数据,您的数据将通过加密的安全连接传输。在 Internet 上传输数据时,没有人可以读取您的数据。当您使用 rsync 命令时,您需要提供用户/root 密码来完成特定任务。使用 SSH 选项将以加密方式发送您的登录信息,以确保您的密码安全。

使用特定的 ssh 端口将文件从远程复制到本地。为了使用 rsync 命令指定协议,您需要使用-e带有您要使用的协议名称的选项。

例如,以下 rsync 命令将通过远程 shell 将文件“deploy.yaml”从远程计算机复制到用户“linoxide”的主目录。

$ rsync -avz -e "ssh -p 22" [email protected]:/home/cas/deploy.yaml /home/linoxide/
[email protected]'s password:
receiving incremental file list

sent 20 bytes received 60 bytes 32.00 bytes/sec
total size is 239 speedup is 2.99

4.从本地机器复制/同步文件和目录到远程机器

假设您要将本地主机中用户“linoxide”的主目录中的文件夹“test-data”复制/同步到远程计算机“[email protected]”的主目录,运行命令:

$ rsync -azrvh /home/linoxide/computer-networking.pdf [email protected]:
[email protected]'s password:
sending incremental file list
computer-networking.pdf

sent 1.04K bytes received 35 bytes 430.80 bytes/sec
total size is 3.03K speedup is 2.81

参数说明:

-r: 递归复制

5.从远程机器复制/同步文件和目录到本地机器

rsync 命令可帮助您传输要复制到本地主机的远程目录“文档”。

假设您要将远程主机“[email protected]”中的文件/目录复制/同步到用户“linoxide”的主目录。

$ rsync -zavhr [email protected]:document /home/linoxide
[email protected]'s password:
receiving incremental file list
document/
document/kubernetes.txt

sent 47 bytes received 139 bytes 74.40 bytes/sec
total size is 0 speedup is 0.00

6. rsync -include 和 -exclude 选项

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

在此示例中,rsync 命令将仅包含以“k”开头的文件和目录,并排除所有其他文件和目录。

$ rsync -avhz -e "ssh -p 22" --include 'k*' --exclude '*' [email protected]: /home/linoxide

[email protected]'s password:
receiving incremental file list
./
k8s.yaml
kubernetes.yaml
kuber/

sent 88 bytes received 280 bytes 147.20 bytes/sec
total size is 73 speedup is 0.20

7.设置文件的最大大小

您可以指定要传输或同步的最大文件大小。您可以使用“--max-size”选项来做到这一点。

在以下示例中,最大文件大小为 200k,因此该命令将仅传输那些等于或小于 200k 的文件。

$ rsync -zavhr --max-size='200k' [email protected]: /home/linoxide
[email protected]'s password:
receiving incremental file list
.bash_history
.bash_logout
.bashrc
.profile
.sudo_as_admin_successful
.viminfo
computer-networking.pdf

sent 2.58K bytes received 51.60K bytes 21.67K bytes/sec
total size is 113.24K speedup is 2.09

8.传输成功后删除源文件

现在,假设您有一个 Web 服务器和一个数据备份服务器,您创建了一个每日备份并将其与您的备份服务器同步,现在您不想将备份的本地副本保留在您的 Web 服务器中。那么,您会等待传输完成然后手动删除该本地备份文件吗?当然不。这种自动删除可以使用“--remove-source-files”选项来完成。

$ ls /home/directory
computer-networking.pdf deploy.yaml document k8s.yaml kuber kubernetes.yaml

------
$ rsync -azvh --remove-source-files /home/linoxide/deploy.yaml [email protected]:
[email protected]'s password:
sending incremental file list

sent 70 bytes received 20 bytes 36.00 bytes/sec
total size is 239 speedup is 2.66

再次运行'ls'命令,你可以看到文件'deploy.yaml'在SRC上被删除了。

$ ls
computer-networking.pdf document k8s.yaml kuber kubernetes.yaml

9.查找SRC和DEST在文件和目录上的区别

为了找到 SRC 和 DEST 之间目录文件的任何差异,您可以使用 '-i' 选项运行 rsync 命令。

$ rsync -avzi /home/linoxide/cloud [email protected]:
[email protected]'s password:
sending incremental file list
cd+++++++++ cloud/
<f+++++++++ cloud/computer-networking.pdf
<f+++++++++ cloud/k8s.yaml
<f+++++++++ cloud/kubernetes.yaml
cd+++++++++ cloud/document/
<f+++++++++ cloud/document/kubernetes.txt
cd+++++++++ cloud/kuber/
<f+++++++++ cloud/kuber/test.txt

sent 1,472 bytes received 127 bytes 1,066.00 bytes/sec
total size is 3,101 speedup is 1.94

10.限制带宽

您可以在运行 rsync 命令时使用 '--bwlimit=<KB/s>' 选项设置带宽限制。

假设您要将数据传输速率限制为 300KB/s,请运行以下命令:

$ rsync -avzh --bwlimit=300 /home/linoxide/coursera.png [email protected]:

[email protected]'s password:
sending incremental file list
coursera.png

sent 2.93M bytes received 35 bytes 234.48K bytes/sec
total size is 3.00M speedup is 1.02

11. scp失败时恢复传输

有时,您必须通过 scp 命令传输一个非常大的文件,但在复制时出错,并且由于文件大小和时间消耗过大而无法再次使用 scp 命令开始传输。

在这种情况下,您可以使用 rsync 命令从出现错误的位置开始复制文件。例如,将主目录中的大型 ISO 文件复制到远程主机 '[email protected]'

$ scp /home/linoxide/ubuntu-18.04.4-live-server-amd64.iso [email protected]:

[email protected]'s password:
ubuntu-18.04.4-live-server-amd64.iso 32% 278MB 86.7MB/s 00:06 ETA

该进程已被用户杀死。当时,32% 的文件被传输。现在,您可以通过运行 rsync 继续当前的工作:

$ rsync -P -avzh ubuntu-18.04.4-live-server-amd64.iso [email protected]:

[email protected]'s password:
sending incremental file list
ubuntu-18.04.4-live-server-amd64.iso
912.26M 100% 15.89MB/s 0:00:54 (xfr#1, to-chk=0/1)

sent 541.25M bytes received 132.44K bytes 9.25M bytes/sec
total size is 912.26M speedup is 1.69

使用Rsync在 Linux 上传输文件的示例_bash_02

rsync 命令恢复传输文件

12.传输数据时显示进度

如果您想在运行 rsync 命令时查看传输进度,可以使用“--progress”选项。

它将显示文件和完成复制的剩余时间。

$ rsync -avh --progress /home/linoxide/cloud/ [email protected]:
[email protected]'s password:
sending incremental file list
./
computer-networking.pdf
3.03K 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=8/10)
coursera.png
3.00M 100% 114.38MB/s 0:00:00 (xfr#2, to-chk=7/10)
k8s.yaml
39 100% 1.52kB/s 0:00:00 (xfr#3, to-chk=6/10)
kubernetes.yaml
34 100% 1.33kB/s 0:00:00 (xfr#4, to-chk=5/10)
ubuntu-18.04.4-live-server-amd64.iso
912.26M 100% 89.60MB/s 0:00:09 (xfr#5, to-chk=4/10)
document/
document/kubernetes.txt
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/10)
kuber/
kuber/test.txt
0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=0/10)

sent 915.49M bytes received 164 bytes 79.61M bytes/sec
total size is 915.26M speedup is 1.00

使用Rsync在 Linux 上传输文件的示例_hg_03

带有进度的 rsync 命令

在本教程中,我们通过一些示例学习了如何使用 rsync 命令来复制或同步文件/目录。

主要分享低代码、微服务、容器化、SAAS‬、系统架构方面的的‬内容‬‬,希望‬大家‬点赞‬,评论,关注‬。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK