Skip to content
advanced Phase 82 · HA Advanced

Backup Strategies

Backup strategies including database backups, file backups, backup verification, and backup automation

45m
0 problems
Topic Progress 0%

Database Backups

Backup Types

Type          | Description              | Use Case
──────────────|──────────────────────────|─────────────
Full          | Complete database        | Weekly backup
Incremental   | Changes since last       | Daily backup
Differential  | Changes since full       | Daily backup
Snapshot       | Point-in-time copy      | Continuous

MySQL Backup Commands

# Full backup
mysqldump -u root -p --single-transaction \
  --routines --triggers --events \
  magento > magento_full_$(date +%Y%m%d).sql

# Compressed backup
mysqldump -u root -p --single-transaction \
  --routines --triggers --events \
  magento | gzip > magento_$(date +%Y%m%d).sql.gz

# Backup to S3
mysqldump -u root -p --single-transaction magento | \
  gzip | aws s3 cp - s3://backups/mysql/magento_$(date +%Y%m%d).sql.gz

Backup Scheduling

# crontab entries
# Full backup every Sunday at 2 AM
0 2 * * 0 /usr/local/bin/mysql-backup.sh full

# Incremental backup daily at 2 AM
0 2 * * 1-6 /usr/local/bin/mysql-backup.sh incremental

# Cleanup old backups
0 3 * * * /usr/local/bin/cleanup-backups.sh 30

File Backups

Magento File Backup

# Backup codebase
tar -czf /backup/magento_code_$(date +%Y%m%d).tar.gz \
  --exclude=var --exclude=pub/static \
  /var/www/magento

# Backup media
tar -czf /backup/magento_media_$(date +%Y%m%d).tar.gz \
  /var/www/magento/pub/media

# Backup configuration
tar -czf /backup/magento_config_$(date +%Y%m%d).tar.gz \
  /var/www/magento/app/etc

Automated File Backup

#!/bin/bash
# backup_files.sh

BACKUP_DIR="/backup/magento"
DATE=$(date +%Y%m%d_%H%M%S)
S3_BUCKET="s3://backups/magento"

# Create backup
mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/files_$DATE.tar.gz \
  --exclude=var --exclude=pub/static/_cache \
  /var/www/magento

# Upload to S3
aws s3 cp $BACKUP_DIR/files_$DATE.tar.gz $S3_BUCKET/

# Cleanup local (keep 7 days)
find $BACKUP_DIR -name "files_*.tar.gz" -mtime +7 -delete

Incremental File Backup

# Using rsync for incremental backups
rsync -av --delete \
  --link-dest=/backup/magento/latest \
  /var/www/magento/ \
  /backup/magento/incremental_$(date +%Y%m%d)/

# Update latest symlink
ln -s /backup/magento/incremental_$(date +%Y%m%d) \
  /backup/magento/latest

Backup Verification

Verification Procedures

#!/bin/bash
# verify_backup.sh

BACKUP_FILE=$1

# 1. Check file integrity
echo "=== Checking file integrity ==="
if ! gzip -t $BACKUP_FILE; then
    echo "FAIL: Corrupt backup file"
    exit 1
fi

# 2. Test restore to temporary database
echo "=== Testing restore ==="
mysql -u root -p -e "CREATE DATABASE magento_test;"
zcat $BACKUP_FILE | mysql -u root -p magento_test

# 3. Verify table count
echo "=== Verifying tables ==="
TABLE_COUNT=$(mysql -u root -p -N -e \
  "SELECT COUNT(*) FROM information_schema.tables \
  WHERE table_schema='magento_test';")
echo "Tables restored: $TABLE_COUNT"

# 4. Verify data integrity
echo "=== Checking data ==="
PRODUCT_COUNT=$(mysql -u root -p -N -e \
  "SELECT COUNT(*) FROM magento_test.catalog_product_entity;")
echo "Products: $PRODUCT_COUNT"

# 5. Cleanup
mysql -u root -p -e "DROP DATABASE magento_test;"

echo "=== Verification complete ==="

Verification Schedule

Check Type      | Frequency | Method
────────────────|───────────|──────────────
File integrity  | Daily     | gzip -t
Restore test    | Weekly    | Restore to test
Data validation | Weekly    | Count records
Full DR test    | Monthly   | Complete restore

Backup Automation

Backup Pipeline

1. Trigger: Cron schedule or manual
2. Backup: MySQL dump + file archive
3. Compress: gzip or zstd
4. Encrypt: AES-256
5. Upload: S3 with versioning
6. Verify: Integrity check
7. Notify: Success/failure alert
8. Cleanup: Remove old local backups

Encrypted Backup

# Backup with encryption
mysqldump -u root -p magento | \
  gzip | \
  openssl enc -aes-256-cbc -salt \
  -pass pass:$BACKUP_KEY \
  -out /backup/magento_encrypted_$(date +%Y%m%d).sql.gz.enc

# Restore encrypted backup
openssl enc -aes-256-cbc -d \
  -pass pass:$BACKUP_KEY \
  -in /backup/magento_encrypted_20260101.sql.gz.enc | \
  gunzip | mysql -u root -p magento

Backup Monitoring

# Alert on backup failure
if ! /usr/local/bin/mysql-backup.sh full; then
    echo "Backup failed at $(date)" | \
      mail -s "ALERT: Backup Failed" ops@example.com
fi

# Monitor backup age
LATEST=$(ls -t /backup/mysql/*.sql.gz | head -1)
AGE=$(( ($(date +%s) - $(stat -c %Y $LATEST)) / 3600 ))
if [ $AGE -gt 25 ]; then
    echo "WARNING: Latest backup is ${AGE}h old"
fi

Quiz

1. What flag ensures consistent MySQL backups?

Question 1 options

2. How often should backup restoration tests be performed?

Question 2 options

3. What is the benefit of encrypted backups?

Question 3 options

Flashcards

Question

MySQL backup flag for consistency?

Answer

--single-transaction for InnoDB consistent snapshot

Question

Backup verification frequency?

Answer

Daily integrity, weekly restore test, monthly full DR test

Question

Backup encryption benefit?

Answer

Protects sensitive data from unauthorized access

Question

Backup pipeline steps?

Answer

Trigger → Backup → Compress → Encrypt → Upload → Verify → Notify

Revision Notes

Key Takeaways

  • 1. Use --single-transaction for consistent MySQL backups
  • 2. Automate backup with cron and upload to S3
  • 3. Verify backups weekly with restoration tests
  • 4. Encrypt backups for security compliance
  • 5. Monitor backup age and alert on failures

Interview Tips

  • Explain backup types and when to use each
  • Discuss backup verification and testing strategies
  • Describe backup automation and encryption approaches

Cheat Sheet

Backup Strategies:
  MySQL: mysqldump --single-transaction
  Files: tar + gzip + S3
  Incremental: rsync with --link-dest

Verification:
  Daily: gzip -t integrity
  Weekly: Restore to test DB
  Monthly: Full DR test

Automation:
  Cron scheduling
  Encrypt with AES-256
  Upload to S3
  Monitor backup age
  Alert on failure