How to Use Rsync to Sync Local and Remote Directories

Learn how to use Rsync to sync local and remote directories efficiently with our step-by-step tutorial covering basics and advanced options.

Introduction

Rsync, which stands for “remote sync,” is a utility for efficiently transferring and synchronizing files between a local computer and a remote host, or between two remote hosts. It’s fast and versatile, making it a popular choice for backups and mirroring files.

In this tutorial, we will cover the basics of using rsync to sync files between local and remote directories. We will explore different options and scenarios to help you take full advantage of rsync’s capabilities.

Prerequisites

To follow along with this tutorial, you will need:

  • Access to a Unix-based system (Linux or macOS)
  • A remote server (if you are syncing files with a remote machine)
  • Basic knowledge of the command line

Step 1 — Installing Rsync

Rsync is typically installed by default on most Unix-based systems. However, if it’s not already installed, you can easily install it using the package manager of your distribution.

For Ubuntu/Debian:

sudo apt update
sudo apt install rsync

For CentOS/RHEL:

sudo yum install rsync

For macOS (using Homebrew):

brew install rsync

Step 2 — Basic Rsync Usage

The basic syntax of rsync is as follows:

rsync [options] source destination

Syncing Local Directories

To sync two local directories, use:

rsync -avh /path/to/source/ /path/to/destination/
  • -a: Archive mode, which preserves permissions, times, symbolic links, and other attributes.
  • -v: Verbose, providing detailed output of the process.
  • -h: Human-readable, making the output easier to read.

Example

Let’s say you have a directory ~/project that you want to sync with ~/backup.

rsync -avh ~/project/ ~/backup/

Step 3 — Syncing with a Remote Server

To sync files with a remote server, the syntax is slightly different. Rsync uses SSH for secure data transfer.

rsync -avh -e ssh /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

Example

Suppose you want to sync the local directory ~/project with a remote directory ~/backup on a server with the IP address 192.168.1.10 and the username user.

rsync -avh -e ssh ~/project/ user@192.168.1.10:/home/user/backup/

Step 4 — Advanced Options

Deleting Files

To delete files on the destination that no longer exist on the source, use the --delete option:

rsync -avh --delete /path/to/source/ /path/to/destination/

Excluding Files

To exclude specific files or directories from being synced, use the --exclude option:

rsync -avh --exclude 'file_or_dir' /path/to/source/ /path/to/destination/

Bandwidth Limiting

If you need to limit the bandwidth used by rsync, use the --bwlimit option:

rsync -avh --bwlimit=1000 /path/to/source/ /path/to/destination/

This limits the bandwidth to 1000 KB/s.

Conclusion

Rsync is a powerful tool for synchronizing files and directories, both locally and remotely. By mastering the basic and advanced options of rsync, you can efficiently manage your backups and file transfers.

Feel free to explore more options in the rsync manual and customize the commands to suit your specific needs.

Happy syncing!