20. Backup & Disaster Recovery
Because containers are ephemeral, backing up the stateless application container itself is unnecessary (you just pull the image again). However, backing up the persistent state—your databases and user-uploaded files residing in Docker Volumes—is absolutely critical to surviving a catastrophic server failure.
20.1 Database Backup
Section titled “20.1 Database Backup”You should rarely back up a database by simply copying its raw physical files off the hard drive, as this can lead to corrupted backups if the database is actively writing data. Instead, use the database’s native dump utilities.
PostgreSQL Backup
Section titled “PostgreSQL Backup”You can use docker exec to trigger the pg_dump utility inside the running database container, securely piping the output to a file on your host machine.
# Dump the entire database to a file named backup.sql on the hostdocker exec -t my-postgres pg_dump -U myuser -d mydb > backup.sql
# Compress it to save massive amounts of spacedocker exec -t my-postgres pg_dump -U myuser -d mydb | gzip > backup.sql.gzRestoring PostgreSQL: To restore the data into a fresh container, pipe the SQL file back in.
cat backup.sql | docker exec -i my-postgres psql -U myuser -d mydbMySQL Backup
Section titled “MySQL Backup”Similarly, MySQL uses the mysqldump utility.
docker exec -t my-mysql mysqldump -u root -p'my_password' mydb > backup.sqlRestoring MySQL:
cat backup.sql | docker exec -i my-mysql mysql -u root -p'my_password' mydb20.2 Volume Backup Architecture
Section titled “20.2 Volume Backup Architecture”For backing up raw files (like a directory of user-uploaded images stored in a named volume), you must back up the Docker Volume directly.
The Flow
Section titled “The Flow” Running Container (Detached) ↓ Shared Named Volume (Data resides here) ↓ Temporary Backup Container (Mounts the volume) ↓ Tar/Gzip Archive (Creates a compressed backup file on the Host) ↓ External Storage (S3 / Offsite storage)Backing Up a Volume
Section titled “Backing Up a Volume”Because the volume is managed by Docker, the safest way to back it up is to spin up a tiny, temporary Linux container (like ubuntu or alpine), mount the volume to it, and compress the contents.
# Back up a named volume 'app_uploads' to the current host directorydocker run --rm \ -v app_uploads:/volume-data \ -v $(pwd):/backup-dest \ alpine tar cvzf /backup-dest/uploads_backup.tar.gz -C /volume-data .Restoring a Volume
Section titled “Restoring a Volume”To restore, reverse the process. Create a fresh named volume, and extract the archive into it.
docker run --rm \ -v app_new_uploads:/volume-data \ -v $(pwd):/backup-dest \ alpine tar xvzf /backup-dest/uploads_backup.tar.gz -C /volume-data20.3 Disaster Recovery (DR)
Section titled “20.3 Disaster Recovery (DR)”Creating a backup is only step one. A true Disaster Recovery plan ensures your business survives when the entire data center catches fire.
Backup Strategy (The 3-2-1 Rule)
Section titled “Backup Strategy (The 3-2-1 Rule)”Never store backups exclusively on the same server that runs the application. If the server dies, both the application and the backup are lost.
- 3 Copies of Data: The live data + 2 backups.
- 2 Different Media: E.g., SSD storage and Cloud Object Storage.
- 1 Offsite Copy: Push your compressed
.tar.gzand.sqldumps to an offsite cloud provider (like AWS S3 or Backblaze B2) using an automated cron job.
Restore Testing
Section titled “Restore Testing”A backup is completely useless if it is corrupted and cannot be restored. You must periodically perform “Fire Drills.” Spin up a fresh Docker stack on a local machine or testing server, pull the latest backup from S3, and verify that the application successfully boots and reads the restored data.
RPO (Recovery Point Objective)
Section titled “RPO (Recovery Point Objective)”“How much data are you willing to lose?”
- If your cron job backs up the database every 24 hours (at midnight), and the server crashes at 11:59 PM, you just lost 24 hours of customer data. Your RPO is 24 hours. For critical applications, backups must happen more frequently (e.g., hourly, or using continuous replication).
RTO (Recovery Time Objective)
Section titled “RTO (Recovery Time Objective)”“How fast can you bring the system back online?”
- Because Docker utilizes Infrastructure as Code (
compose.yaml), your RTO is incredibly fast. If a server dies, you can provision a new VPS, rungit clone, executedocker compose up, download the database backup from S3, and restore it. What used to take days of manual server configuration now takes minutes.