Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to migrate scp users to rsync

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to carry out the rsync migration of scp users, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

In the SSH 8.0 pre-release announcement, the OpenSSH project said they thought the scp protocol was outdated, inflexible, and not easy to fix, and then recommended using sftp or rsync for file transfer.

However, many users grew up with scp commands, so they are not familiar with rsync. In addition, rsync can do much more than copy files, which may leave the impression that rookies are complex and difficult to master. In particular, the flag of the scp command can generally correspond directly to the flag of the cp command, while the flag of the rsync command is quite different from it.

Let's jump to the most common scenario: copy files and copy directories.

Copy a file

The scp and rsync commands are actually equivalent for copying a single file. For example, you need to send foo.txt to your home directory on a server called server:

$scp foo.txt me@server:/home/me/

The corresponding rsync command only needs to enter rsync instead of scp:

$rsync foo.txt me@server:/home/me/ copy directory

There is a great deal of disagreement about copying directories, which explains why rsync is considered more complex than scp. If you want to copy the bar directory to the server server, except for specifying ssh information, the corresponding scp command is exactly the same as the cp command.

$scp-r bar/ me@server:/home/me/

For rsync, there are many factors to consider because it is a more powerful tool. First, let's take a look at the simplest form:

$rsync-r bar/ me@server:/home/me/

Doesn't it look easy? For simple cases that contain only directories and ordinary files, this is fine. However, rsync is more concerned about sending exactly the same files as on the host system. Let's create a slightly more complex, but not uncommon, example:

# create a multi-level directory structure $mkdir-p bar/baz# create a file under its root $touch bar/foo.txt# now create a symbolic link to go back to the file $cd bar/baz$ ln-s.. / foo.txt link.txt# return to the original location $cd-

Now we have a directory tree like this:

Bar ├── baz │ └── link.txt->.. / foo.txt └── foo.txt 1 directory, 2 files

If we try the above command to copy bar, we will notice very different (and surprising) results. First, let's try scp:

$scp-r bar/ me@server:/home/me/

If you ssh access your server and look at bar's directory tree, you will find that there is an important and subtle difference between it and your host system:

Bar ├── baz │ └── link.txt └── foo.txt 1 directory, 2 files

Note that link.txt is no longer a symbolic link, it is now a complete copy of foo.txt. If you are used to using cp, this can be surprising behavior. If you try to copy the bar directory using cp-r, you will get a new directory with the same symbolic links as bar. Now if we try to use the previous rsync command, we will get a warning:

$rsync-r bar/ me@server:/home/me/skipping non-regular file "bar/baz/link.txt"

Rsync warns us that it has found an unusual file and is skipping it. Because you didn't tell it to copy symbolic links, it ignored them. Rsync has a section called "symbolic links" in the manual that explains all possible behavior options. In 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 symbolic link is copied as a symbolic link. Note that this is different from the way scp copies symbolic links.

Bar/ ├── baz │ └── link.txt->.. / foo.txt └── foo.txt 1 directory, 2 files

To save some typing and take advantage of more file protection options, you can use the archive flag-archive (- a) when copying directories. The archive flag will do what most people expect because it can implement recursive replication, symbolic link replication, and many other options.

$rsync-a bar/ me@server:/home/me/

If you are interested, the rsync manual page has an in-depth explanation of the archiving logo.

Matters needing attention

However, there is one caveat when using rsync. It is easier to specify a non-standard ssh port with scp than with rsync. For example, if server uses a SSH connection on port 8022, these commands would look like this:

$scp-P 8022 foo.txt me@server:/home/me/

When using rsync, you must specify the "remote shell" command to use, which is ssh by default. You can use the-e flag to specify.

$rsync-e'ssh-p 8022' foo.txt me@server:/home/me/

Rsync will use your ssh configuration; however, if you connect to this server frequently, you can add the following code to your ~ / .ssh/config file. This way you no longer need to specify ports for rsync or ssh commands!

Host server Port 8022

In addition, if each server you connect to runs on the same non-standard port, you can also configure the RSYNC_RSH environment variable.

Why should you still switch to rsync?

Now that we have introduced the daily use cases and considerations for switching from scp to rsync, let's take a moment to explore the advantages of why you might want to use rsync. Many people started using rsync a long time ago because of these advantages.

Instant compression

If the network connection between you and the server is slow or limited, rsync can spend more CPU processing power to save network bandwidth. It is achieved by compressing the data immediately before it is sent. Compression can be enabled with the-z flag.

Differential transmission

Rsync also copies files only if the target file is different from the source file. This works recursively in the directory. For example, if you take our last bar example above and rerun that rsync command multiple times, there will be no transmission after the initial transfer. If you know that you will reuse these commands, such as backing up to a USB drive, it is worthwhile to use rsync even for local replication, because this feature can save a lot of time dealing with large data sets.

Synchronization

As the name implies, rsync can do more than just copy data. So far, we have only demonstrated how to copy files using rsync. If you want rsync to make the destination directory look like a source directory, you can add the delete flag-delete to rsync. This delete flag causes rsync to copy files from the source directory that do not exist in the destination directory, and then it will delete files in the destination directory that do not exist in the source directory. The result is that the destination directory is exactly the same as the source directory. By contrast, scp only adds files in the target directory.

Conclusion

For simple usage, rsync is not much more complex than the established scp tools. The only significant difference is the use of-an instead of-r when recursively copying directories. However, as we can see, rsync's-a flag is more like cp's-r flag than scp's-r flag.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report