Loading...

Tech Deep Dive

Synchronized backups with rsync

Data protection is a top priority, and backups play a key strategic role.

Remote backups

Table of contents

  • What is rsync and why use it for remote backups
  • How rsync works: differential synchronization
  • rsync syntax and main options
  • How to back up to a remote folder
  • Cloud backup with rsync

However, creating backups manually or using inefficient methods can lead to errors, delays, and data loss.

That’s where rsync comes in—a powerful and flexible tool perfect for performing synchronized backups both locally and remotely.

In this article, we’ll explore what rsync is, how it works, how to install it, and how to use it effectively, even for Cloud storage.

What is rsync and why use it for remote backups

rsync is an open-source utility designed for Unix-like operating systems (such as Linux, macOS, and BSD), and it is also available for Windows through environments like WSL (Windows Subsystem for Linux) or Cygwin.

Its main purpose is to synchronize files and directories efficiently between two locations—either local or remote.

How rsync works: differential synchronization

The power of rsync lies in its delta-transfer algorithm. Instead of copying entire files, rsync only transfers the parts that have changed, which makes it extremely fast and bandwidth-efficient.

It also preserves file metadata, such as permissions, timestamps, symbolic links, and ownership information.

Why rsync is essential for cyber security

In cyber security, having a robust and up-to-date backup strategy is critical. Poor or incomplete backups are a common cause of data loss in the event of ransomware, human error, or system failure. rsync helps to mitigate these risks thanks to:

  • Security
    It can use SSH encryption to ensure secure file transfers.
  • Reliability
    Proven and stable, widely used in production environments.
  • Automation
    Easy to script and schedule with cron jobs.
  • Scalability
    Usable on local disks, company servers, or cloud instances.

Practical example: remote backup over SSH

rsync -azv /home/user/data/ user@remoteserver.com:/home/backup/data/

Explanation:

  • -a: archive mode (preserves structure and metadata)
  • -z: compresses data for faster transfer
  • -v: verbose output
  • user@remoteserver.com: SSH connection to remote server

The system will prompt for the SSH password unless SSH key authentication is set up—a highly recommended step for security and automation.

Example in a script for automation:

#!/bin/bash

SOURCE="/home/user/projects/"

DEST="user@remoteserver.com:/backup/projects/"

LOG="/var/log/rsync_backup.log"

rsync -azv --delete "$SOURCE" "$DEST" >> "$LOG" 2>&1

The –delete option removes files from the destination that no longer exist in the source, maintaining a perfectly mirrored backup.

Requirements to build a backup system with rsync

To build a reliable synchronized backup system using rsync, several key elements must be in place. In professional or enterprise environments, these prerequisites ensure security, efficiency, and data continuity.

1. Root or sudo access

Some directories, especially system-level ones, require administrative permissions. Ensure you have root access or are allowed to use sudo.

Example:

sudo rsync -az /etc/ /mnt/backup/etc/

This copies the system configuration directory using elevated privileges.

2. Target server with rsync installed

If the destination is local, it can be a second disk or mounted network drive. If remote, the destination server must have rsync installed.

Check installation:

rsync --version

On a remote Ubuntu server:

ssh user@remoteserver "which rsync || sudo apt install rsync"

3. Active SSH connection (for remote backups)

rsync uses SSH to transfer data securely over the network. The remote host must accept SSH connections, and port 22 should be open unless customized.

Example:

rsync -az /home/user/data/ user@192.168.1.101:/home/backup/data/

4. Basic command line knowledge

rsync is a command-line tool, so you should be comfortable with shell commands like:

  • cd, ls, mkdir, rm, nano, vim
  • chmod, chown, scp, ssh
  • Redirection (>, >>) and piping (|)

Simple example:

mkdir -p /mnt/backup/logs/

rsync -av /var/log/ /mnt/backup/logs/

5. Clear backup policy

A good backup strategy includes:

  • What to back up (user data, system config, databases?)
  • When to run it (daily, weekly?)
  • Where to store it (local drive, NAS, remote server, Cloud?)

Sample strategy:

ItemDestinationFrequency
/home/user/mnt/backup/home/Daily
/etc/mnt/backup/system/Weekly
/var/lib/mysqlRemote server via SSHEvery 12h

6. Secure and up-to-date destination server

For remote backups, the receiving server should:

  • Be fully updated and secured
  • Limit access to authorized users only
  • Have a properly configured firewall
  • Use SSH key-based authentication

Setting up SSH keys:

On the client:

ssh-keygen -t rsa -b 4096

ssh-copy-id user@remoteserver

This enables password-less rsync sessions, essential for automated backup scripts.

How to install rsync

On many Linux systems, rsync comes pre-installed. If not, installation is straightforward.

On Debian/Ubuntu:

sudo apt update

sudo apt install rsync

On CentOS/RHEL/Fedora:

sudo dnf install rsync

On Arch Linux:

sudo pacman -S rsync

On macOS:

brew install rsync

On Windows:

Use rsync via WSL (Windows Subsystem for Linux) or Cygwin. WSL offers better compatibility:

wsl sudo apt install rsync

 Subsystem

rsync syntax and main options

Basic syntax for rsync:

rsync [options] source destination

Key options include:

  • -a: archive mode (preserves permissions, timestamps, etc.)
  • -v: verbose (shows detailed output)
  • -z: compresses data during transfer
  • -p: preserves file permissions
  • -h: human-readable numbers (e.g. 1K, 234M)
  • –progress: shows transfer status

