#!/usr/bin/env bash # Backup and restore drill (spec NFR-005, AC-17): an online backup of the live database verifies (integrity_check, # foreign keys, migrations, row counts), a damaged or missing file is refused, and the backup restored into an isolated # environment (a separate container on port 8802 with its own db folder; the live database is never touched) serves # the same data: the export of a test tenant from the restored copy equals the export from the live server. # The restore time is measured against RTO_SECONDS (default 120); RPO is the backup interval (see README). # # Needs the compose stack running (container "axonasp", http://localhost:8801). Creates a throw-away tenant, one # backup file under db/backups and a scratch folder db/backups/drill-*, and removes all of them at the end. # bash tests/backup-restore.sh set -u BASE=${BASE:-http://localhost:8801/api/v1} ISO_PORT=${ISO_PORT:-8802} ISO_BASE=http://localhost:$ISO_PORT/api/v1 RTO_SECONDS=${RTO_SECONDS:-120} IMAGE=$(docker inspect axonasp --format '{{.Config.Image}}') REPO=$(cd "$(dirname "$0")/.." && pwd -W) TAG=drill-$$-$RANDOM BK=db/backups/$TAG.sqlite ISO=db/backups/$TAG ISO_NAME=amk-restore-$TAG PASS=0; FAIL=0; FAILED=(); TENANT="" export MSYS_NO_PATHCONV=1 prov() { docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \ -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \ axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015' } bak() { # bak [file] (in the live container) docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site -e "BACKUP_ACTION=$1" -e "BACKUP_FILE=${2:-}" \ axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/backup.asp 2>&1 | tr -d '\015' } iso_run() { # one-off container with the isolated db folder (what scripts/restore.cmd does, minus the live db) docker run --rm -v "$REPO:/opt/axonasp/site" -v "$REPO/$ISO:/opt/axonasp/site/db" \ -e SERVER_WEB_ROOT=/opt/axonasp/site -e "BACKUP_ACTION=$1" -e "BACKUP_FILE=${2:-}" \ "$IMAGE" ./axonasp-cli -r /opt/axonasp/site/scripts/backup.asp 2>&1 | tr -d '\015' } field() { sed -n "s/.*$1: *\([^ ]*\).*/\1/p" | head -1; } jget() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -1; } last() { grep '^RESULT:' | tail -1; } check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; } data_of() { python -I -c "import sys,json;sys.stdin.reconfigure(encoding='utf-8');print(json.dumps(json.load(sys.stdin)['data'],sort_keys=True))"; } sha() { sha256sum "$1" | cut -c1-64; } cleanup() { docker rm -f "$ISO_NAME" >/dev/null 2>&1 [ -n "$TENANT" ] && prov purge "$TENANT" >/dev/null rm -rf "$REPO/$ISO" "$REPO/$BK" "$REPO/$BK-damaged" } trap cleanup EXIT cd "$REPO" || exit 2 # ---- test data in the live database ---------------------------------------------------------------------------- TENANT=$(prov tenant "drill" | field tenant_id) OUT=$(prov principal "$TENANT" drill-owner human); PID=$(echo "$OUT" | field principal_id); KEY=$(echo "$OUT" | field api_key) prov grant "$PID" /drill "read,write,admin" 1 >/dev/null NS=$(prov namespace "$TENANT" /drill | field namespace_id) [ -n "$NS" ] && [ -n "$KEY" ] || { echo "setup failed"; exit 2; } auth=(-H "Authorization: Bearer $KEY") MID=$(curl -s "${auth[@]}" -X POST "$BASE/remember" -d "{\"namespace_id\":\"$NS\",\"type\":\"decision\",\"title\":\"Drill fact\",\"content\":\"Backups are taken with VACUUM INTO.\"}" | jget id) curl -s -o /dev/null "${auth[@]}" -X POST "$BASE/memories/$MID/verifications" -d '{"result":"verified","method":"manual","revision":1}' curl -s -o /dev/null "${auth[@]}" -X POST "$BASE/ingestion/jobs" -d "{\"namespace_id\":\"$NS\",\"title\":\"Drill runbook\",\"format\":\"markdown\",\"content\":\"# Restore\n\nStop the server, copy, start, reindex.\"}" [ -n "$MID" ] || { echo "data setup failed"; exit 2; } # ---- backup and verify (live container, server running) -------------------------------------------------------- OUT=$(bak backup "$BK") check "backup -> ok" "RESULT: ok" "$(echo "$OUT" | last)" check "backup integrity_check ok" "ok" "$(echo "$OUT" | field integrity_check)" check "backup row counts match live" "match" "$(echo "$OUT" | field row_counts)" check "backup file exists" "yes" "$([ -s "$BK" ] && echo yes || echo no)" check "backup to an existing file -> failed" "RESULT: failed" "$(bak backup "$BK" | last)" check "backup path with .. -> failed" "RESULT: failed" "$(bak backup "db/../x.sqlite" | last)" check "verify backup -> ok" "RESULT: ok" "$(bak verify "$BK" | last)" check "verify missing file -> failed" "RESULT: failed" "$(bak verify "db/backups/none-$TAG.sqlite" | last)" python -I -c "import sys;b=bytearray(open(sys.argv[1],'rb').read());n=len(b)//2;b[n:n+20000]=b'\xa5'*20000;open(sys.argv[2],'wb').write(b)" "$BK" "$BK-damaged" check "verify damaged file -> failed" "RESULT: failed" "$(bak verify "$BK-damaged" | last)" LIVE_EXPORT=$(curl -s "${auth[@]}" -X POST "$BASE/exports" -d '{}' | data_of) # ---- restore into an isolated environment ---------------------------------------------------------------------- mkdir -p "$ISO" cp "$BK" "$ISO/backup.sqlite"; cp "$BK-damaged" "$ISO/damaged.sqlite" OUT=$(iso_run restore db/damaged.sqlite) check "restore of a damaged backup -> failed" "RESULT: failed" "$(echo "$OUT" | last)" check "failed restore created no database" "no" "$([ -e "$ISO/app.sqlite" ] && echo yes || echo no)" T0=$(date +%s) OUT=$(iso_run restore db/backup.sqlite) check "isolated restore -> ok" "RESULT: ok" "$(echo "$OUT" | last)" check "restored file equals the backup" "$(sha "$BK")" "$(sha "$ISO/app.sqlite")" docker run -d --name "$ISO_NAME" -p "$ISO_PORT:8801" -v "$REPO:/opt/axonasp/site" -v "$REPO/$ISO:/opt/axonasp/site/db" \ -e SERVER_WEB_ROOT=/opt/axonasp/site/public "$IMAGE" >/dev/null for i in $(seq 1 60); do curl -s -o /dev/null "$ISO_BASE/health/live" && break; sleep 1; done RTO=$(( $(date +%s) - T0 )) check "restored server healthy within RTO (${RTO}s <= ${RTO_SECONDS}s)" "yes" "$([ "$(curl -s -o /dev/null -w '%{http_code}' "$ISO_BASE/health/live")" = 200 ] && [ "$RTO" -le "$RTO_SECONDS" ] && echo yes || echo no)" R=$(curl -s "${auth[@]}" "$ISO_BASE/memories/$MID") check "restored: API key works, memory present" "Backups are taken with VACUUM INTO." "$(echo "$R" | python -I -c "import sys,json;print(json.load(sys.stdin)['data']['content'])")" check "restored: verification kept" "verified" "$(echo "$R" | python -I -c "import sys,json;print(json.load(sys.stdin)['data']['verification_status'])")" check "restored: lexical search works" "yes" "$(curl -s "${auth[@]}" -X POST "$ISO_BASE/search" -d '{"query":"reindex"}' | grep -q '"chunk_id"' && echo yes || echo no)" check "restored export equals live export" "yes" "$([ "$(curl -s "${auth[@]}" -X POST "$ISO_BASE/exports" -d '{}' | data_of)" = "$LIVE_EXPORT" ] && echo yes || echo no)" check "restore kept no pre-restore copy (no previous db)" "0" "$(ls "$ISO"/backups 2>/dev/null | grep -c pre-restore)" OUT=$(docker exec "$ISO_NAME" sh -c 'BACKUP_ACTION=restore BACKUP_FILE=db/backup.sqlite SERVER_WEB_ROOT=/opt/axonasp/site ./axonasp-cli -r /opt/axonasp/site/scripts/backup.asp' 2>&1 | tr -d '\015') check "second restore keeps the previous db" "1" "$(ls "$ISO"/backups 2>/dev/null | grep -c pre-restore)" check "live server unaffected" "200" "$(curl -s -o /dev/null -w '%{http_code}' "${auth[@]}" "$BASE/memories/$MID")" echo "restore time: ${RTO}s (RTO target ${RTO_SECONDS}s)" echo "passed: $PASS failed: $FAIL" for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done [ "$FAIL" -eq 0 ]