Repository Public
1#!/bin/bash2# Restores a Sheet backup.3# scripts/restore.sh db <sheet-….dump.enc> <postgres URL of an EMPTY target database>4# scripts/restore.sh storage <sheet-….tar.enc> <target STORAGE_ROOT (empty or new directory)>5# Then point .env at the restored database and storage and start the processes. See docs/BACKUP_RESTORE.md.6set -euo pipefail7cd "$(dirname "$0")/.."8if [ -f .env ]; then9 while IFS='=' read -r k v; do10 case "$k" in BACKUP_PASSPHRASE_FILE) v="${v%\"}"; v="${v#\"}"; [ -n "${!k:-}" ] || export "$k=$v" ;; esac11 done < .env12fi13export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:$PATH"14: "${BACKUP_PASSPHRASE_FILE:?BACKUP_PASSPHRASE_FILE missing}"15dec() { openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass "file:$BACKUP_PASSPHRASE_FILE" -in "$1"; }16check() {17 [ -f "$1.hmac" ] || { echo "no $1.hmac next to the archive: refusing to restore an archive whose integrity cannot be checked (FORCE=1 to override)" >&2; [ "${FORCE:-}" = 1 ] || exit 1; return 0; }18 [ "$(openssl dgst -sha256 -hmac "$(cat "$BACKUP_PASSPHRASE_FILE")" -r "$1" | cut -d' ' -f1)" = "$(cat "$1.hmac")" ] || { echo "INTEGRITY CHECK FAILED for $1: the archive was altered or the passphrase is wrong" >&2; exit 1; }19}20case "${1:-}" in21 db)22 file="${2:?backup file}"; target="${3:?target database URL}"23 check "$file"; dec "$file" | pg_restore --no-owner --no-privileges --exit-on-error -d "$target"24 echo "database restored from $file" ;;25 storage)26 file="${2:?backup file}"; dest="${3:?target STORAGE_ROOT}"27 mkdir -p "$dest"; chmod 700 "$dest"28 check "$file"; dec "$file" | tar -C "$dest" -xf -29 echo "storage restored into $dest" ;;30 *) sed -n 2,5p "$0"; exit 2 ;;31esac32