An article about setting up Ghost blogging says to use scp
to copy from my local machine to a remote server:
scp -r ghost-0.3 root@*your-server-ip*:~/
However, Railscast 339: Chef Solo Basics uses scp
to copy in the opposite direction (from the remote server to the local machine):
scp -r root@178.xxx.xxx.xxx:/var/chef .
In the same Railscast, when the author wants to copy files to the remote server (same direction as the first example), he uses rsync
:
rsync -r . root@178.xxx.xxx.xxx:/var/chef
Why use the rsync
command if scp
will copy in both directions? How does scp
differ from rsync
?
The major difference between these tools is how they copy files.
scp
basically reads the source file and writes it to the destination. It performs a plain linear copy, locally, or over a network.
rsync
also copies files locally or over a network. But it employs a special delta transfer algorithm and a few optimizations to make the operation a lot faster. Consider the call.
rsync A host:B
rsync
will check files sizes and modification timestamps of both A and B, and skip any further processing if they match.
If the destination file B already exists, the delta transfer algorithm will make sure only differences between A and B are sent over the wire.
rsync
will write data to a temporary file T, and then replace the destination file B with T to make the update look "atomic" to processes that might be using B.
Another difference between them concerns invocation. rsync
has a plethora of command line options, allowing the user to fine tune its behavior. It supports complex filter rules, runs in batch mode, daemon mode, etc. scp
has only a few switches.
In summary, use scp
for your day to day tasks. Commands that you type once in a while on your interactive shell. It's simpler to use, and in those cases rsync
optimizations won't help much.
For recurring tasks, like cron
jobs, use rsync
. As mentioned, on multiple invocations it will take advantage of data already transferred, performing very quickly and saving on resources. It is an excellent tool to keep two directories synchronized over a network.
Also, when dealing with large files, use rsync
with the -P
option. If the transfer is interrupted, you can resume it where it stopped by reissuing the command. See Sid Kshatriya's answer.
Finally, note that rsync://
the protocol is similar to plain HTTP: unencrypted and no integrity checks. Be sure to always use rsync
via SSH (as in the examples from the question above), not via the rsync protocol, unless you really know what you're doing. scp
will always use SSH as underlying transport mechanism which has both integrity and confidentiality guarantees, so that is another difference between the two utilities.