Overview

Rsync is a Linux command-line utility for efficiently synchronizing files and directories between two locations, either locally or over a network. It’s particularly popular for backup and mirroring purposes due to its ability to copy only the differences between source and destination files.

Installation

This utility is pre-installed on most linux distribution and macOS. But if you don’t have rsync installed on your system, you use the following to install.

Install Rsync on Ubuntu and Debian

sudo apt install rsync

Install Rsync on CentOS and Fedora

sudo yum install rsync

Basic Syntax

Here’s a basic syntax of how rsync is used:

Local to Local:

rsync [OPTION] source destination

Local to Remote:

rsync [OPTION] $source user@HOST:destination

Remote to Local:

rsync [OPTION] user@HOST:source destination
  • source: This is the file or directory you want to synchronize from.
  • destination: This is the location where you want to synchronize the files to.

Some common parameters/options include:

  • -a, --archive: This is used for archive mode. This option tells rsync to syncs directories recursively, which preserves symbolic links, permissions, ownerships, timestamps, etc.
  • -v, --verbose: This option increases verbosity, providing more detailed output during synchronization. -c, --checksum: Skip based on checksum, not mod-time & size
  • -z, --compress: Compresses file data during transfer, reducing the amount of data sent over the network. Use this option only if the connection to the remote machine is slow.
  • -P, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.
  • -r, --recursive: Recursively copies files and directories. -q, --quiet: Use this option if you want to suppress non-error messages.
  • -n, --dry-run: Performs a trial run with no changes made; useful for testing and verifying what rsync would do.
  • --delete: This option deletes files at the destination that are not present at the source, ensuring the destination is an exact mirror of the source.
  • -e. This option allows you to choose a different remote shell. By default, rsync is configured to use ssh.

How to use

  • Copy single file to another directory:
rsync -a /opt/filename.zip /tmp/
  • Copy single file to another directory with a new name
rsync -a /opt/filename.zip /tmp/newfilename.zip
  • Copy directory to directory
rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/

Example

In this scenario, I’ll sync public_html directory between remote to local Webservers over SSH.

Instead of pushing to the remote server, I’ll use pulling method to sync the directory from remote to local server.

rsync -chavzP -e "ssh -p 9999 -i pat/to/sshkey" [email protected]:/var/www/html/public_html /var/www/html/public_html
  • 9999: is my SSH port.
  • pat/to/sshkey: is location of my SSH private key.
  • [email protected] is the user on the remote server. This is used to SSH login into remote server.

Crontab

In order to keep the directory synced, we need to run that rsync command on schedule. We can utilize crontab to create the schedule.

crontab is bassicaly a command used in Unix-like operating systems to schedule tasks or commands to run periodically at fixed times, dates, or intervals. These scheduled tasks are referred to as cron jobs.Cron jobs are often used for automated maintenance, backups, updates, and other routine tasks.

The crontab command is used to manipulate the cron table, which is a file containing the list of cron jobs scheduled for execution. Each user on the system can have their own crontab file.

  • To modify the cronjob, use the following
sudo crontab -e
  • To sync the public_html directory between remote to local Webservers over SSH every 5 minutes, I add the following on crontab
*/5 * * * * rsync -chavzP -e "ssh -p 9999 -i pat/to/sshkey" [email protected]:/var/www/html/public_html /var/www/html/public_html