Sunday, April 5, 2026
LinuxBackup

How to Set Up Automated Incremental Backups with rsync and Cron on Linux

Automating Linux backups with rsync
Automating Linux backups with rsync
37views

Data loss is one of the most disruptive events a system administrator or Linux user can experience. Hard drives fail. Ransomware encrypts. Accidental rm -rf happens. Power failures corrupt filesystems at the worst possible moment. The only genuine protection against all of these scenarios is a backup system that actually runs, reliably, on a schedule, without requiring you to remember to do it manually.

The combination of rsync and cron is the foundation of practical Linux backup strategy. rsync is one of the most powerful and efficient file synchronization tools available on any operating system it copies only what has changed, preserves all file metadata, works locally and over SSH, and produces detailed logs of every operation. Cron is the Linux task scheduler that runs your backup script silently every day, every hour, or any interval you specify, regardless of whether you are at your keyboard.

This guide builds a complete, production-ready incremental backup system from the ground up. We start with the concepts what incremental backups are, how rsync works under the hood, and the difference between backup strategies.

Then we build progressively: a simple daily backup script, a 7-day rotating snapshot system with hard links, remote SSH backup, email notifications, and log rotation. Every script is annotated, every flag explained, and every decision justified. By the end, you will have an automated backup system that you trust because you understand exactly how it works.

Understanding Backup Strategies: Full, Incremental, and Differential

Before building a backup system, it helps to understand the three fundamental backup strategies and the trade-offs between them. Choosing the right strategy determines your storage requirements, your backup window, and how quickly you can recover from data loss.

StrategyStorage UseBackup SpeedRecovery SpeedBest For
FullHigh — copies everything each timeSlow — transfers all dataFast — single set to restoreInfrequent backups where fast recovery is critical
IncrementalLow — only changed files since last backupFast — minimal data transferredSlower — requires chain of setsDaily backups where storage efficiency is important
DifferentialMedium — changed files since last FULL backupMedium — grows over timeMedium — two sets to restoreWeekly full + daily differential for balanced approach
SnapshotVery Low — hard links for unchanged filesFast — only changed filesFast — each snapshot is completeDaily automated snapshots with retention periods (this guide)

rsync with the –link-dest option implements the snapshot strategy — the most storage-efficient approach that also provides the fastest recovery. Each snapshot directory appears to contain a complete copy of your data, but unchanged files are stored as hard links pointing to the same data blocks as the previous snapshot. The result: you get the browse-and-restore convenience of full backups at the storage cost of incremental backups.

Installing rsync

rsync is pre-installed on virtually all major Linux distributions. Verify it is available and check its version:

If it is not installed (rare on modern systems), install it using your distribution’s package manager:

  Step 1: Create the Basic Daily Backup Script

Building the Foundation Backup Script

Start with a clear, simple backup script that covers the most common use case: syncing a source directory to a local backup destination daily. We will progressively enhance this script in subsequent steps.

Create the script in /usr/local/bin/ where it is accessible system-wide and will be found in the PATH for any user:

Paste the following complete, annotated script:

Save the file (Ctrl+O, Enter, Ctrl+X in nano) and make it executable:

Step 2: Test the Script Before Scheduling It

Running and Verifying the Backup Script

Testing manually before automating with cron is essential. Silent failures in cron are hard to diagnose — a 5-minute manual test saves hours of troubleshooting later.

Dry Run First — See What Will Happen Without Making Changes

Always run rsync with –dry-run before the first real execution, especially when –delete is involved:

The –dry-run flag simulates the operation completely — it shows every file that would be transferred and every file that would be deleted — without actually making any changes. Review the output carefully before committing to the real run.

Run the Script and Check the Log

Execute the backup script as root (required for proper permission handling):

The script runs silently — all output goes to the log file. Check the log:

The log should show the backup start timestamp, a list of transferred files, rsync statistics, and a COMPLETED success message.

Verify the backup destination contains the expected files:

Run the script a second time to verify that unchanged files are skipped and only modified files are re-transferred:

