You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 line
7.5KB

  1. #!/usr/bin/env bash
  2. # Backup and restore drill (spec NFR-005, AC-17): an online backup of the live database verifies (integrity_check,
  3. # foreign keys, migrations, row counts), a damaged or missing file is refused, and the backup restored into an isolated
  4. # environment (a separate container on port 8802 with its own db folder; the live database is never touched) serves
  5. # the same data: the export of a test tenant from the restored copy equals the export from the live server.
  6. # The restore time is measured against RTO_SECONDS (default 120); RPO is the backup interval (see README).
  7. #
  8. # Needs the compose stack running (container "axonasp", http://localhost:8801). Creates a throw-away tenant, one
  9. # backup file under db/backups and a scratch folder db/backups/drill-*, and removes all of them at the end.
  10. # bash tests/backup-restore.sh
  11. set -u
  12. BASE=${BASE:-http://localhost:8801/api/v1}
  13. ISO_PORT=${ISO_PORT:-8802}
  14. ISO_BASE=http://localhost:$ISO_PORT/api/v1
  15. RTO_SECONDS=${RTO_SECONDS:-120}
  16. IMAGE=$(docker inspect axonasp --format '{{.Config.Image}}')
  17. REPO=$(cd "$(dirname "$0")/.." && pwd -W)
  18. TAG=drill-$$-$RANDOM
  19. BK=db/backups/$TAG.sqlite
  20. ISO=db/backups/$TAG
  21. ISO_NAME=amk-restore-$TAG
  22. PASS=0; FAIL=0; FAILED=(); TENANT=""
  23. export MSYS_NO_PATHCONV=1
  24. prov() {
  25. docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
  26. -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
  27. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
  28. }
  29. bak() { # bak <action> [file] (in the live container)
  30. docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site -e "BACKUP_ACTION=$1" -e "BACKUP_FILE=${2:-}" \
  31. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/backup.asp 2>&1 | tr -d '\015'
  32. }
  33. iso_run() { # one-off container with the isolated db folder (what scripts/restore.cmd does, minus the live db)
  34. docker run --rm -v "$REPO:/opt/axonasp/site" -v "$REPO/$ISO:/opt/axonasp/site/db" \
  35. -e SERVER_WEB_ROOT=/opt/axonasp/site -e "BACKUP_ACTION=$1" -e "BACKUP_FILE=${2:-}" \
  36. "$IMAGE" ./axonasp-cli -r /opt/axonasp/site/scripts/backup.asp 2>&1 | tr -d '\015'
  37. }
  38. field() { sed -n "s/.*$1: *\([^ ]*\).*/\1/p" | head -1; }
  39. jget() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" | head -1; }
  40. last() { grep '^RESULT:' | tail -1; }
  41. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  42. 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))"; }
  43. sha() { sha256sum "$1" | cut -c1-64; }
  44. cleanup() {
  45. docker rm -f "$ISO_NAME" >/dev/null 2>&1
  46. [ -n "$TENANT" ] && prov purge "$TENANT" >/dev/null
  47. rm -rf "$REPO/$ISO" "$REPO/$BK" "$REPO/$BK-damaged"
  48. }
  49. trap cleanup EXIT
  50. cd "$REPO" || exit 2
  51. # ---- test data in the live database ----------------------------------------------------------------------------
  52. TENANT=$(prov tenant "drill" | field tenant_id)
  53. OUT=$(prov principal "$TENANT" drill-owner human); PID=$(echo "$OUT" | field principal_id); KEY=$(echo "$OUT" | field api_key)
  54. prov grant "$PID" /drill "read,write,admin" 1 >/dev/null
  55. NS=$(prov namespace "$TENANT" /drill | field namespace_id)
  56. [ -n "$NS" ] && [ -n "$KEY" ] || { echo "setup failed"; exit 2; }
  57. auth=(-H "Authorization: Bearer $KEY")
  58. 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)
  59. curl -s -o /dev/null "${auth[@]}" -X POST "$BASE/memories/$MID/verifications" -d '{"result":"verified","method":"manual","revision":1}'
  60. 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.\"}"
  61. [ -n "$MID" ] || { echo "data setup failed"; exit 2; }
  62. # ---- backup and verify (live container, server running) --------------------------------------------------------
  63. OUT=$(bak backup "$BK")
  64. check "backup -> ok" "RESULT: ok" "$(echo "$OUT" | last)"
  65. check "backup integrity_check ok" "ok" "$(echo "$OUT" | field integrity_check)"
  66. check "backup row counts match live" "match" "$(echo "$OUT" | field row_counts)"
  67. check "backup file exists" "yes" "$([ -s "$BK" ] && echo yes || echo no)"
  68. check "backup to an existing file -> failed" "RESULT: failed" "$(bak backup "$BK" | last)"
  69. check "backup path with .. -> failed" "RESULT: failed" "$(bak backup "db/../x.sqlite" | last)"
  70. check "verify backup -> ok" "RESULT: ok" "$(bak verify "$BK" | last)"
  71. check "verify missing file -> failed" "RESULT: failed" "$(bak verify "db/backups/none-$TAG.sqlite" | last)"
  72. 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"
  73. check "verify damaged file -> failed" "RESULT: failed" "$(bak verify "$BK-damaged" | last)"
  74. LIVE_EXPORT=$(curl -s "${auth[@]}" -X POST "$BASE/exports" -d '{}' | data_of)
  75. # ---- restore into an isolated environment ----------------------------------------------------------------------
  76. mkdir -p "$ISO"
  77. cp "$BK" "$ISO/backup.sqlite"; cp "$BK-damaged" "$ISO/damaged.sqlite"
  78. OUT=$(iso_run restore db/damaged.sqlite)
  79. check "restore of a damaged backup -> failed" "RESULT: failed" "$(echo "$OUT" | last)"
  80. check "failed restore created no database" "no" "$([ -e "$ISO/app.sqlite" ] && echo yes || echo no)"
  81. T0=$(date +%s)
  82. OUT=$(iso_run restore db/backup.sqlite)
  83. check "isolated restore -> ok" "RESULT: ok" "$(echo "$OUT" | last)"
  84. check "restored file equals the backup" "$(sha "$BK")" "$(sha "$ISO/app.sqlite")"
  85. docker run -d --name "$ISO_NAME" -p "$ISO_PORT:8801" -v "$REPO:/opt/axonasp/site" -v "$REPO/$ISO:/opt/axonasp/site/db" \
  86. -e SERVER_WEB_ROOT=/opt/axonasp/site/public "$IMAGE" >/dev/null
  87. for i in $(seq 1 60); do curl -s -o /dev/null "$ISO_BASE/health/live" && break; sleep 1; done
  88. RTO=$(( $(date +%s) - T0 ))
  89. 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)"
  90. R=$(curl -s "${auth[@]}" "$ISO_BASE/memories/$MID")
  91. 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'])")"
  92. check "restored: verification kept" "verified" "$(echo "$R" | python -I -c "import sys,json;print(json.load(sys.stdin)['data']['verification_status'])")"
  93. 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)"
  94. check "restored export equals live export" "yes" "$([ "$(curl -s "${auth[@]}" -X POST "$ISO_BASE/exports" -d '{}' | data_of)" = "$LIVE_EXPORT" ] && echo yes || echo no)"
  95. check "restore kept no pre-restore copy (no previous db)" "0" "$(ls "$ISO"/backups 2>/dev/null | grep -c pre-restore)"
  96. 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')
  97. check "second restore keeps the previous db" "1" "$(ls "$ISO"/backups 2>/dev/null | grep -c pre-restore)"
  98. check "live server unaffected" "200" "$(curl -s -o /dev/null -w '%{http_code}' "${auth[@]}" "$BASE/memories/$MID")"
  99. echo "restore time: ${RTO}s (RTO target ${RTO_SECONDS}s)"
  100. echo "passed: $PASS failed: $FAIL"
  101. for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done
  102. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.