#!/bin/bash # AKOS restore: scripts/restore.sh [target-db-name] [artifacts.tgz] # Restores into the target database (default: akos_restore_test — never the live DB unless you pass it explicitly). set -euo pipefail export PATH=/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:/usr/bin:/bin DUMP="${1:?dump file}"; DB="${2:-akos_restore_test}"; ART="${3:-}" if [ -f "${DUMP%.dump}.sha256" ]; then (cd "$(dirname "$DUMP")" && shasum -a 256 -c "$(basename "${DUMP%.dump}.sha256")" >/dev/null) && echo "checksum ok"; fi psql -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DB'" | grep -q 1 || psql -d postgres -qc "CREATE DATABASE $DB OWNER akos" pg_restore --clean --if-exists --no-owner -d "$DB" "$DUMP" echo "restored $DUMP into $DB: $(psql -d "$DB" -tAc 'select count(*) from executions') executions, $(psql -d "$DB" -tAc 'select count(*) from knowledge_packages') packages, $(psql -d "$DB" -tAc 'select count(*) from audit_events') audit events" if [ -n "$ART" ]; then DEST="${AKOS_RESTORE_DATA:-/tmp/akos-restore-data}"; mkdir -p "$DEST"; tar -C "$DEST" -xzf "$ART"; echo "artifacts extracted to $DEST/artifacts ($(find "$DEST/artifacts" -type f | wc -l | tr -d ' ') files)"; fi