The second run should show zero or very few files transferred — confirming that rsync’s incremental sync is working correctly and only detecting actual changes.

  Step 3: Automate with Cron — Daily Scheduled Backups

Scheduling the Backup with Cron

With the script tested and confirmed working, automate it using cron. Because backup scripts typically need root privileges to read all files and write to /backup/, use the root crontab:

Add the following line to run the backup daily at 2:00 AM. Running at an off-peak hour reduces the impact on system performance and avoids conflicts with peak-hour file access:

For a more robust entry that also captures cron execution errors (distinct from rsync errors already captured by the script):

Verify the cron job was added:

Step 4: 7-Day Rotating Snapshots with Hard Links (–link-dest)

Understanding Hard Link Snapshots

The basic backup script in Step 1 maintains a single backup destination that is always a mirror of the current source state. If you accidentally delete a file and the backup runs before you notice, the file disappears from the backup too. A rotating snapshot system solves this by maintaining multiple dated backup directories — each one appearing to be a full copy of your data, but using hard links to avoid storing duplicate file data.

How the Initial Snapshot Baseline Works

Before running the rotation script for the first time, create the initial baseline snapshot manually:

This initial run is the only full data transfer. All subsequent snapshots use –link-dest to reference daily.0 (after rotation, always the previous day’s snapshot), copying only changed files and hard-linking everything else.

The 7-Day Rotation Script

Create the rotating snapshot script:

Paste the following complete script:

Make the script executable and schedule it with cron:

Browsing and Restoring from Snapshots

After several days of rotation, the snapshot directory structure looks like this:

Each directory can be browsed like a normal file tree. To restore a specific file from 3 days ago:

Step 5: Back Up to a Remote Server Over SSH

Why Remote Backup Is Essential for Disaster Recovery

Local backups — even excellent rotating snapshots — are destroyed by the same events that destroy the original data: house fire, flood, theft, storage controller failure, or ransomware that encrypts all locally-connected drives. The 3-2-1 backup rule exists precisely for this reason: 3 copies of data, on 2 different types of storage, with 1 copy off-site. Remote SSH backup fulfills the off-site requirement.

Set Up SSH Key-Based Authentication (Required for Cron)

Cron jobs run without a TTY and cannot accept interactive password prompts. SSH key authentication is mandatory for automated remote rsync. Generate a dedicated SSH key pair for backup use:

Copy the public key to the remote backup server

Test the key-based connection (should not prompt for password)

Remote Backup Script

Create a remote backup script that uses SSH key authentication:

Step 6: Add Email Notifications for Backup Status Alerts

Getting Notified When Backups Succeed or Fail

An automated backup that fails silently is nearly as dangerous as having no backup at all. Adding email notifications means you are alerted immediately when a backup fails — not when you need to restore and discover the backup has been broken for three weeks.

The email notification can be added directly to either the basic or rotating backup script. Install the mail utility first:

# RHEL / CentOS / Rocky / Fedora

Add the following email notification block at the end of your backup script (before the final fi closing statement):

The failure notification pipes the log file content into the email body, giving you the full rsync output (including the error details) in the failure notification. This makes diagnosing the failure possible from your inbox without having to SSH to the server.

Conclusion

rsync combined with cron and a well-crafted rotation script gives you a backup system that is simultaneously powerful, storage-efficient, and completely transparent — no black-box agents, no proprietary formats, no vendor lock-in. Your backups are ordinary directories of ordinary files that can be browsed with ls, restored with cp, and verified with diff. Every component is a standard Linux tool that has been in production for decades.

The progression in this guide — from a simple daily sync to a 7-day rotating hard-link snapshot system with remote SSH backup, email notifications, and integrity verification — represents a complete, production-quality backup architecture that professionals deploy on critical systems. Start with Step 1 and Step 3 to get the basic daily backup running, then add the snapshot rotation in Step 4 for point-in-time recovery capability, and complete the 3-2-1 picture with the remote backup in Step 5.

Leave a Response

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock