pg-dump.sh44 lines · main
| 1 | #!/bin/sh |
| 2 | # briven — nightly pg_dump of every data-plane project schema + control meta-DB |
| 3 | # intended to run as a Dokploy cron (or plain host crontab) on the KVM that |
| 4 | # hosts the postgres containers. |
| 5 | # |
| 6 | # required env: |
| 7 | # BRIVEN_BACKUP_B2_KEY_ID — Backblaze B2 application key id |
| 8 | # BRIVEN_BACKUP_B2_APP_KEY — Backblaze B2 application key secret |
| 9 | # BRIVEN_BACKUP_B2_BUCKET — target bucket name (e.g. briven-backups) |
| 10 | # BRIVEN_BACKUP_CONTROL_URL — postgres://… for briven_control |
| 11 | # BRIVEN_BACKUP_DATA_URL — postgres://… for briven_data |
| 12 | # |
| 13 | # optional env: |
| 14 | # BRIVEN_BACKUP_PREFIX — key prefix inside the bucket (default: prod/) |
| 15 | # BRIVEN_BACKUP_RETENTION_DAYS — not enforced here; set a bucket lifecycle |
| 16 | # rule instead (kept as a doc hint). |
| 17 | # |
| 18 | # restore is documented in infra/backups/RESTORE.md |
| 19 | |
| 20 | set -eu |
| 21 | |
| 22 | ts=$(date -u +%Y%m%d-%H%M%S) |
| 23 | prefix=${BRIVEN_BACKUP_PREFIX:-prod} |
| 24 | tmp=/tmp/briven-backup-$ts |
| 25 | mkdir -p "$tmp" |
| 26 | |
| 27 | echo "[briven-backup $ts] control-plane pg_dump" |
| 28 | pg_dump --format=custom --compress=6 --file "$tmp/control.dump" \ |
| 29 | "$BRIVEN_BACKUP_CONTROL_URL" |
| 30 | |
| 31 | echo "[briven-backup $ts] data-plane pg_dump (all schemas)" |
| 32 | pg_dump --format=custom --compress=6 --file "$tmp/data.dump" \ |
| 33 | "$BRIVEN_BACKUP_DATA_URL" |
| 34 | |
| 35 | # Optional SHA256 manifest — cheap integrity check at restore time. |
| 36 | ( cd "$tmp" && sha256sum *.dump > sha256sums.txt ) |
| 37 | |
| 38 | echo "[briven-backup $ts] uploading to b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts/" |
| 39 | export B2_APPLICATION_KEY_ID="$BRIVEN_BACKUP_B2_KEY_ID" |
| 40 | export B2_APPLICATION_KEY="$BRIVEN_BACKUP_B2_APP_KEY" |
| 41 | b2 sync --delete "$tmp" "b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts" |
| 42 | |
| 43 | rm -rf "$tmp" |
| 44 | echo "[briven-backup $ts] done" |