devops/scripts/migrate-db.sh
Marcus (agent-cos) 7b5ffe6b11 Initial cluster config: NixOS flake + k3s + 5 node definitions
- Multi-host flake.nix (3x N97 x86_64 + 2x RPi4 aarch64)
- Shared modules: k3s-cluster, tailscale, meos-gateway
- Node configs: n97-1 (control plane), n97-2/n97-3 (Ollama workers), rpi4-1/rpi4-2 (ARM workers)
- K8s manifests: Forgejo, Postgres (CNPG), Umami, OpenBrain, MeOS Dashboard, MCP servers
- Bootstrap and DB migration scripts
- Migration plan doc (7-day phased rollout)
2026-05-04 12:22:09 -07:00

49 lines
No EOL
1.8 KiB
Bash
Executable file

# Migrate PostgreSQL from bare metal (n97-1) to CloudNative PG (k3s)
#
# Prerequisites:
# - CNPG operator installed in k3s
# - postgres-scobleclaw cluster created (apps/postgres/cluster.yaml)
# - Both clusters accessible from n97-1
#
set -euo pipefail
SOURCE_HOST="127.0.0.1"
SOURCE_PORT="5432"
CNPG_HOST="postgres-scobleclaw-rw.database.svc.cluster.local"
CNPG_PORT="5432"
echo "==> Starting PostgreSQL migration"
echo " Source: $SOURCE_HOST:$SOURCE_PORT (bare metal)"
echo " Target: $CNPG_HOST:$CNPG_PORT (k3s/CNPG)"
# 1. Dump all databases
echo "==> Dumping databases from bare metal..."
sudo -u postgres pg_dumpall --clean --if-exists > /tmp/pg-migration-dump.sql
# 2. Wait for CNPG cluster to be ready
echo "==> Waiting for CNPG cluster to be ready..."
kubectl -n database wait --for=condition=Ready cluster/postgres-scobleclaw --timeout=300s
# 3. Get CNPG superuser password
CNPG_PASSWORD=$(kubectl -n database get secret postgres-scobleclaw-superuser -o jsonpath='{.data.password}' | base64 -d)
# 4. Restore dump into CNPG
echo "==> Restoring dump into CNPG..."
PGPASSWORD="$CNPG_PASSWORD" psql -h "$CNPG_HOST" -p "$CNPG_PORT" -U postgres < /tmp/pg-migration-dump.sql
# 5. Verify
echo "==> Verifying migration..."
DB_COUNT=$(PGPASSWORD="$CNPG_PASSWORD" psql -h "$CNPG_HOST" -p "$CNPG_PORT" -U postgres -t -c "SELECT count(*) FROM pg_database WHERE datname IN ('forgejo','umami');" | tr -d ' ')
echo " Databases found: $DB_COUNT (expected: 2)"
if [ "$DB_COUNT" = "2" ]; then
echo "==> Migration successful! You can now disable bare-metal PostgreSQL:"
echo " Set services.postgresql.enable = false; in nodes/n97-1.nix"
echo " And rebuild: nixos-rebuild switch --flake .#n97-1"
else
echo "==> WARNING: Not all databases migrated. Check manually."
exit 1
fi
# Cleanup
rm -f /tmp/pg-migration-dump.sql