SPB Git

Repository Public

restore.sh 1.7 KB 31 lines shellscript

1#!/bin/bash2# CLIP restore drill / disaster recovery.3#   scripts/restore.sh <backup-dir> [target-database]4# Restores the newest dump into a scratch database (default: clip_restore_check), verifies row counts and that5# every published film's media exists in the backed-up asset store. It never touches the live database.6# To recover for real: restore into a new database the same way, point DATABASE_URL at it, rsync7# <backup-dir>/assets/objects/ back into $ASSET_ROOT/objects/, then restart clip-web and clip-worker.8set -euo pipefail9export PATH="/opt/homebrew/opt/postgresql@17/bin:/opt/homebrew/bin:/usr/bin:/bin:$PATH"10BACKUP_DIR="${1:?usage: restore.sh <backup-dir> [target-database]}"11TARGET="${2:-clip_restore_check}"12DUMP="$(ls -1t "$BACKUP_DIR"/db/clip-*.dump | head -1)"13[ -f "$DUMP" ] || { echo "no dump found in $BACKUP_DIR/db"; exit 1; }1415case "$TARGET" in clip|clip_dev) echo "refusing to restore over a live database ($TARGET)"; exit 1;; esac16dropdb --if-exists "$TARGET"17createdb "$TARGET"18pg_restore --no-owner --dbname="$TARGET" "$DUMP"1920FILMS="$(psql -d "$TARGET" -Atc "SELECT COUNT(*) FROM films WHERE status = 'published'")"21JOBS="$(psql -d "$TARGET" -Atc "SELECT COUNT(*) FROM provider_jobs")"22MISSING=023while IFS='|' read -r sha ext; do24  [ -z "$sha" ] && continue25  f="$BACKUP_DIR/assets/objects/${sha:0:2}/${sha:2:2}/$sha.$ext"26  [ -f "$f" ] || { MISSING=$((MISSING + 1)); echo "missing asset $sha.$ext"; }27done < <(psql -d "$TARGET" -Atc "SELECT master_sha, 'mp4' FROM films WHERE status = 'published' UNION ALL SELECT poster_sha, 'jpg' FROM films WHERE status = 'published'")2829echo "restored $(basename "$DUMP") into $TARGET: $FILMS published film(s), $JOBS provider job(s), $MISSING missing asset(s)"30[ "$MISSING" -eq 0 ]31
Files main
Menu

Theme