Full example:

rsync -avzh --progress /home/user/data/ backup@192.168.1.100:/mnt/backup/

This command performs a remote backup of /home/user/data/ with compression, preserving permissions, and displaying progress.

  • How to back up to a local folder
    Creating a local backup with rsync is the best way to start using the tool. It’s straightforward, safe, and lets you get familiar with key options without needing remote access or SSH configuration.

Use case: copying the documenti folder

Let’s say you want to back up /home/user/documenti to a second drive mounted at /mnt/backup.

The command would be:

rsync -avh /home/user/documenti/ /mnt/backup/documenti/

What this command does:

  • -a: archive mode, copies recursively and preserves permissions, ownership, symbolic links, and timestamps
  • -v: verbose, lists all copied files
  • -h: human-readable, displays file sizes in an easy-to-read format (e.g., 1.4K, 22M)

Final slash behavior

  • With trailing slash /: copies only the contents of the directory
  • Without slash: includes the directory itself

With slash:

rsync -avh /home/user/documenti/ /mnt/backup/documenti/

Result: the contents of documenti are copied into /mnt/backup/documenti/

Without slash:

rsync -avh /home/user/documenti /mnt/backup/

Result: the documenti folder itself is placed inside /mnt/backup/

Incremental backup: only modified files

A core feature of rsync is that it only copies modified or new files on subsequent runs.

Example:

First backup:

rsync -avh /home/user/documenti/ /mnt/backup/documenti/

After editing or adding files:

rsync -avh /home/user/documenti/ /mnt/backup/documenti/

Only changed files will be transferred—saving time and disk I/O.

Additional tips

Log the output:
rsync -avh /home/user/documenti/ /mnt/backup/documenti/ >> /var/log/rsync_backup.log 2>&1

Exclude temporary/cache files:
rsync -avh –exclude ‘*.tmp’ /home/user/documenti/ /mnt/backup/documenti/

Dry-run to preview changes:
rsync -avh –dry-run /home/user/documenti/ /mnt/backup/documenti/

How to back up to a remote folder

The true power of rsync lies in its ability to handle remote backups securely and efficiently—ideal for replicating data to external backup servers or remote storage for disaster recovery.

Basic remote backup via SSH

rsync -azp /var/www/ user@remote.server:/home/backup/www/

Option breakdown:

  • -a: archive mode, preserves structure, permissions, timestamps, and symbolic links
  • -z: compression, reduces data size during transfer
  • -p: permissions, retains original access settings
  • user@remote.server: SSH-based connection to the remote host

This command synchronizes the contents of /var/www/ to /home/backup/www/ on the remote server.

Final slash behavior

  • /var/www/: copies only the contents of the folder
  • /var/www: copies the folder itself and its contents

SSH key-based authentication

To automate backups without entering a password, use SSH keys.

1. Generate a key on the client:

ssh-keygen -t rsa -b 4096

2. Copy the key to the server:

ssh-copy-id user@remote.server

Now, rsync can connect securely without prompting for a password:

rsync -azp /var/www/ user@remote.server:/home/backup/www/

Full example in a bash script

#!/bin/bash

SOURCE="/var/www/"

DEST="user@remote.server:/home/backup/www/"

LOG="/var/log/rsync_remote.log"

rsync -azp --delete "$SOURCE" "$DEST" >> "$LOG" 2>&1

Schedule this script with cron to run daily at 2 AM:

0 2 * * * /path/to/script.sh

Additional tips

  • Use –progress to see real-time transfer status
  • Use –exclude to skip temporary/cache files

To change the SSH port, add -e “ssh -p PORT”:

rsync -azp -e “ssh -p 2222” /var/www/ user@remote.server:/home/backup/www/

How to schedule a backup with rsync

You can schedule rsync using cron, the job scheduler on Linux.

To run a backup every day at 2 AM:

crontab -e

Then add:

0 2 * * * rsync -azp /data/ user@192.168.1.100:/backup/data/

You can also create a bash script:

#!/bin/bash

rsync -azp /data/ user@192.168.1.100:/backup/data/ >> /var/log/rsync.log 2>&1

And schedule that script using cron.

Cloud backup with rsync

You can use rsync with Cloud storage as long as you have SSH or FTP/SFTP access. Many VPS providers and cloud platforms like AWS, Azure, or Google Cloud support SSH.

Example with an AWS EC2 instance:

rsync -azp -e "ssh -i ~/.ssh/aws-key.pem" /home/user/data/ ec2-user@aws-instance:/mnt/cloud_backup/

For services like Dropbox, Google Drive, or OneDrive, tools like rclone simulate remote file systems and can be used in combination with rsync.


Questions and answers

  1. Does rsync work on Windows?
    Yes, via WSL or Cygwin.
  2. Can rsync do incremental backups?
    Yes, it transfers only file differences.
  3. Does rsync use SSH?
    Yes, by default for remote transfers.
  4. Can I sync two local disks with rsync?
    Yes, absolutely.
  5. Can rsync be used with Cloud?
    Yes, if SSH access is available or with tools like rclone.
  6. What does -avz mean?
    Archive mode, verbose output, and compression.
  7. Is rsync secure?
    Yes, especially when used with SSH.
  8. Can I schedule rsync with cron?
    Yes, it’s commonly done.
  9. Can I exclude files in rsync?
    Yes, with –exclude.
  10. Where are rsync logs saved?
    You can redirect them using >> rsync.log.
To top