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 the username@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 to cp and scp.
  • -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 to scp.
  • 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
2
3
4
5
rsync \
-e 'ssh -i SSH-key.pem' \
-r -v -z \
TypeWriter_dataset/ \
ubuntu@104.171.203.254:/home/ubuntu/TypeWriter_dataset

Conversely, to sync the remote directory /home/ubuntu/TypeWriter_dataset to the local directory TypeWriter_dataset:

1
2
3
4
5
rsync \
-e 'ssh -i SSH-key.pem' \
-r -v -z \
ubuntu@104.171.203.254:/home/ubuntu/TypeWriter_dataset/ \
TypeWriter_dataset

References:

  • https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories
  • https://en.wikipedia.org/wiki/Rsync

Syncing a Local Directory With a Remote Directory via rsync
https://abbaswu.github.io/2023/07/11/Syncing-a-Local-Directory-With-a-Remote-Directory-via-rsync/
Author
Jifeng Wu
Posted on
July 11, 2023
Licensed under