This document outlines the strategy for database backups, including cross-region replication for disaster recovery.
Our backup strategy is designed to be resilient and to ensure business continuity in the event of a regional outage. It involves:
- Regular Backups: Automated daily backups of the primary database.
- Geo-Redundancy: Replication of backups to a secondary geographical region.
- Encryption: Backups are encrypted both in transit and at rest.
- Integrity Checks: Regular verification of backup integrity.
- Retention Policies: A defined lifecycle for backup storage and deletion.
- Restore Testing: Periodic testing of the restore process.
-
Backup Creation: A scheduled job runs the
replicate-backup.shscript daily. This script performs a database dump, compresses it, and calculates a SHA256 checksum for integrity. -
Primary Storage: The compressed backup and its checksum file are uploaded to the primary S3 bucket located in the
us-east-1region. -
Cross-Region Replication: S3 is configured to automatically replicate all new objects from the primary backup bucket to a secondary bucket in the
us-west-2region. This provides a geo-redundant copy of our backups.
The backup infrastructure is managed using Terraform. The relevant configuration can be found in infrastructure/terraform/cross-region-backup.tf. This file defines:
- The secondary S3 bucket for replicated backups.
- IAM roles and policies required for S3 replication.
- The replication configuration itself.
- In Transit: All communication with AWS S3 uses SSL/TLS, encrypting the backups while they are being uploaded.
- At Rest: The S3 buckets are configured with server-side encryption (SSE-S3), which encrypts the backup files once they are stored.
For each backup, a SHA256 checksum is generated and stored alongside the backup file in S3. During a restore process, this checksum can be used to verify that the backup file has not been corrupted.
S3 Lifecycle Policies are used to manage the retention of backups:
- Standard-IA: Backups are stored in the
STANDARD_INFREQUENT_ACCESSstorage class to reduce costs. - Glacier: After 90 days, backups are moved to
GLACIERfor long-term archival. - Deletion: Backups are permanently deleted after 365 days.
These policies are defined in the Terraform configuration for the S3 buckets.
To ensure our backups are reliable, we perform regular restore tests. This process is automated and involves:
- Fetching a Backup: A recent backup is downloaded from the secondary S3 bucket.
- Restoring to a Test Environment: The backup is restored to a temporary, isolated database instance.
- Verification: Automated tests are run against the restored database to ensure data integrity and consistency.
- Cleanup: The temporary database and its resources are destroyed after the test is complete.
This process is managed by a CI/CD pipeline, which is triggered on a weekly basis.
In the event of a failure in the primary region (us-east-1), the following steps should be taken to restore service from a backup:
- Initiate Recovery: The on-call engineer will declare a disaster and begin the recovery process.
- Provision New Infrastructure: A new set of infrastructure will be provisioned in the secondary region (
us-west-2) using Terraform. - Restore Database: The latest backup will be retrieved from the
us-west-2S3 bucket and restored to the new database instance. - Update DNS: DNS records will be updated to point to the new application endpoints in the secondary region.
- Verify Service: The application will be tested to ensure it is fully functional.