Automated Multi-Server Incremental Backups with Remote Replication and Alerting for Production Databases

In modern production environments, database availability and disaster recovery are critical. A single-server backup is often insufficient due to hardware failure, ransomware attacks, or accidental deletion.

This guide demonstrates how to implement automated incremental backups across multiple servers, with:

This setup is ideal for production MySQL, MariaDB, or PostgreSQL databases across multiple servers or data centers.


Real-Life Scenario

Scenario:

Solution: Multi-server incremental backups with remote replication and alerting.


Step 1: Prerequisites


Step 2: Configure SSH Keys for Passwordless Access

Generate SSH key on primary backup server:

ssh-keygen -t ed25519 -f ~/.ssh/id_rsa_backup -N ""

Copy key to remote backup server:

ssh-copy-id -i ~/.ssh/id_rsa_backup user@remote-backup-server

Why: Allows automated backups via cron without manual password entry.


Step 3: Prepare Backup Directories

Local backup directories:

mkdir -p /backup/db/full
mkdir -p /backup/db/incremental

Remote backup directories:

ssh user@remote-backup-server "mkdir -p /remote-backups/db/full /remote-backups/db/incremental"

Step 4: Create Incremental Backup Script

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

#!/bin/bash

# Database credentials
DB_USER="root"
DB_PASS="YourStrongPassword"

# Directories
LOCAL_FULL="/backup/db/full"
LOCAL_INC="/backup/db/incremental"
REMOTE_USER="user"
REMOTE_HOST="remote-backup-server"
REMOTE_DIR="/remote-backups/db"

DATE=$(date +%F_%H-%M)

# Step 1: Full backup weekly (Sunday)
DAY=$(date +%u)
if [ "$DAY" -eq 7 ]; then
    mysqldump -u $DB_USER -p$DB_PASS --all-databases > $LOCAL_FULL/db_full_$DATE.sql
    rsync -avz $LOCAL_FULL/db_full_$DATE.sql $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/full/
else
    # Incremental backup
    mkdir -p $LOCAL_INC/$DATE
    rsync -av --link-dest=$LOCAL_FULL $LOCAL_FULL/ $LOCAL_INC/$DATE/
    rsync -avz $LOCAL_INC/$DATE/ $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/incremental/$DATE/
fi

# Step 2: Alert if backup fails
if [ $? -ne 0 ]; then
    echo "Backup failed on $(hostname) at $(date)" | mail -s "Backup Failure Alert" [email protected]
fi

Make it executable:

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

Explanation:


Step 5: Automate with Cron

Edit cron:

crontab -e

Add:

# Run backup daily at 2 AM
0 2 * * * /usr/local/bin/multi_server_db_backup.sh

Step 6: Slack Notifications (Optional)

Add Slack alert function:

send_slack_alert() {
    WEBHOOK="https://hooks.slack.com/services/XXXX/XXXX/XXXX"
    MESSAGE="$1"
    curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" $WEBHOOK
}

Call after backup failure:

send_slack_alert "Backup failed on $(hostname) at $(date)"

Step 7: Retention Policy

Automatically remove backups older than 30 days:

find /backup/db/full -type f -mtime +30 -delete
find /backup/db/incremental -type d -mtime +30 -exec rm -rf {} \;

ssh user@remote-backup-server "find /remote-backups/db/full -type f -mtime +30 -delete"
ssh user@remote-backup-server "find /remote-backups/db/incremental -type d -mtime +30 -exec rm -rf {} \;"

Step 8: Real-Life Flow

  1. Sunday → Full backup locally + remote

  2. Monday–Saturday → Incremental backup locally + remote

  3. Rsync ensures synchronized copies across servers

  4. Cron runs daily → backups automated

  5. Admin receives alerts if backup fails

  6. Older backups deleted automatically → efficient storage usage


Step 9: Restore Procedure

  1. Restore latest full backup from local or remote:

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

mysql -u root -p < /backup/db/incremental/2026-01-29/db_full_2026-01-28.sql
  1. Remote backups ensure disaster recovery if local server fails.


Step 10: Best Practices

gpg -c /backup/db/full/db_full_2026-01-28.sql

Step 11: Benefits


💡 Pro Tip: Integrate with object storage (S3, GCS, Azure Blob) for cloud-based redundancy. Combine with monitoring tools like Prometheus for proactive backup health alerts.