Backups are like insurance; you don’t need them every day, but when disaster strikes, such as accidental file deletion, a disk failure, or a ransomware attack, it’s enough to ruin everything if you’re not prepared. That’s where smart backup planning comes in.

In this guide, I’ll show you how to schedule incremental backups using rsync and cron. I’ll explain what incremental backups are, how rsync works under the hood, and how to automate the whole process with cron.

What Is an Incremental Backup?

An incremental backup means you’re only backing up the files that have changed since the last backup. So instead of copying everything every time, which can be slow and take up a lot of space, you’re only saving the new or updated files.

Let’s say you have 10,000 files in a folder and only 20 of them changed today. An incremental backup will skip the 9,980 unchanged files and only back up the 20 that actually changed, which is efficient and perfect for daily backups.

Why Use rsync?

rsync is a powerful and reliable tool used for copying files and directories in Linux. What makes rsync special is its ability to sync only the differences between the source and destination.

It works locally (from one folder to another on the same system) or remotely (over SSH to another server). It also preserves file permissions, timestamps, symbolic links, and even supports deletion of removed files, which is fast, flexible, and already installed on most Linux distros.

If rsync not installed, you can get it with:

sudo apt install rsync       # Debian/Ubuntu
sudo yum install rsync       # CentOS/RHEL

Our Backup Plan

Let’s say you have some important files stored under /home/ravi/documents/, and you want to back them up to /backup/documents/. We’ll write a simple shell script that uses rsync to copy changed files to the backup directory. Then we’ll use cron to run this script every day at 2:00 AM.

Step 1: Write the Backup Script

First, let’s create a shell script to perform the backup.

sudo nano /usr/local/bin/rsync-backup.sh

Paste the following script into it:

#!/bin/bash

SOURCE="/home/ravi/documents/"
DEST="/backup/documents/"
LOGFILE="/var/log/rsync-backup.log"
DATE=$(date +"%Y-%m-%d %H:%M:%S")

rsync -av --delete "$SOURCE" "$DEST" >> "$LOGFILE" 2>&1
echo "Backup completed at $DATE" >> "$LOGFILE"

This script tells rsync to sync the files from the source directory to the destination. The -a flag tells it to run in archive mode, preserving permissions and metadata.

The -v makes the output verbose (so we can log what’s happening), and --delete removes files from the backup if they no longer exist in the source. All output is written to a log file at /var/log/rsync-backup.log so we can check later if anything went wrong.

Now make the script executable:

sudo chmod +x /usr/local/bin/rsync-backup.sh

Step 2: Schedule the Script with Cron

Next, we need to make sure the backup script runs automatically every day at 2:00 AM, so you need to edit your cron jobs, type:

crontab -e

Add this line at the bottom:

0 2 * * * /usr/local/bin/rsync-backup.sh

To confirm your cron job was added:

crontab -l

Step 3: Test the Backup Setup

Before you let the system run backups automatically, it’s important to test the script manually to make sure everything works as expected, which will help you catch any path issues, permission errors, or typos before cron runs it silently in the background.

Firt, run the backup script manually, which will trigger the backup process immediately and you’ll see a list of files being copied or skipped.

sudo /usr/local/bin/rsync-backup.sh

Once the script finishes, go to your backup directory and verify that the files were copied correctly:

ls -lh /backup/documents/

Now, check the log file to make sure the script ran without errors and logged the backup time:

cat /var/log/rsync-backup.log

You should see output similar to this:

sending incremental file list
./
file1.txt
folder2/
folder2/file2.pdf

Backup completed at 2025-06-16 14:00:01

This confirms that the script not only copied files but also recorded the event with a timestamp.

Step 4: Create Daily Snapshot Backups

If you want to go one step further and keep daily snapshots of your data (instead of just one backup folder), you can use the --link-dest option in rsync, which lets you make hard-linked backups basically creating new folders that look like full backups but only use space for files that changed.

On day one, create the initial full backup:

rsync -a /home/ravi/documents/ /backup/daily.0/

On the next day, use the previous day’s folder as a reference to create an incremental backup:

rsync -a --link-dest=/backup/daily.0/ /home/ravi/documents/ /backup/daily.1/

Files that haven’t changed will be hard-linked, saving space. You can even rotate these folders using a simple script that renames the old ones and creates a new snapshot every day.

Here’s a basic rotation script for 7 days:

#!/bin/bash

rm -rf /backup/daily.7
mv /backup/daily.6 /backup/daily.7
mv /backup/daily.5 /backup/daily.6
mv /backup/daily.4 /backup/daily.5
mv /backup/daily.3 /backup/daily.4
mv /backup/daily.2 /backup/daily.3
mv /backup/daily.1 /backup/daily.2
mv /backup/daily.0 /backup/daily.1

rsync -a --delete --link-dest=/backup/daily.1 /home/ravi/documents/ /backup/daily.0/

You can schedule this script using cron, just like the basic backup script. For example, to run it every day at 2:00 AM:

0 2 * * * /usr/local/bin/daily-rsync-rotate.sh

Bonus Tip: Back Up to a Remote Server

If you want to back up your data to another machine (like a backup server), you can use rsync over SSH, but make sure SSH keys are set up for passwordless login, then run something like this:

rsync -av -e ssh /home/ravi/documents/ ravi@backup-server:/backup/ravi/

You can add the above command to your script or make a separate script just for remote backups.

Final Thoughts

Backups might not feel exciting, but losing your data sure is. Once you set up incremental backups with rsync and cron, you can relax knowing your files are protected every single day.

Always test your backups, make sure your scripts work, and don’t forget to check the logs from time to time. If you ever need to restore something, you’ll be glad you had this system in place.

Share.
Leave A Reply