#!/usr/bin/env bash # PEP-63: moves Bugsink's data from its Postgres database to SQLite in the volume peppa-bugsink-data. # Django's own way (Bugsink has no dedicated tool): `dumpdata` from Postgres → fresh SQLite with # `migrate` → `loaddata` → row counts compared table by table (`modelcounts`). Postgres is only read. # # scripts/bugsink-migrate-to-sqlite.sh [path/to/.env] (default: /.env) # # Steps: stop peppa-bugsink (no events arrive during the dump) → dump → load into the volume → compare # → start Bugsink on SQLite (infra/docker-compose.yml) → health. Any failure before the start leaves # Postgres as it was and starts the old container again. The dump is kept in ~/.peppa/backups. # The volume must be empty (or absent): the script refuses to overwrite an existing database. # # Overrides (tests): BUGSINK_IMAGE, BUGSINK_SOURCE_CONTAINER, BUGSINK_PG_URL, BUGSINK_PG_NETWORK, # BUGSINK_SQLITE_VOLUME, BUGSINK_BACKUP_DIR, BUGSINK_MIGRATE_NO_START=1 (stop after the comparison). set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" ENV_FILE="${1:-$ROOT/.env}" [ -f "$ENV_FILE" ] || { echo "no $ENV_FILE"; exit 1; } get() { grep -E "^$1=" "$ENV_FILE" | tail -1 | cut -d= -f2- || true; } IMAGE="${BUGSINK_IMAGE:-bugsink/bugsink:2.6.3}" SOURCE="${BUGSINK_SOURCE_CONTAINER:-peppa-bugsink}" PG_URL="${BUGSINK_PG_URL:-postgresql://bugsink:$(get BUGSINK_DB_PASSWORD)@peppa-postgres:5432/bugsink}" PG_NETWORK="${BUGSINK_PG_NETWORK:-peppa_default}" VOLUME="${BUGSINK_SQLITE_VOLUME:-peppa-bugsink-data}" BACKUPS="${BUGSINK_BACKUP_DIR:-${PEPPA_HOME:-$HOME/.peppa}/backups}" SECRET="$(get BUGSINK_SECRET_KEY)" [ -n "$SECRET" ] || { echo "BUGSINK_SECRET_KEY is not in $ENV_FILE"; exit 1; } STAMP="$(date +%Y%m%d-%H%M%S)" DUMP="$BACKUPS/bugsink-pg-$STAMP.json" mkdir -p "$BACKUPS" chmod 700 "$BACKUPS" # bugsink-manage in a throwaway container: on Postgres (source) or on the SQLite volume (target). on_pg() { docker run --rm -i --network "$PG_NETWORK" -e SECRET_KEY="$SECRET" -e DATABASE_URL="$PG_URL" --entrypoint bugsink-manage "$IMAGE" "$@"; } on_sqlite() { docker run --rm -i -v "$VOLUME:/data" -e SECRET_KEY="$SECRET" -e DATABASE_PATH=/data/db.sqlite3 "$@"; } # Refuse to overwrite: the target volume must not hold a database yet. if docker volume inspect "$VOLUME" >/dev/null 2>&1; then if docker run --rm -v "$VOLUME:/data" --entrypoint sh "$IMAGE" -c 'test -s /data/db.sqlite3'; then echo "$VOLUME already has db.sqlite3 — move it away first (docker volume rm $VOLUME after checking)"; exit 1 fi fi on_pg migrate --check >/dev/null || { echo "Postgres source is not reachable or not migrated: $PG_NETWORK / ${PG_URL%%:*}…"; exit 1; } was_running="" if [ "$(docker inspect -f '{{.State.Running}}' "$SOURCE" 2>/dev/null || true)" = "true" ]; then was_running=1 echo "stopping $SOURCE (no new events during the move)" docker stop "$SOURCE" >/dev/null fi loaded="" restore() { echo "move failed — Postgres untouched" # A half-loaded database must not block the next attempt. if [ -n "$loaded" ]; then docker run --rm -v "$VOLUME:/data" --entrypoint sh "$IMAGE" -c 'rm -f /data/db.sqlite3 /data/db.sqlite3-wal /data/db.sqlite3-shm'; fi if [ -n "$was_running" ]; then docker start "$SOURCE" >/dev/null && echo "$SOURCE started again on Postgres"; fi } trap restore ERR echo "dumping Postgres → $DUMP" # contenttypes and auth.permission are recreated by `migrate`; references to them go by natural key. # sessions: logins are not worth keeping (users sign in again). on_pg dumpdata --natural-foreign --natural-primary \ --exclude contenttypes --exclude auth.permission --exclude sessions --exclude admin.logentry \ >"$DUMP" chmod 600 "$DUMP" echo "dump: $(wc -c <"$DUMP" | tr -d ' ') bytes" echo "loading into SQLite (volume $VOLUME)" loaded=1 on_sqlite -v "$DUMP:/dump.json:ro" --entrypoint sh "$IMAGE" -c ' set -e bugsink-manage migrate --no-input >/dev/null # migrate makes its own installation row; the dump brings the original one. bugsink-manage shell -c "from phonehome.models import Installation; Installation.objects.all().delete()" bugsink-manage loaddata /dump.json' echo "comparing row counts" # Rows of every model of the main database except the ones recreated or skipped above (snappea is # the container's own queue, not data). `modelcounts` would also touch the throwaway queue database. COUNT_PY=' from django.apps import apps skip = {"contenttypes", "sessions", "admin", "snappea"} for m in sorted(apps.get_models(), key=lambda m: m._meta.label): if m._meta.app_label in skip or m._meta.label == "auth.Permission": continue print(m._meta.label, m.objects.count())' counts() { grep -E "^[a-z_]+\.[A-Za-z]+ [0-9]+$" || true; } before="$(on_pg shell -c "$COUNT_PY" | counts)" after="$(on_sqlite --entrypoint bugsink-manage "$IMAGE" shell -c "$COUNT_PY" | counts)" if [ "$before" != "$after" ]; then diff <(echo "$before") <(echo "$after") || true false fi echo "$after" trap - ERR if [ -n "${BUGSINK_MIGRATE_NO_START:-}" ]; then echo "moved; not starting (BUGSINK_MIGRATE_NO_START)"; exit 0; fi echo "starting Bugsink on SQLite" PEPPA_ENV_FILE="$ENV_FILE" "$ROOT/scripts/infra-up.sh" bugsink for _ in $(seq 1 60); do curl -sf http://127.0.0.1:3180/health/ready >/dev/null && break sleep 2 done curl -sf http://127.0.0.1:3180/health/ready >/dev/null || { echo "bugsink not ready on SQLite — see rollback in infra/bugsink-legacy-pg.yml"; docker logs --tail 30 "$SOURCE"; exit 1; } echo "Bugsink runs on SQLite. Postgres (profile legacy) keeps the old data for rollback; dump: $DUMP"