Syncing a Local Directory With a Remote Directory via rsync
Syncing a Local Directory With a Remote Directory via rsync
rsync
is a powerful utility for efficiently synchronizing directories between a local computer and a remote server. It achieves this synchronization by comparing file modification times and sizes, utilizing delta encoding, and optionally employing data compression to minimize network usage.
To successfully synchronize directories between two systems using rsync, you need to have rsync installed on both the local and remote machines. Additionally, the remote machine should be accessible from the local machine via SSH, enabling the local machine to invoke the remote machine's rsync and determine which parts of local files need to be transferred.
The rsync command-line syntax is similar to that of cp and scp. The following command-line options are commonly used:
-e
: Specifies the command to establish an SSH connection before theusername@domain
section. This option allows you to customize SSH behavior, such as using specific ports or providing a PEM file (e.g.,ssh -i SSH-key.pem
).-r
(recursive): Used for syncing directories, similar tocp
andscp
.-v
(verbose): Lists files being transferred during synchronization.-z
: Enables additional data compression for improved network usage.
When specifying the source directory and the destination directory, keep the following points in mind:
- A local directory can be represented using either a relative or absolute path.
- A remote directory is represented using the
username@domain:<absolute path on the remote machine>
notation, similar toscp
. - A source directory must end with a trailing slash.
- A destination directory must not end with a trailing slash.
Here's an example that illustrates how to sync the local directory TypeWriter_dataset
to the remote directory /home/ubuntu/TypeWriter_dataset
with additional data compression. This assumes connecting to ubuntu@104.171.203.254
via the command ssh -i SSH-key.pem
:
1 |
|
Conversely, to sync the remote directory /home/ubuntu/TypeWriter_dataset
to the local directory TypeWriter_dataset
:
1 |
|
References:
- https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories
- https://en.wikipedia.org/wiki/Rsync