psql -U postgres with no -d restores into the 'postgres' maintenance database unless the app DB happens to carry that name. Fourth required argument passes -d <db-name>; ON_ERROR_STOP=1 so a mid-stream failure aborts instead of half-restoring silently. Found by the prod-migration plan final review (finding #6); drilled in Task 9 before cutover day. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
12 lines
1.1 KiB
Bash
Executable file
12 lines
1.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# usage: ./scripts/restore-db.sh <artifact.sql.gz> <target-host> <postgres-container> <db-name>
|
|
# Streams a Coolify Postgres backup artifact into the target container over
|
|
# tailnet SSH. Refuses to run without explicit confirmation of the target.
|
|
set -euo pipefail
|
|
ARTIFACT="${1:?backup artifact (.sql.gz)}"; HOST="${2:?target host (tailnet name)}"; CONTAINER="${3:?postgres container name}"; DBNAME="${4:?target database name (docker exec <container> psql -U postgres -lqt to list)}"
|
|
echo "About to RESTORE ${ARTIFACT} into database ${DBNAME} in ${CONTAINER} on ${HOST} — this overwrites that database."
|
|
read -r -p "Type the target host to confirm: " CONFIRM
|
|
[ "$CONFIRM" = "$HOST" ] || { echo "confirmation mismatch; aborting"; exit 1; }
|
|
# shellcheck disable=SC2029 # intentional: $CONTAINER/$DBNAME are local vars, expand client-side before they reach the remote shell
|
|
gunzip -c "$ARTIFACT" | ssh "root@${HOST}" "docker exec -i ${CONTAINER} psql -U postgres -d ${DBNAME} -v ON_ERROR_STOP=1"
|
|
echo "restore complete — run the verification checks in runbooks/restore-drill.md step 4"
|