Resources · Security guides · Security guide
A backup you have not restored is a hope
Back up the database consistently, encrypt and store copies off the server, and prove the backup works by restoring it on a schedule.
Check command
restic snapshots --latest 3
Copied.
Backups fail quietly: the dump job has been erroring for weeks, the copy sits on the same disk that died, or the archive is fine but nobody knows the restore steps. Ransomware and a deleted droplet are the two events where you find out. Plan for both.
1. Dump the database consistently
Copying live database files gives you a corrupt backup. Use the database's own dump tool:
# PostgreSQL: custom format, compressed, consistent snapshot
pg_dump --format=custom --file=/var/backups/db/app-$(date +%F).dump app
# MySQL / MariaDB: InnoDB tables without locking
mysqldump --single-transaction --routines --triggers app > /var/backups/db/app-$(date +%F).sql
Run it as a user with read access only, using ~/.pgpass or ~/.my.cnf for credentials, not a password on the command line.
2. Encrypt it and get it off the server
A backup on the same VPS dies with the VPS, and an attacker with root deletes it. restic encrypts on the client and supports S3-compatible storage:
export RESTIC_REPOSITORY=s3:https://s3.example.com/backups-app
export RESTIC_PASSWORD_FILE=/root/.restic-password
restic init # once
restic backup /var/backups/db /var/www/app/storage/app
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
Keep the restic password somewhere other than the server too (a password manager). Without it, the backup cannot be read, by you or anyone else. Use storage credentials that can write but not delete, or enable object lock, so a compromised server cannot erase its own history.
3. Restore it on a schedule
Once a month, restore into a scratch location and check it:
restic restore latest --target /tmp/restore-test
createdb restore_check
pg_restore --dbname=restore_check /tmp/restore-test/var/backups/db/app-*.dump
psql restore_check -c 'select count(*) from users;'
dropdb restore_check && rm -rf /tmp/restore-test
Compare the row count with production, or run the app's test suite against the restored copy. Write down how long the restore took: that is your real recovery time.
4. Know when it stops working
Alert on the absence of a fresh snapshot, not on the job's exit code alone. restic snapshots --latest 1 in a daily check that pages you when the newest snapshot is older than 26 hours catches a silently broken cron entry.