Database Backups

Backing Up and Restoring BookNest's Database

A volume is not a backup: Retain survives a deleted claim, not a dropped table or a lost node. The simplest backup is a logical one, pg_dump, streamed out of the Pod. The sql helper runs psql in postgres-0:

Dumping the database, losing a row and restoring itYAML
sql() { kubectl exec -i postgres-0 -- psql -U booknest -d booknest "$@"; }
kubectl exec postgres-0 -- pg_dump -U booknest -d booknest --clean --if-exists > ~/booknest.sql
grep -c '^' ~/booknest.sql
sql -c "DELETE FROM books WHERE genre = 'Cooking'"
sql -q -o /dev/null < ~/booknest.sql
sql -tAc 'SELECT id, title FROM books WHERE id = 3'
Output
75
DELETE 1
3|Salt and Saffron

--clean --if-exists lets the dump restore onto a live database; a nightly CronJob (CronJobs and Schedule Syntax) can take it and copy it off the cluster. Large databases need physical backups (pg_basebackup plus WAL archiving, for point-in-time recovery). CSI VolumeSnapshots, Velero 570,931 (github.com/vmware-tanzu/velero (https://github.com/vmware-tanzu/velero 10,316 ), Apache-2.0, v1.18.3) and operators such as CloudNativePG 171,411 (PostgreSQL Operator) cover the rest. Test your restores.