# 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