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
20set -eu
21
22ts=$(date -u +%Y%m%d-%H%M%S)
23prefix=${BRIVEN_BACKUP_PREFIX:-prod}
24tmp=/tmp/briven-backup-$ts
25mkdir -p "$tmp"
26
27echo "[briven-backup $ts] control-plane pg_dump"
28pg_dump --format=custom --compress=6 --file "$tmp/control.dump" \
29 "$BRIVEN_BACKUP_CONTROL_URL"
30
31echo "[briven-backup $ts] data-plane pg_dump (all schemas)"
32pg_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
38echo "[briven-backup $ts] uploading to b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts/"
39export B2_APPLICATION_KEY_ID="$BRIVEN_BACKUP_B2_KEY_ID"
40export B2_APPLICATION_KEY="$BRIVEN_BACKUP_B2_APP_KEY"
41b2 sync --delete "$tmp" "b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts"
42
43rm -rf "$tmp"
44echo "[briven-backup $ts] done"