Repository Public
1#!/bin/zsh2# Proves a backup can be restored: loads the latest dump into a scratch database, points a scratch data directory at3# the backed-up objects, and re-hashes EVERY object after decryption (cli verify --deep). Touches nothing in production.4# Usage: scripts/restore-test.sh [backup-dir] (run from the app directory; needs the master key)5set -euo pipefail6export PATH=/opt/homebrew/bin:/opt/homebrew/opt/postgresql@17/bin:/usr/bin:/bin7OUT=${1:-${SPD_BACKUP_DIR:-$HOME/backups/sp-drive}}8KEY=${SPD_MASTER_KEY_FILE:-$HOME/.config/sp-drive/master.key}9DUMP=$(ls -1t "$OUT"/db/sp-drive-*.dump.enc | head -1)10SCRATCH_DB=sp_drive_restore_test11SCRATCH=$(mktemp -d /tmp/spd-restore.XXXXXX)12trap 'dropdb -h 127.0.0.1 --if-exists $SCRATCH_DB; rm -rf "$SCRATCH"' EXIT13echo "restoring $DUMP"14dropdb -h 127.0.0.1 --if-exists $SCRATCH_DB; createdb -h 127.0.0.1 $SCRATCH_DB15openssl enc -d -aes-256-ctr -pbkdf2 -iter 200000 -pass "file:$KEY" -in "$DUMP" | pg_restore --no-owner -h 127.0.0.1 -d $SCRATCH_DB16mkdir -p "$SCRATCH/data"; ln -s "$OUT/blobs" "$SCRATCH/data/blobs"17CLI=dist/cli.mjs; RUN=(node $CLI); [[ -f $CLI ]] || RUN=(pnpm -s exec tsx services/api/src/cli.ts)18DATABASE_URL=postgres://127.0.0.1/$SCRATCH_DB SPD_DATA_DIR="$SCRATCH/data" SPD_MASTER_KEY_FILE="$KEY" SPD_ORIGIN=${SPD_ORIGIN:-https://drive.spboucher.ai} SPD_RP_ID=${SPD_RP_ID:-drive.spboucher.ai} "${RUN[@]}" verify --deep19echo "files in restored metadata: $(psql -h 127.0.0.1 -At -d $SCRATCH_DB -c 'select count(*) from files')"20echo "RESTORE TEST OK"21