Spaces:
Running

File size: 1,699 Bytes
fd5dd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# Restore a backup made by deploy/scripts/backup.sh into an EMPTY database.
#
#   WP_RESTORE_DATABASE_URL=postgresql://admin:...@host:5432/workpilot_restore \
#     deploy/scripts/restore.sh backups/workpilot-20261205T020000Z.dump
#
# Before running:
# 1. The roles workpilot_owner and workpilot_app must exist on the target server
#    (python -m core.db.bootstrap, or deploy/sql/bootstrap_managed.sql on managed Postgres).
#    Ownership and grants in the dump refer to them by name.
# 2. The target database exists, is empty, and is owned by workpilot_owner; the pgvector
#    extension is available (the dump re-creates it with CREATE EXTENSION).
# 3. Stop the api and worker (or point them elsewhere): never restore under live traffic.
#
# After running: verify (docs/deployment.md, "Restore drill"):
#   python -m core.cli check-deploy                     # owner role; also proves connectivity
#   python -m core.cli verify-audit --tenant <id>       # hash chains intact
# then repoint WP_DATABASE_URL / WP_OWNER_DATABASE_URL and start the services.
set -euo pipefail

DUMP="${1:?usage: restore.sh <file.dump>}"
: "${WP_RESTORE_DATABASE_URL:?set WP_RESTORE_DATABASE_URL (superuser or the database owner)}"
PG_RESTORE="${PG_RESTORE:-pg_restore}"
JOBS="${WP_RESTORE_JOBS:-2}"

"$PG_RESTORE" --list "$DUMP" > /dev/null  # refuse a corrupt/foreign file before touching the DB
# --exit-on-error: a partial restore is worse than none. --no-owner is NOT used: the RLS
# design depends on workpilot_owner owning every table and workpilot_app owning none.
"$PG_RESTORE" --exit-on-error --jobs="$JOBS" --no-password --dbname="$WP_RESTORE_DATABASE_URL" "$DUMP"
echo "restored $DUMP"