pg-dump.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. set -eu
  20. ts=$(date -u +%Y%m%d-%H%M%S)
  21. prefix=${BRIVEN_BACKUP_PREFIX:-prod}
  22. tmp=/tmp/briven-backup-$ts
  23. mkdir -p "$tmp"
  24. echo "[briven-backup $ts] control-plane pg_dump"
  25. pg_dump --format=custom --compress=6 --file "$tmp/control.dump" \
  26. "$BRIVEN_BACKUP_CONTROL_URL"
  27. echo "[briven-backup $ts] data-plane pg_dump (all schemas)"
  28. pg_dump --format=custom --compress=6 --file "$tmp/data.dump" \
  29. "$BRIVEN_BACKUP_DATA_URL"
  30. # Optional SHA256 manifest — cheap integrity check at restore time.
  31. ( cd "$tmp" && sha256sum *.dump > sha256sums.txt )
  32. echo "[briven-backup $ts] uploading to b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts/"
  33. export B2_APPLICATION_KEY_ID="$BRIVEN_BACKUP_B2_KEY_ID"
  34. export B2_APPLICATION_KEY="$BRIVEN_BACKUP_B2_APP_KEY"
  35. b2 sync --delete "$tmp" "b2://$BRIVEN_BACKUP_B2_BUCKET/$prefix/$ts"
  36. rm -rf "$tmp"
  37. echo "[briven-backup $ts] done"