~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
126 lines · 5.0 KB Raw History
#!/usr/bin/env sh
# Restore the Litestream replica of the zds database into a scratch directory
# and check that the copy is usable: SQLite's full integrity check, foreign
# keys, the size of each table that matters, and (with ZDS_DRILL_BIN) a boot
# of zds against it. It only reads the bucket and never touches the live
# volume. See docs/backups.md.
#
# Needs litestream and sqlite3 on PATH and the same bucket settings the
# container uses (ZDS_LITESTREAM_BUCKET, AWS_ACCESS_KEY_ID,
# AWS_SECRET_ACCESS_KEY, AWS_ENDPOINT_URL_S3, AWS_REGION). Optional:
#   ZDS_LITESTREAM_PATH  replica path in the bucket (default zds.sqlite3)
#   ZDS_DRILL_TIMESTAMP  restore to this RFC 3339 time instead of the latest
#   ZDS_DRILL_LIVE_URL   live PDS origin; compare each repo's rev with it
#   ZDS_DRILL_BIN        zds binary to boot against the restored copy
#   ZDS_DRILL_KEEP=1     keep the scratch directory afterwards
set -eu

: "${ZDS_LITESTREAM_BUCKET:?set ZDS_LITESTREAM_BUCKET to the replica bucket}"
export ZDS_LITESTREAM_PATH="${ZDS_LITESTREAM_PATH:-zds.sqlite3}"
export AWS_ENDPOINT_URL_S3="${AWS_ENDPOINT_URL_S3:-}"
export AWS_REGION="${AWS_REGION:-auto}"

work=$(mktemp -d "${TMPDIR:-/tmp}/zds-restore-drill.XXXXXX")
db="$work/zds.sqlite3"
cleanup() {
  status=$?
  if [ -n "${server_pid:-}" ]; then
    kill "$server_pid" 2>/dev/null || true
    wait "$server_pid" 2>/dev/null || true
  fi
  if [ "${ZDS_DRILL_KEEP:-0}" = 1 ] || [ "$status" -ne 0 ]; then
    echo "scratch directory kept: $work" >&2
  else
    rm -rf "$work"
  fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

# The same settings as config/litestream.yml, written out so the drill does
# not depend on the container's copy of that file.
config="$work/litestream.yml"
cat >"$config" <<EOF
dbs:
  - path: $db
    replica:
      type: s3
      bucket: \${ZDS_LITESTREAM_BUCKET}
      path: \${ZDS_LITESTREAM_PATH}
      endpoint: \${AWS_ENDPOINT_URL_S3}
      region: \${AWS_REGION}
EOF

started=$(date +%s)
if [ -n "${ZDS_DRILL_TIMESTAMP:-}" ]; then
  litestream restore -config "$config" -integrity-check full -timestamp "$ZDS_DRILL_TIMESTAMP" "$db"
else
  litestream restore -config "$config" -integrity-check full "$db"
fi
echo "restored $(wc -c <"$db" | tr -d ' ') bytes in $(($(date +%s) - started))s"

q() { sqlite3 -batch -noheader "$db" "$1"; }

fk=$(q "PRAGMA foreign_key_check" | head -5)
if [ -n "$fk" ]; then
  echo "foreign key violations in the restored copy:" >&2
  echo "$fk" >&2
  exit 1
fi

for table in accounts records commits blocks seq_events blobs record_blobs; do
  printf '%-14s %s\n' "$table" "$(q "SELECT count(*) FROM $table")"
done
echo "newest event seq=$(q "SELECT coalesce(max(seq), 0) FROM seq_events")"

# A restored copy is only as fresh as the last sync, so compare each repo's
# newest commit with what the live host says now.
if [ -n "${ZDS_DRILL_LIVE_URL:-}" ]; then
  q "SELECT a.did, coalesce((SELECT rev FROM commits c WHERE c.did = a.did ORDER BY seq DESC LIMIT 1), '') FROM accounts a" \
    >"$work/restored-revs"
  cursor=""
  : >"$work/live-revs"
  while :; do
    page=$(curl -fsS -G "${ZDS_DRILL_LIVE_URL%/}/xrpc/com.atproto.sync.listRepos" \
      --data-urlencode limit=1000 ${cursor:+--data-urlencode "cursor=$cursor"})
    printf %s "$page" | jq -r '.repos[] | "\(.did)|\(.rev)"' >>"$work/live-revs"
    cursor=$(printf %s "$page" | jq -r '.cursor // empty')
    [ -n "$cursor" ] || break
  done
  sort -o "$work/restored-revs" "$work/restored-revs"
  sort -o "$work/live-revs" "$work/live-revs"
  total=$(wc -l <"$work/live-revs" | tr -d ' ')
  same=$(comm -12 "$work/restored-revs" "$work/live-revs" | wc -l | tr -d ' ')
  echo "repos at the live rev: $same of $total"
fi

# Booting proves this build can open and migrate the copy. Everything that
# could reach the outside is off: no crawlers, mail kept in the outbox, no
# permissioned-data workers, no blob mirror, and an empty scratch blobstore.
if [ -n "${ZDS_DRILL_BIN:-}" ]; then
  port="${ZDS_DRILL_PORT:-2599}"
  mkdir -p "$work/blobs"
  # The copy must never write to the live host's bucket: with the blob mirror
  # on, its blob collection would delete the bucket's copies.
  env -u ZDS_BLOB_MIRROR_BUCKET -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY -u AWS_SESSION_TOKEN \
    ZDS_DB="$db" ZDS_BLOBSTORE_PATH="$work/blobs" ZDS_HOST=127.0.0.1 ZDS_PORT="$port" \
    ZDS_PUBLIC_URL="http://127.0.0.1:$port" ZDS_SERVER_DID="did:web:127.0.0.1" \
    ZDS_CRAWLERS="" ZDS_MAIL_PROVIDER=outbox ZDS_PERMISSIONED_DATA=false \
    ZDS_JWT_SECRET="restore-drill-$(date +%s)-not-a-production-secret" \
    "$ZDS_DRILL_BIN" >"$work/zds.log" 2>&1 &
  server_pid=$!
  tries=0
  until curl -fsS -m 2 "http://127.0.0.1:$port/xrpc/_health" >/dev/null 2>&1; do
    tries=$((tries + 1))
    if [ "$tries" -ge 60 ] || ! kill -0 "$server_pid" 2>/dev/null; then
      echo "zds did not come up on the restored copy; log: $work/zds.log" >&2
      exit 1
    fi
    sleep 1
  done
  repos=$(curl -fsS "http://127.0.0.1:$port/xrpc/com.atproto.sync.listRepos?limit=1000" | jq '.repos | length')
  echo "zds booted on the restored copy and lists $repos repos"
fi

echo "restore drill passed"