Database Backup and Recovery: Strategies for Data Safety
Data loss is not a matter of if, but when. Hardware failures, software bugs, human errors, malicious attacks, and natural disasters can all destroy data in an instant. A robust backup and recovery strategy is your safety net — without it, a single failure can mean permanent data loss, regulatory penalties, and irreparable damage to your business.
This guide covers backup types, recovery strategies, replication, and disaster recovery planning for production databases.
Backup Types
Full Backup
A full backup copies the entire database — all tables, indexes, schemas, and data. It is the most comprehensive backup type and forms the foundation of any backup strategy.
-- PostgreSQL
pg_dump -U postgres mydb > full_backup.sql
-- MySQL
mysqldump -u root mydb > full_backup.sql
-- MongoDB
mongodump --db mydb --out /backups/full/Pros: Complete snapshot, simplest to restore.
Cons: Large size, slow to create, resource-intensive.
Incremental Backup
An incremental backup copies only data that has changed since the last backup (full or incremental). This dramatically reduces backup time and storage requirements.
# Using rsync for incremental file-level backup
rsync -av --link-dest=/backups/full/ \
/var/lib/mysql/ /backups/incremental/$(date +%Y%m%d)/Pros: Fast, small footprint, low resource usage.
Cons: Complex restore (must replay all increments since last full backup), each increment is a single point of failure.
Differential Backup
A differential backup copies all data changed since the last full backup. Unlike incremental backups, each differential does not depend on previous differentials.
# MySQL using mysqlbackup
mysqlbackup --backup-dir=/backups \
--with-timestamp backup-and-apply-logPros: Faster restore than incremental (only need full + latest differential).
Cons: Grows larger over time (resets after each full backup).
Backup Strategies in Practice
The 3-2-1 Rule
The gold standard for backup safety:
- 3 copies of your data (1 production + 2 backups)
- 2 different media types (e.g., local disk + cloud storage)
- 1 copy offsite (different physical location or cloud region)
Recovery Point Objective (RPO) and Recovery Time Objective (RTO)
- RPO: Maximum acceptable data loss (e.g., 1 hour = hourly backups)
- RTO: Maximum acceptable downtime (e.g., 4 hours = must restore within 4 hours)
Your backup strategy must meet both objectives. A financial trading system might need RPO of seconds and RTO of minutes, while a blog might tolerate RPO of 24 hours and RTO of a few hours.
Point-in-Time Recovery (PITR)
PITR lets you restore a database to any moment in time, not just the last backup. It works by replaying write-ahead logs (WAL) or binary logs from a base backup to the target time.
-- PostgreSQL PITR
-- 1. Take a base backup
pg_basebackup -D /backups/base/ -X stream
-- 2. In postgresql.conf, enable archiving
archive_mode = on
archive_command = 'cp %p /backups/archives/%f'
-- 3. Restore to a specific time
-- In recovery.conf (or postgresql.conf for >= 12):
restore_command = 'cp /backups/archives/%f %p'
recovery_target_time = '2025-06-15 14:30:00 UTC'-- MySQL PITR using binary logs
-- Enable binary logging in my.cnf:
log_bin = mysql-bin
expire_logs_days = 7
-- Restore from backup + replay binlogs
mysql -u root < full_backup.sql
mysqlbinlog mysql-bin.000001 mysql-bin.000002 | mysql -u rootPITR requires continuous WAL or binlog archiving. The trade-off is storage space versus recovery granularity.
Replication for High Availability
Replication copies data to another server in real time. It is not a backup (logical errors replicate too), but it is essential for availability and disaster recovery.
Streaming Replication (PostgreSQL)
# Standby server setup
pg_basebackup -h primary -D /var/lib/postgresql/data -P -U replicatorGroup Replication (MySQL)
-- Configure each node
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='node2.example.com',
SOURCE_USER='repl',
SOURCE_PASSWORD='password'
FOR CHANNEL 'group_replication';Replica Sets (MongoDB)
rs.initiate({
_id: "myReplicaSet",
members: [
{ _id: 0, host: "primary:27017" },
{ _id: 1, host: "secondary1:27017" },
{ _id: 2, host: "secondary2:27017", arbiterOnly: true }
]
---);Disaster Recovery Planning
Recovery Playbook
Every backup strategy needs a documented, tested recovery procedure:
- Assess the damage — what failed, what data is affected, how recent is the last good backup
- Provision infrastructure — spin up replacement servers (bare metal, VM, or container)
- Restore the last full backup — this is your foundation
- Apply differential/incremental backups — in the correct order
- Replay transactions (PITR) — reach the target recovery point
- Verify data integrity — run checksums, test queries, validate row counts
- Redirect traffic — update DNS, load balancers, or connection strings
- Monitor — watch for replication lag, query errors, performance degradation
Testing Your Backups
“A backup is only as good as its last restore.”
Schedule regular restore drills:
- Monthly: restore to a staging environment and verify
- Quarterly: measure RTO and RPO against SLAs
- Annually: full disaster simulation (fail over to DR site)
Cloud Disaster Recovery Patterns
| Pattern | RTO | RPO | Cost | |
Backup Types and Strategies
Full Backups
A full backup copies the entire database. It is the foundation of any backup strategy — all other backup types depend on having a recent full backup. Full backups take the longest time and consume the most storage but are the simplest to restore. Schedule full backups during low-traffic windows.
Incremental Backups
Incremental backups capture only data changed since the last backup (full or incremental). They are faster and smaller than full backups but require all intervening incremental backups for restoration — losing one incremental backup makes all subsequent increments unusable.
Differential Backups
Differential backups capture data changed since the last full backup. They are larger than incremental backups but restore faster — you only need the last full backup plus the latest differential. Many organizations use a combination: weekly full backups with daily differentials and hourly transaction log backups.
Point-in-Time Recovery (PITR)
PITR allows restoring a database to any state between the last full backup and the present by replaying transaction logs. This is critical for recovering from accidental data deletion or corruption that was not immediately detected:
-- PostgreSQL PITR
pg_restore --dbname=mydb --recovery-target-time="2024-03-15 14:30:00" backup.dump
-- SQL Server restore to point in time
RESTORE LOG database_name FROM backup_device
WITH STOPAT = '2024-03-15 14:30:00';Backup Validation
A backup that cannot be restored is worthless. Regularly validate backups by restoring them to a test environment and running integrity checks. Automate backup validation in your recovery testing schedule — at minimum, test full restores quarterly and point-in-time restores monthly.
Cloud Backup Strategies
Cloud databases offer managed backup features: AWS RDS automated backups with 35-day retention, Azure SQL point-in-time restore, and GCP Cloud SQL automatic backups. For self-managed databases in the cloud, use cloud storage (S3, Blob Storage, GCS) for backup destinations with cross-region replication for disaster recovery protection. Implement lifecycle policies to tier older backups to cheaper storage classes.
FAQ
What is the difference between SQL and NoSQL? SQL databases use structured schemas with tables, relationships, and ACID transactions. NoSQL databases offer flexible schemas, horizontal scaling, and various data models (document, key-value, graph, column-family). Choose based on data structure and access patterns.
When should I use an index? Index queries filtering on the indexed column(s), particularly for WHERE clauses, JOINs, and ORDER BY. Avoid over-indexing — each index slows writes and consumes storage. Monitor slow queries to identify indexing opportunities.
What is database normalization? Normalization organizes data to reduce redundancy and improve integrity. First Normal Form (1NF) eliminates duplicate columns. Second Normal Form (2NF) removes partial dependencies. Third Normal Form (3NF) removes transitive dependencies.
How do I choose between PostgreSQL and MySQL? PostgreSQL offers more advanced features (JSONB, full-text search, custom types, better concurrency). MySQL is simpler to configure and widely available. Both are excellent choices; PostgreSQL is often preferred for complex applications.
What is a deadlock in databases? A deadlock occurs when two transactions each hold locks the other needs. The database detects deadlocks and kills one transaction automatically. Reduce deadlocks by accessing tables in the same order across transactions and keeping transactions short.
———|—–|—–|——| | Backup & Restore | Hours | 24h | Low | | Pilot Light | Minutes | Minutes | Medium | | Warm Standby | Minutes | Seconds | High | | Multi-Site Active | Seconds | Near-zero | Very High |
Automation and Monitoring
Automate backups to eliminate human error:
#!/bin/bash
# Automated PostgreSQL backup script
BACKUP_DIR="/backups/$(date +%Y-%m-%d)"
mkdir -p "$BACKUP_DIR"
pg_dump -U postgres -Fc mydb > "$BACKUP_DIR/mydb.dump"
# Validate
pg_restore -l "$BACKUP_DIR/mydb.dump" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Backup validated successfully"
else
echo "Backup validation FAILED" | mail -s "Backup failure" admin@example.com
fi
# Rotate old backups (keep 30 days)
find /backups/ -type d -mtime +30 -exec rm -rf {} \;Monitor backups with alerts for failures, slow performance, and storage capacity.
Conclusion
A solid backup and recovery strategy is the most important investment you can make in data safety. Follow the 3-2-1 rule, automate everything, document your recovery procedures, and — most importantly — test your restores regularly. When disaster strikes, you will be glad you did.
Related: Check our Database Indexing Guide.
For a comprehensive overview, read our article on Acid Transactions Guide.