#!/usr/bin/env bash
# Dump the gateway's database to $BACKUP_BUCKET. Run as root by labllm-dump.service.
# The dump is finished on local disk before the upload, so a failed dump uploads nothing.
# The upload is GCP's `gcloud storage`; another cloud swaps that one line.
set -euo pipefail
: "${BACKUP_BUCKET:?set BACKUP_BUCKET in /etc/labllm/backup.env}"
name="labllm-$(date -u +%Y%m%dT%H%M%SZ).dump"
tmp=$(mktemp /var/tmp/labllm-dump.XXXXXX)
trap 'rm -f "$tmp"' EXIT
labllm-db pg_dump --format=custom labllm >"$tmp"
gcloud storage cp --quiet "$tmp" "$BACKUP_BUCKET/$name"
echo "dumped to $BACKUP_BUCKET/$name"
