`n

Database Backup Automation

Implement comprehensive automated backup strategies for MySQL, PostgreSQL, and MongoDB. Learn scheduling, monitoring, and disaster recovery best practices.

Automated Backup Process

Schedule
→
Backup
→
Verify
→
Monitor

Published: September 25, 2024 | Reading time: 20 minutes

🤖 Backup Automation Benefits

Reliability: Eliminates human error and ensures consistent backups

Efficiency: Runs during off-peak hours without manual intervention

Compliance: Meets regulatory requirements for data protection

Backup Strategy Overview

📅 Full Backups

Complete database backup including all data and schema.
# MySQL Full Backup mysqldump --single-transaction \ --routines --triggers \ --all-databases > full_backup.sql # PostgreSQL Full Backup pg_dumpall -h localhost -U postgres > full_backup.sql

🔄 Incremental Backups

Backup only changes since last backup to save storage.
# MySQL Binary Log Backup mysqlbinlog --start-datetime="2024-01-01" \ --stop-datetime="2024-01-02" \ mysql-bin.000001 > incremental.sql # PostgreSQL WAL Backup pg_basebackup -D /backup/incremental \ -Ft -z -P -v

📊 Differential Backups

Backup changes since last full backup.
# MongoDB Differential Backup mongodump --db myapp \ --query '{"updated_at": {"$gte": ISODate("2024-01-01")}}' \ --out /backup/differential

Automated Backup Scheduling

📅 Backup Schedule Timeline

🌅

Daily Full Backups

Complete database backup every night at 2 AM

Retention: 7 days

🔄

Hourly Incremental Backups

Backup changes every hour during business hours

Retention: 24 hours

📅

Weekly Archive Backups

Compressed archive backup every Sunday

Retention: 4 weeks

🗓️

Monthly Long-term Backups

Offsite backup for disaster recovery

Retention: 12 months

MySQL Backup Automation

🐬 MySQL Automated Backup Setup

Complete MySQL backup automation with mysqldump and binary log backups.

🐬
MySQL Backup Script
#!/bin/bash # MySQL Automated Backup Script # Configuration DB_HOST="localhost" DB_USER="backup_user" DB_PASS="secure_password" BACKUP_DIR="/backups/mysql" DATE=$(date +%Y%m%d_%H%M%S) RETENTION_DAYS=7 # Create backup directory mkdir -p $BACKUP_DIR # Full database backup echo "Starting MySQL full backup..." mysqldump --host=$DB_HOST \ --user=$DB_USER \ --password=$DB_PASS \ --single-transaction \ --routines \ --triggers \ --events \ --all-databases | gzip > $BACKUP_DIR/mysql_full_$DATE.sql.gz # Verify backup if [ $? -eq 0 ]; then echo "MySQL backup completed successfully" # Clean old backups find $BACKUP_DIR -name "mysql_full_*.sql.gz" -mtime +$RETENTION_DAYS -delete # Send notification echo "Backup completed: mysql_full_$DATE.sql.gz" | mail -s "MySQL Backup Success" admin@company.com else echo "MySQL backup failed" echo "Backup failed at $(date)" | mail -s "MySQL Backup Failed" admin@company.com exit 1 fi

PostgreSQL Backup Automation

🐘 PostgreSQL Automated Backup Setup

PostgreSQL backup automation using pg_dump and WAL archiving.

🐘
PostgreSQL Backup Script
#!/bin/bash # PostgreSQL Automated Backup Script # Configuration DB_HOST="localhost" DB_USER="postgres" BACKUP_DIR="/backups/postgresql" DATE=$(date +%Y%m%d_%H%M%S) RETENTION_DAYS=7 # Create backup directory mkdir -p $BACKUP_DIR # Full database backup echo "Starting PostgreSQL full backup..." pg_dump -h $DB_HOST -U $DB_USER \ --verbose \ --clean \ --if-exists \ --create \ --format=custom \ --file=$BACKUP_DIR/postgresql_full_$DATE.backup # Verify backup if [ $? -eq 0 ]; then echo "PostgreSQL backup completed successfully" # Clean old backups find $BACKUP_DIR -name "postgresql_full_*.backup" -mtime +$RETENTION_DAYS -delete # Send notification echo "Backup completed: postgresql_full_$DATE.backup" | mail -s "PostgreSQL Backup Success" admin@company.com else echo "PostgreSQL backup failed" echo "Backup failed at $(date)" | mail -s "PostgreSQL Backup Failed" admin@company.com exit 1 fi

MongoDB Backup Automation

🍃 MongoDB Automated Backup Setup

MongoDB backup automation using mongodump with compression and verification.

🍃
MongoDB Backup Script
#!/bin/bash # MongoDB Automated Backup Script # Configuration DB_HOST="localhost:27017" DB_NAME="myapp" BACKUP_DIR="/backups/mongodb" DATE=$(date +%Y%m%d_%H%M%S) RETENTION_DAYS=7 # Create backup directory mkdir -p $BACKUP_DIR # Full database backup echo "Starting MongoDB full backup..." mongodump --host $DB_HOST \ --db $DB_NAME \ --out $BACKUP_DIR/mongodb_full_$DATE \ --gzip # Verify backup if [ $? -eq 0 ]; then echo "MongoDB backup completed successfully" # Compress backup directory tar -czf $BACKUP_DIR/mongodb_full_$DATE.tar.gz -C $BACKUP_DIR mongodb_full_$DATE rm -rf $BACKUP_DIR/mongodb_full_$DATE # Clean old backups find $BACKUP_DIR -name "mongodb_full_*.tar.gz" -mtime +$RETENTION_DAYS -delete # Send notification echo "Backup completed: mongodb_full_$DATE.tar.gz" | mail -s "MongoDB Backup Success" admin@company.com else echo "MongoDB backup failed" echo "Backup failed at $(date)" | mail -s "MongoDB Backup Failed" admin@company.com exit 1 fi

Cron Job Scheduling

⏰ Cron Job Configuration

Set up automated backup schedules using cron jobs for reliable execution.

⏰
Crontab Configuration
# Edit crontab crontab -e # Add backup schedules # Daily full backup at 2 AM 0 2 * * * /scripts/mysql_backup.sh >> /var/log/backup.log 2>&1 # Hourly incremental backup (9 AM to 5 PM) 0 9-17 * * * /scripts/mysql_incremental.sh >> /var/log/backup.log 2>&1 # Weekly archive backup on Sunday at 3 AM 0 3 * * 0 /scripts/mysql_archive.sh >> /var/log/backup.log 2>&1 # Monthly offsite backup on 1st of month at 4 AM 0 4 1 * * /scripts/mysql_offsite.sh >> /var/log/backup.log 2>&1 # PostgreSQL backup at 2:30 AM 30 2 * * * /scripts/postgresql_backup.sh >> /var/log/backup.log 2>&1 # MongoDB backup at 3:30 AM 30 3 * * * /scripts/mongodb_backup.sh >> /var/log/backup.log 2>&1

Backup Monitoring and Alerting

📊 Backup Monitoring Dashboard

Monitor backup status, success rates, and storage usage with automated alerts.

99.8%
Success Rate
2.3TB
Total Storage
15min
Avg Duration
24/7
Monitoring
📊
Monitoring Script
#!/bin/bash # Backup Monitoring Script BACKUP_DIR="/backups" LOG_FILE="/var/log/backup_monitor.log" ALERT_EMAIL="admin@company.com" # Check backup success check_backup_success() { local backup_type=$1 local expected_count=$2 actual_count=$(find $BACKUP_DIR -name "*${backup_type}*" -mtime -1 | wc -l) if [ $actual_count -lt $expected_count ]; then echo "ALERT: Missing ${backup_type} backups. Expected: $expected_count, Found: $actual_count" | tee -a $LOG_FILE echo "Missing ${backup_type} backups" | mail -s "Backup Alert" $ALERT_EMAIL fi } # Check disk space check_disk_space() { usage=$(df $BACKUP_DIR | awk 'NR==2 {print $5}' | sed 's/%//') if [ $usage -gt 85 ]; then echo "ALERT: Backup disk space usage is ${usage}%" | tee -a $LOG_FILE echo "Backup disk space critical: ${usage}%" | mail -s "Disk Space Alert" $ALERT_EMAIL fi } # Check backup integrity check_backup_integrity() { local backup_file=$1 if [[ $backup_file == *.gz ]]; then if ! gzip -t $backup_file; then echo "ALERT: Corrupted backup file: $backup_file" | tee -a $LOG_FILE echo "Corrupted backup: $backup_file" | mail -s "Backup Integrity Alert" $ALERT_EMAIL fi fi } # Run checks check_backup_success "mysql_full" 1 check_backup_success "postgresql_full" 1 check_backup_success "mongodb_full" 1 check_disk_space # Check recent backups find $BACKUP_DIR -name "*_$(date +%Y%m%d)*" -exec bash -c 'check_backup_integrity "$0"' {} \;

Disaster Recovery Planning

🚨 Disaster Recovery Procedures

Comprehensive disaster recovery plan for database restoration scenarios.

1

Immediate Response

Assess the scope of data loss and determine recovery point objective (RPO)

Activate disaster recovery team and notify stakeholders

2

Data Recovery

Restore from most recent successful backup

Apply incremental backups to reach desired recovery point

3

System Validation

Verify data integrity and application functionality

Run comprehensive tests to ensure system stability

4

Service Restoration

Gradually restore services to minimize impact

Monitor system performance and user experience

Backup Testing and Validation

✅ Backup Testing Checklist

1

Regular Backup Testing

Test backup restoration monthly

Verify data integrity after restoration

2

Recovery Time Testing

Measure actual recovery time (RTO)

Test different recovery scenarios

3

Cross-Platform Testing

Test restoration on different hardware

Verify compatibility across environments

4

Automation Testing

Test automated backup scripts

Verify monitoring and alerting systems

Backup Storage Strategies

Storage Strategy Comparison

Storage Type Cost Speed Reliability Scalability Best Use Case
Local Disk Low Fast Medium Limited Short-term backups
Network Storage Medium Medium High Good Shared backups
Cloud Storage Medium Medium Very High Unlimited Long-term archives
Tape Storage Low Slow Very High Good Compliance archives

Summary

Database backup automation is essential for data protection:

  • Automate backup scheduling to ensure consistency and reliability
  • Implement multiple backup types (full, incremental, differential)
  • Monitor backup success with automated alerting
  • Test restoration procedures regularly
  • Plan for disaster recovery scenarios

Need Backup Automation?

Our database experts can help you implement comprehensive backup automation strategies tailored to your specific requirements.

Get Backup Help