How to use Rsync to sync local and remote directories?

Rsync is a powerful tool for syncing files and directories between local and remote systems. It is a popular choice for backup and mirroring tasks, as it can efficiently transfer large amounts of data while preserving file permissions, ownership, and timestamps. In this article, we’ll explore how to use Rsync to sync local and remote directories, with practical examples to help you get started.

Prerequisites

Before we get started, make sure you have the following:

  • A local machine running Linux or macOS
  • A remote machine running Linux or macOS
  • Rsync installed on both machines (it’s usually pre-installed on Linux systems)

Syncing Local Directories

To sync a local directory with another local directory, use the following command:

rsync -av /path/to/source/directory/ /path/to/destination/directory/

Here’s what the options mean:

  • -a: archive mode, which preserves permissions, ownership, and timestamps
  • -v: verbose mode, which displays detailed output
  • /path/to/source/directory/: the path to the source directory you want to sync
  • /path/to/destination/directory/: the path to the destination directory you want to sync to

For example, let’s say you have a local directory called mydata that you want to sync with a backup directory called mydata_backup. You can run the following command:

rsync -av /home/user/mydata/ /home/user/mydata_backup/

This will copy all the files and directories from mydata to mydata_backup, preserving their permissions, ownership, and timestamps.

Syncing Local and Remote Directories

To sync a local directory with a remote directory, use the following command:

rsync -av /path/to/local/directory/ user@remote:/path/to/remote/directory/

Here’s what the options mean:

  • -a: archive mode, which preserves permissions, ownership, and timestamps
  • -v: verbose mode, which displays detailed output
  • /path/to/local/directory/: the path to the local directory you want to sync
  • user@remote:/path/to/remote/directory/: the remote machine’s username, hostname or IP address, and the path to the remote directory you want to sync to

For example, let’s say you have a local directory called mydata that you want to sync with a remote directory called mydata_backup on a machine with the IP address 192.168.1.100. You can run the following command:

rsync -av /home/user/mydata/ user@192.168.1.100:/home/user/mydata_backup/

This will copy all the files and directories from mydata to mydata_backup on the remote machine, preserving their permissions, ownership, and timestamps.

Syncing Remote Directories

To sync a remote directory with another remote directory, use the following command:

rsync -av user1@remote1:/path/to/source/directory/ user2@remote2:/path/to/destination/directory/

Here’s what the options mean:

  • -a: archive mode, which preserves permissions, ownership, and timestamps
  • -v: verbose mode, which displays detailed output
  • user1@remote1:/path/to/source/directory/: the username, hostname or IP address, and the path to the source directory on the remote machine
  • user2@remote2:/path/to/destination/directory/: the username