Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - SCP user’s migration guide to rsync

#1
SCP user’s migration guide to rsync

<div><p>As part of the<a href="https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html"> 8.0 pre-release announcement,</a> 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.</p>
<p>Many users grew up on the <em>scp </em>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 it’s complicated and opaque. Especially when broadly the scp flags map directly to the cp flags while the rsync flags do not.</p>
<p>This article will provide an introduction and transition guide for anyone familiar with scp. Let’s jump into the most common scenarios: Copying Files and Copying Directories.</p>
<h2>Copying files</h2>
<p>For copying a single file, the scp and rsync commands are effectively equivalent. Let’s say you need to ship <em>foo.txt</em> to your home directory on a server named <em>server.</em></p>
<pre class="wp-block-preformatted">$ scp foo.txt me@server:/home/me/</pre>
<p>The equivalent rsync command requires only that you type rsync instead of scp:</p>
<pre class="wp-block-preformatted">$ rsync foo.txt me@server:/home/me/</pre>
<h2>Copying directories</h2>
<p>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 <em>bar </em>to <em>server</em> the corresponding scp command looks exactly like the cp command except for specifying ssh information:</p>
<pre class="wp-block-preformatted">$ scp -r bar/ me@server:/home/me/</pre>
<p>With rsync, there are more considerations, as it’s a more powerful tool. First, let’s look at the simplest form:</p>
<pre class="wp-block-preformatted">$ rsync -r bar/ me@server:/home/me/</pre>
<p>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. Let’s create a slightly more complex, but not uncommon, example.</p>
<pre class="wp-block-preformatted"># 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 -</pre>
<p>We now have a directory tree that looks like the following:</p>
<pre class="wp-block-preformatted">bar
├── baz
│&nbsp;&nbsp; └── link.txt -&gt; ../foo.txt
└── foo.txt 1 directory, 2 files</pre>
<p>If we try the commands from above to copy bar, we’ll notice very different (and surprising) results. First, let’s give scp a go:</p>
<pre class="wp-block-preformatted">$ scp -r bar/ me@server:/home/me/</pre>
<p>If you ssh into your server and look at the directory tree of bar you’ll notice an important and subtle difference from your host system:</p>
<pre class="wp-block-preformatted">bar
├── baz
│&nbsp;&nbsp; └── link.txt
└── foo.txt 1 directory, 2 files</pre>
<p>Note that <em>link.txt</em> is no longer a symlink. It is now a full-blown copy of <em>foo.txt</em>. This might be surprising behavior if you’re used to <em>cp</em>. If you did try to copy the <em>bar</em> directory using <em>cp -r</em>, you would get a new directory with the exact symlinks that <em>bar</em> had. Now if we try the same rsync command from before we’ll get a warning:</p>
<pre class="wp-block-preformatted">$ rsync -r bar/ me@server:/home/me/
skipping non-regular file "bar/baz/link.txt"</pre>
<p>Rsync has warned us that it found a non-regular file and is skipping it. Because you didn’t tell it to copy symlinks, it’s 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.</p>
<pre class="wp-block-preformatted">$ rsync -r --links bar/ me@server:/home/me/</pre>
<p>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.</p>
<pre class="wp-block-preformatted">bar/
├── baz
│&nbsp;&nbsp; └── link.txt -&gt; ../foo.txt
└── foo.txt 1 directory, 2 files</pre>
<p>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.</p>
<pre class="wp-block-preformatted">$ rsync -a bar/ me@server:/home/me/</pre>
<p>The rsync man page has in-depth explanations of what the archive flag enables if you’re curious.</p>
<h2>Caveats</h2>
<p>There is one caveat, however, to using rsync. It’s much easier to specify a non-standard ssh port with scp than with rsync. If <em>server </em>was using port 8022 SSH connections, for instance, then those commands would look like this:</p>
<pre class="wp-block-preformatted">$ scp -P 8022 foo.txt me@server:/home/me/</pre>
<p>With rsync, you have to specify the “remote shell” command to use. This defaults to <em>ssh</em>. You do so using the<em> </em>-e flag.</p>
<pre class="wp-block-preformatted">$ rsync -e 'ssh -p 8022' foo.txt me@server:/home/me/</pre>
<p>Rsync does use your ssh config; however, so if you are connecting to this server frequently, you can add the following snippet to your <em>~/.ssh/config</em> file. Then you no longer need to specify the port for the rsync or ssh commands!</p>
<pre class="wp-block-preformatted">Host server Port 8022</pre>
<p>Alternatively, if every server you connect to runs on the same non-standard port, you can configure the <em>RSYNC_RSH</em> environment variable.</p>
<h2>Why else should you switch to rsync?</h2>
<p>Now that we’ve covered the everyday use cases and caveats for switching from scp to rsync, let’s 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.</p>
<h3>In-flight compression</h3>
<p>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.</p>
<h3>Delta transfers</h3>
<p>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.</p>
<h3>Syncing</h3>
<p>As the name implies, rsync can do more than just copy data. So far, we’ve 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 don’t 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.</p>
<h2>Conclusion</h2>
<p>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 rsync’s -a flag behaves more like cp’s -r flag than scp’s -r flag does.</p>
<p>Hopefully, with these new commands, you can speed up your file transfer workflow!</p>
</div>


https://www.sickgaming.net/blog/2020/07/...-to-rsync/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016