This commit is contained in:
Xingyu Wang 2020-08-29 21:48:59 +08:00
parent 80300e64aa
commit fe6effa37e
2 changed files with 182 additions and 182 deletions

View File

@ -1,182 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (SCP users migration guide to rsync)
[#]: via: (https://fedoramagazine.org/scp-users-migration-guide-to-rsync/)
[#]: author: (chasinglogic https://fedoramagazine.org/author/chasinglogic/)
SCP users migration guide to rsync
======
![][1]
As part of the [8.0 pre-release announcement,][2] the OpenSSH project stated that they consider the scp protocol outdated, inflexible, and not readily fixed. They then go on to recommend the use of sftp or rsync for file transfer instead.
Many users grew up on the _scp_ command, however, and so are not familiar with rsync. Additionally, rsync can do much more than just copy files, which can give a beginner the impression that its complicated and opaque. Especially when broadly the scp flags map directly to the cp flags while the rsync flags do not.
This article will provide an introduction and transition guide for anyone familiar with scp. Lets jump into the most common scenarios: Copying Files and Copying Directories.
### Copying files
For copying a single file, the scp and rsync commands are effectively equivalent. Lets say you need to ship _foo.txt_ to your home directory on a server named _server._
```
$ scp foo.txt me@server:/home/me/
```
The equivalent rsync command requires only that you type rsync instead of scp:
```
$ rsync foo.txt me@server:/home/me/
```
### Copying directories
For copying directories, things do diverge quite a bit and probably explains why rsync is seen as more complex than scp. If you want to copy the directory _bar_ to _server_ the corresponding scp command looks exactly like the cp command except for specifying ssh information:
```
$ scp -r bar/ me@server:/home/me/
```
With rsync, there are more considerations, as its a more powerful tool. First, lets look at the simplest form:
```
$ rsync -r bar/ me@server:/home/me/
```
Looks simple right? For the simple case of a directory that contains only directories and regular files, this will work. However, rsync cares a lot about sending files exactly as they are on the host system. Lets create a slightly more complex, but not uncommon, example.
```
# Create a multi-level directory structure
$ mkdir -p bar/baz
# Create a file at the root directory
$ touch bar/foo.txt
# Now create a symlink which points back up to this file
$ cd bar/baz
$ ln -s ../foo.txt link.txt
# Return to our original location
$ cd -
```
We now have a directory tree that looks like the following:
```
bar
├── baz
│ └── link.txt -> ../foo.txt
└── foo.txt
1 directory, 2 files
```
If we try the commands from above to copy bar, well notice very different (and surprising) results. First, lets give scp a go:
```
$ scp -r bar/ me@server:/home/me/
```
If you ssh into your server and look at the directory tree of bar youll notice an important and subtle difference from your host system:
```
bar
├── baz
│ └── link.txt
└── foo.txt
1 directory, 2 files
```
Note that _link.txt_ is no longer a symlink. It is now a full-blown copy of _foo.txt_. This might be surprising behavior if youre used to _cp_. If you did try to copy the _bar_ directory using _cp -r_, you would get a new directory with the exact symlinks that _bar_ had. Now if we try the same rsync command from before well get a warning:
```
$ rsync -r bar/ me@server:/home/me/
skipping non-regular file "bar/baz/link.txt"
```
Rsync has warned us that it found a non-regular file and is skipping it. Because you didnt tell it to copy symlinks, its ignoring them. Rsync has an extensive manual section titled “SYMBOLIC LINKS” that explains all of the possible behavior options available to you. For our example, we need to add the links flag.
```
$ rsync -r --links bar/ me@server:/home/me/
```
On the remote server we see that the symlink was copied over as a symlink. Note that this is different from how scp copied the symlink.
```
bar/
├── baz
│ └── link.txt -> ../foo.txt
└── foo.txt
1 directory, 2 files
```
To save some typing and take advantage of more file-preserving options, use the archive (-a for short) flag whenever copying a directory. The archive flag will do what most people expect as it enables recursive copy, symlink copy, and many other options.
```
$ rsync -a bar/ me@server:/home/me/
```
The rsync man page has in-depth explanations of what the archive flag enables if youre curious.
### Caveats
There is one caveat, however, to using rsync. Its much easier to specify a non-standard ssh port with scp than with rsync. If _server_ was using port 8022 SSH connections, for instance, then those commands would look like this:
```
$ scp -P 8022 foo.txt me@server:/home/me/
```
With rsync, you have to specify the “remote shell” command to use. This defaults to _ssh_. You do so using the **-e flag.
```
$ rsync -e 'ssh -p 8022' foo.txt me@server:/home/me/
```
Rsync does use your ssh config; however, so if you are connecting to this server frequently, you can add the following snippet to your _~/.ssh/config_ file. Then you no longer need to specify the port for the rsync or ssh commands!
```
Host server
Port 8022
```
Alternatively, if every server you connect to runs on the same non-standard port, you can configure the _RSYNC_RSH_ environment variable.
### Why else should you switch to rsync?
Now that weve covered the everyday use cases and caveats for switching from scp to rsync, lets take some time to explore why you probably want to use rsync on its own merits. Many people have made the switch to rsync long before now on these merits alone.
#### In-flight compression
If you have a slow or otherwise limited network connection between you and your server, rsync can spend more CPU cycles to save network bandwidth. It does this by compressing data before sending it. Compression can be enabled with the -z flag.
#### Delta transfers
Rsync also only copies a file if the target file is different than the source file. This works recursively through directories. For instance, if you took our final bar example above and re-ran that rsync command multiple times, it would do no work after the initial transfer. Using rsync even for local copies is worth it if you know you will repeat them, such as backing up to a USB drive, for this feature alone as it can save a lot of time with large data sets.
#### Syncing
As the name implies, rsync can do more than just copy data. So far, weve only demonstrated how to copy files with rsync. If you instead want rsync to make the target directory look like your source directory, you can add the delete flag to rsync. The delete flag makes it so rsync will copy files from the source directory which dont exist on the target directory. Then it will remove files on the target directory which do not exist in the source directory. The result is the target directory is identical to the source directory. By contrast, scp will only ever add files to the target directory.
### Conclusion
For simple use cases, rsync is not significantly more complicated than the venerable scp tool. The only significant difference being the use of -a instead of -r for recursive copying of directories. However, as we saw rsyncs -a flag behaves more like cps -r flag than scps -r flag does.
Hopefully, with these new commands, you can speed up your file transfer workflow!
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/scp-users-migration-guide-to-rsync/
作者:[chasinglogic][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/chasinglogic/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/07/scp-rsync-816x345.png
[2]: https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html

View File

@ -0,0 +1,182 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (SCP users migration guide to rsync)
[#]: via: (https://fedoramagazine.org/scp-users-migration-guide-to-rsync/)
[#]: author: (chasinglogic https://fedoramagazine.org/author/chasinglogic/)
scp 用户的 rsync 迁移指南
======
![][1]
在 [SSH 8.0 预发布公告][2]中OpenSSH 项目表示,他们认为 scp 协议已经过时,不灵活,而且不容易修复,然后他们继续推荐使用 `sftp``rsync` 来进行文件传输。
然而,很多用户都是从小用着 `scp` 命令长大的,所以对 `rsync` 并不熟悉。此外,`rsync` 可以做的事情也远不止复制文件,这可能会给菜鸟们留下复杂和不透明的印象。尤其是,`scp` 命令的标志大体上可以直接对应到 `cp` 命令的标志,而 `rsync` 命令的标志却和它大相径庭。
本文将为熟悉 `scp` 的人提供一个介绍和过渡的指南。让我们跳进最常见的场景:复制文件和复制目录。
### 复制文件
对于复制单个文件而言,`scp` 和 `rsync` 命令实际上是等价的。比方说,你需要把 `foo.txt` 传到一个名为 `server` 的服务器上你的主目录下:
```
$ scp foo.txt me@server:/home/me/
```
相应的 `rsync` 命令只需要输入 `rsync` 取代 `scp`
```
$ rsync foo.txt me@server:/home/me/
```
### 复制目录
对于复制目录,就确实有了很大的分歧,这也解释了为什么 `rsync` 会被认为比 `scp` 更复杂。如果你想把 `bar` 目录复制到 `server` 服务器上,除了指定 `ssh` 信息外,相应的 `scp` 命令和 `cp` 命令一模一样。
```
$ scp -r bar/ me@server:/home/me/
```
对于 `rsync`,考虑的因素比较多,因为它是一个比较强大的工具。首先,我们来看一下最简单的形式:
```
$ rsync -r bar/ me@server:/home/me/
```
看起来很简单吧?对于只包含目录和普通文件的简单情况,这就可以了。然而,`rsync` 更在意发送与主机系统中一模一样的文件。让我们来创建一个稍微复杂一些,但并不罕见的例子:
```
# 创建多级目录结构
$ mkdir -p bar/baz
# 在其根目录下创建文件
$ touch bar/foo.txt
# 现在创建一个符号链接指回到该文件
$ cd bar/baz
$ ln -s ../foo.txt link.txt
# 返回原位置
$ cd -
```
现在我们有了一个如下的目录树:
```
bar
├── baz
│ └── link.txt -> ../foo.txt
└── foo.txt
1 directory, 2 files
```
如果我们尝试上面的命令来复制 `bar`,我们会注意到非常不同的(并令人惊讶的)结果。首先,我们来试试 `scp`
```
$ scp -r bar/ me@server:/home/me/
```
如果你 `ssh` 进入你的服务器,看看 `bar` 的目录树,你会发现它和你的主机系统有一个重要而微妙的区别:
```
bar
├── baz
│ └── link.txt
└── foo.txt
1 directory, 2 files
```
请注意,`link.txt` 不再是一个符号链接,它现在是一个完整的 `foo.txt` 副本。如果你习惯于使用 `cp`,这可能会是令人惊讶的行为。如果你尝试使用 `cp -r` 复制 `bar` 目录,你会得到一个新的目录,里面的符号链接和 `bar` 的一样。现在如果我们尝试使用之前的 `rsync` 命令,我们会得到一个警告:
```
$ rsync -r bar/ me@server:/home/me/
skipping non-regular file "bar/baz/link.txt"
```
`rsync` 警告我们它发现了一个非常规的文件,并正在跳过它。因为你没有告诉它可以复制符号链接,所以它忽略了它们。`rsync` 在手册中有一节“符号链接”,解释了所有可能的行为选项。在我们的例子中,我们需要添加 `-links` 标志:
```
$ rsync -r --links bar/ me@server:/home/me/
```
在远程服务器上,我们看到这个符号链接是作为一个符号链接复制过来的。请注意,这与 `scp` 复制符号链接的方式不同。
```
bar/
├── baz
│ └── link.txt -> ../foo.txt
└── foo.txt
1 directory, 2 files
```
为了省去一些打字工作,并利用更多的文件保护选项,在复制目录时可以使用 `-archive`(简称 `-a`)标志。该归档标志将做大多数人所期望的事情,因为它可以实现递归复制、符号链接复制和许多其他选项。
```
$ rsync -a bar/ me@server:/home/me/
```
如果你感兴趣的话,`rsync` 手册页有关于存档标志的深入解释。
### 注意事项
不过,使用 `rsync` 有一个注意事项。使用 `scp` 比使用 `rsync` 更容易指定一个非标准的 ssh 端口。例如,如果 `server` 使用 8022 端口的 SSH 连接,那么这些命令就会像这样:
```
$ scp -P 8022 foo.txt me@server:/home/me/
```
而在使用 `rsync` 时,你必须指定要使用的“远程 shell”命令默认是 `ssh`。你可以使用 `-e` 标志来指定。
```
$ rsync -e 'ssh -p 8022' foo.txt me@server:/home/me/
```
`rsync` 会使用你的 `ssh` 配置;但是,如果你经常连接到这个服务器,你可以在你的 `~/.ssh/config` 文件中添加以下代码。这样你就不需要再为 `rsync``ssh` 命令指定端口了!
```
Host server
Port 8022
```
另外,如果你连接的每一台服务器都在同一个非标准端口上运行,你还可以配置 `RSYNC_RSH` 环境变量。
### 为什么你还是应该切换到 rsync
现在我们已经介绍了从 `scp` 切换到 `rsync` 的日常使用案例和注意事项,让我们花一些时间来探讨一下为什么你可能想要使用 `rsync` 的优点。很多人在很久以前就已经开始使用 `rsync` 了,就是因为这些优点。
#### 即时压缩
如果你和服务器之间的网络连接速度较慢或有限,`rsync` 可以花费更多的 CPU 处理能力来节省网络带宽。它通过在发送数据之前对数据进行即时压缩来实现。压缩可以用 `-z` 标志来启用。
#### 差量传输
`rsync` 也只在目标文件与源文件不同的情况下复制文件。这可以在目录中递归地工作。例如,如果你拿我们上面的最后一个 `bar` 的例子,并多次重新运行那个 `rsync` 命令,那么在最初的传输之后就不会有任何传输。如果你知道你会重复使用这些命令,例如备份到 U 盘,那么使用 `rsync` 即使是进行本地复制也是值得的,因为这个功能可以节省处理大型数据集的大量的时间。
#### 同步
顾名思义,`rsync` 可以做的不仅仅是复制数据。到目前为止,我们只演示了如何使用 `rsync` 复制文件。如果你想让 `rsync` 把目标目录变成源目录的样子,你可以在 `rsync` 中添加 `-delete` 标志。这个删除标志使得 `rsync` 将从源目录中复制不存在于目标目录中的文件,然后它将删除目标目录中不存在于源目录中的文件。结果就是目标目录和源目录完全一样。相比之下,`scp` 只会在目标目录下添加文件。
### 结论
对于简单的使用情况,`rsync` 并不比老牌的 `scp` 工具复杂多少。唯一显著的区别是在递归复制目录时使用 `-a` 而不是 `-r`。然而,正如我们看到的,`rsync` 的 `-a` 标志比 `scp``-r` 标志更像 `cp``-r` 标志。
希望通过这些新命令,你可以加快你的文件传输工作流程。
--------------------------------------------------------------------------------
via: https://fedoramagazine.org/scp-users-migration-guide-to-rsync/
作者:[chasinglogic][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://fedoramagazine.org/author/chasinglogic/
[b]: https://github.com/lujun9972
[1]: https://fedoramagazine.org/wp-content/uploads/2020/07/scp-rsync-816x345.png
[2]: https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html