Files
loki/scripts/db/dump-from-neon.sh
T
Mao NakamotoandClaude Opus 4.7 a59180cb18 chore(db): migration toolkit for Neon → Hetzner / Oracle Free
Grab-and-place migration kit so we can move off Neon (which capped our
data transfer earlier today) on ~30 min notice when the quota resets or
we upgrade the plan just long enough to dump.

scripts/db/dump-from-neon.sh
  pg_dump + emit a manifest of pg_stat_user_tables row counts. Plain SQL
  format (auditable, diff-friendly). Halts the script if SOURCE_DATABASE_URL
  is missing.

scripts/db/restore-to-target.sh
  psql --single-transaction --set ON_ERROR_STOP=on against the new host.
  Refuses to overwrite a target that already has users rows (set
  RECREATE_DB=1 to force). After restore, compares pg_stat_user_tables
  row counts against the source manifest and halts if any table
  mismatches — keeps a botched migration from making it to "flip Vercel."

scripts/db/SETUP_HETZNER.md
  Runbook from "open Hetzner account" to "Vercel deployed + smoke passes"
  for a CX22 (~€4.5/mo). Covers Postgres 17 install, SSL, ufw, fail2ban,
  pg_hba, env swap, smoke. ~30 min end-to-end.

scripts/db/SETUP_ORACLE_FREE.md
  Same shape for Oracle Always-Free ARM (4 OCPU / 24 GB / free forever).
  Calls out the gotchas: dual firewall (Oracle Security List + iptables),
  ARM-specific Postgres package source, idle-reclamation risk.

scripts/db/README.md
  6-step end-to-end migration cookbook tying the scripts and runbooks
  together. Why pg_dump over Drizzle migrations: data move, not schema
  sync. Why keep the Neon dump cold for 30 days post-migration.

Today the toolkit can't actually run end-to-end because Neon's quota is
blocking pg_dump. That'll clear on monthly reset (or with a one-month
plan upgrade just long enough to dump). When it does, the full migration
is a coffee break, not a project.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 08:15:13 +02:00

70 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Dump the FleetCrown production database from Neon to a portable SQL file
# that can be restored anywhere a Postgres 17 server is running.
#
# Usage:
# SOURCE_DATABASE_URL="postgresql://..." scripts/db/dump-from-neon.sh
#
# Output: ./neon-dump-<timestamp>.sql + a manifest with row counts so the
# restore script can verify nothing was silently dropped.
#
# Why a flat SQL dump instead of pg_dump's custom format:
# - We want to read it (eyeball schema, redact secrets in row data).
# - Custom format requires pg_restore (still pg, but one extra step).
# - SQL plays nicer with diff-tools when comparing pre/post-migration state.
#
# Why no --schema-only fallback by default: the data IS the migration. If
# you just want schema, run `pg_dump --schema-only ...` directly.
#
# Quota note (2026-06): Neon free tier's data transfer cap blocked pg_dump
# entirely once it tripped. If this script fails with "Your project has
# exceeded the data transfer quota," you must either upgrade the plan or
# wait for the monthly reset before this works.
set -euo pipefail
if [[ -z "${SOURCE_DATABASE_URL:-}" ]]; then
echo "error: SOURCE_DATABASE_URL not set" >&2
echo "usage: SOURCE_DATABASE_URL='postgresql://...' $0" >&2
exit 64
fi
STAMP=$(date -u +%Y%m%d-%H%M%S)
OUT="neon-dump-${STAMP}.sql"
MANIFEST="neon-dump-${STAMP}.manifest.txt"
echo "==> dumping schema + data to ${OUT}"
pg_dump \
--no-owner \
--no-acl \
--no-tablespaces \
--format=plain \
--quote-all-identifiers \
--file="${OUT}" \
"${SOURCE_DATABASE_URL}"
SIZE=$(stat -c %s "${OUT}" 2>/dev/null || stat -f %z "${OUT}" 2>/dev/null)
echo " dump size: ${SIZE} bytes"
echo "==> writing manifest to ${MANIFEST}"
{
echo "Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Source host: $(echo "${SOURCE_DATABASE_URL}" | sed 's|.*@||; s|/.*||')"
echo "Dump file: ${OUT}"
echo "Dump size: ${SIZE} bytes"
echo
echo "Row counts (for post-restore verification):"
psql "${SOURCE_DATABASE_URL}" -tAc "
SELECT schemaname || '.' || relname || ' ' || n_live_tup
FROM pg_stat_user_tables
ORDER BY relname
" | sed 's/^/ /'
} > "${MANIFEST}"
echo
echo "==> done"
echo " ${OUT}"
echo " ${MANIFEST}"
echo
echo "Next: scripts/db/restore-to-target.sh"