To ensure data is preserved and recoverable in case of corruption, accidental deletion, or catastrophic failure.
- Always. For every production database.
- Scripting: Write a script (bash/python) to run
pg_dump(Postgres) ormysqldump.pg_dump -h $HOST -U $USER $DB | gzip > backup.sql.gz.
- Upload: Stream or upload the file to offsite storage (AWS S3, Google Cloud Storage).
aws s3 cp backup.sql.gz s3://my-backups/$(date +%F)/.
- Scheduling: Add to crontab:
0 2 * * * /path/to/backup.sh(Runs at 2 AM). - Retention Policy: Configure S3 Lifecycle Rules to delete backups older than X days.
- Verification: Crucial. Regularly attempt to restore a backup to a test DB to ensure it works.
- Encrypt backups at rest.
- Do not store backups on the same server as the database.
- Monitor the backup job (alert if it fails).
A reliable, automated system that produces daily/hourly backups stored securely offsite.