LifeMate — Personal, Family & Learning Companion

docs/offline-data-rescue-options
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
sh 26 lines 2.58 KB
Raw
1 #!/usr/bin/env bash
2 # Explicit operator-only adoption. Never SSHs or restores/drops application data.
3 set -euo pipefail
4 cd "$(dirname "${BASH_SOURCE[0]}")/.."
5 [[ "${1:-}" == '--local-existing' && "${2:-}" == '--expected-owner' && "${4:-}" == '--backup-file' && "$#" -eq 5 ]] || { echo 'Usage: --local-existing --expected-owner ROLE --backup-file /backups/LifeGuide-....dump' >&2; exit 1; }
6 expected_owner="$3"
7 backup_file="$5"
8 [[ "$expected_owner" =~ ^[a-z_][a-z0-9_]{0,62}$ && "$backup_file" =~ ^/backups/LifeGuide-[A-Za-z0-9._-]+\.dump$ ]] || { echo 'Use an explicit safe expected owner and archive within the backup volume' >&2; exit 1; }
9 if [[ -n "${DOCKER_CONTEXT:-}" ]]; then endpoint="$(docker context inspect "$DOCKER_CONTEXT" --format '{{.Endpoints.docker.Host}}')"; else endpoint="${DOCKER_HOST:-$(docker context inspect --format '{{.Endpoints.docker.Host}}')}"; fi
10 case "$endpoint" in unix://*|npipe://*) ;; *) echo 'Refusing remote Docker' >&2; exit 1;; esac
11 docker compose config --quiet
12 # Stop writers deliberately before invoking this helper; it refuses an active API.
13 if [[ -n "$(docker compose ps --status running -q api)" ]]; then echo 'Stop the local API/gateway writers and take a fresh verified backup before adoption' >&2; exit 1; fi
14 database="$(sed -n 's/^POSTGRES_DB=//p' .env)"
15 [[ "$database" =~ ^[a-z_][a-z0-9_]{0,62}$ ]] || { echo 'Invalid configured app database name' >&2; exit 1; }
16 toc_file="$(mktemp "${TMPDIR:-/tmp}/lifeguide-adoption-toc.XXXXXX")"
17 trap 'rm -f "$toc_file"' EXIT
18 docker compose run --rm --no-deps --entrypoint pg_restore -e TZ=UTC backup --list "$backup_file" >"$toc_file"
19 archive_db="$(sed -n 's/^;[[:space:]]*dbname: //p' "$toc_file")"
20 [[ "$archive_db" == "$database" ]] || { echo 'Backup is for another database; refusing adoption' >&2; exit 1; }
21 digest="$(docker compose run --rm --no-deps --entrypoint sha256sum backup "$backup_file" | cut -d' ' -f1)"
22 [[ "$digest" =~ ^[a-f0-9]{64}$ ]] || { echo 'Cannot hash verified archive' >&2; exit 1; }
23 node --input-type=module -e 'console.log(JSON.stringify({format:"LifeGuide-adoption-backup-v1",database:process.argv[1],sha256:process.argv[2],pgRestoreList:true,validatedAt:new Date().toISOString()}))' "$database" "$digest" | \
24 docker compose run --rm --no-deps --entrypoint sh backup -c 'umask 077; cat > "$1.validated.json"' sh "$backup_file"
25 docker compose run --rm --no-deps db-provision node scripts/provision-db-roles.js --prepare --adopt-existing --expected-owner "$expected_owner" --backup-file "$backup_file"
26 echo 'PASS explicit local ownership adoption. Run migrations/grants/start and synthetic acceptance before admitting users.'