Implementing Automated Database Backups with Incremental Snapshots Using rsync and Cron on Linux

Databases are the backbone of almost every IT system, and backup strategies are crucial to prevent data loss. While full backups are standard, they can be time-consuming and storage-intensive for large databases.

This guide explains how to implement automated incremental backups for MySQL/MariaDB/PostgreSQL using rsync and cron. Incremental backups save time, bandwidth, and storage, making it ideal for production environments.


Real-Life Scenario

Scenario:

Solution: Incremental backups with rsync and automated cron jobs.


Step 1: Prerequisites

sudo apt install rsync -y        # Debian/Ubuntu
sudo dnf install rsync -y        # RHEL/CentOS/AlmaLinux

Step 2: Prepare Backup Directories

Create directories for full and incremental backups:

sudo mkdir -p /backup/db/full
sudo mkdir -p /backup/db/incremental
sudo chown -R $(whoami):$(whoami) /backup/db

Step 3: Take Initial Full Backup

For MySQL/MariaDB:

mysqldump -u root -p --all-databases > /backup/db/full/db_full_$(date +%F).sql

For PostgreSQL:

sudo -u postgres pg_dumpall > /backup/db/full/db_full_$(date +%F).sql

Explanation:


Step 4: Create Incremental Backup Script

Create /usr/local/bin/db_incremental_backup.sh:

#!/bin/bash

# Source and destination directories
SRC="/backup/db/full/"
DEST="/backup/db/incremental/"
DATE=$(date +%F_%H-%M)

# Create destination directory
mkdir -p $DEST/$DATE

# Rsync incremental backup
rsync -av --link-dest=$SRC $SRC $DEST/$DATE

# Optional: Keep only last 7 incremental backups
cd /backup/db/incremental
ls -1tr | head -n -7 | xargs -d '\n' rm -rf --

Make it executable:

chmod +x /usr/local/bin/db_incremental_backup.sh

Explanation:


Step 5: Schedule Automated Backups with Cron

Edit crontab:

crontab -e

Add:

# Full backup weekly on Sunday at 2:00 AM
0 2 * * 0 mysqldump -u root -pYourPassword --all-databases > /backup/db/full/db_full_$(date +\%F).sql

# Incremental backup daily at 2:00 AM (except Sunday)
0 2 * * 1-6 /usr/local/bin/db_incremental_backup.sh

Explanation:


Step 6: Real-Life Example


Step 7: Restore from Incremental Backup

  1. Restore the full backup:

mysql -u root -p < /backup/db/full/db_full_2026-01-28.sql
  1. Apply incremental backups in chronological order:

mysql -u root -p < /backup/db/incremental/2026-01-29/db_full_2026-01-28.sql

Repeat for each day to reach the latest state.


Step 8: Best Practices

gpg -c /backup/db/full/db_full_$(date +%F).sql
rsync -avz /backup/db/ user@backup-server:/backups/db/

Benefits


💡 Pro Tip: Combine this with ZFS snapshots or LVM snapshots for near-instant backups with minimal downtime, especially on large-scale production servers.