randomayzer/tools/db-restore-check.sh

16 lines
1.1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
: "${RANDOMAYZER_ENV_FILE:?Set absolute 0600 env file path}"
[[ $(stat -c '%a' "$RANDOMAYZER_ENV_FILE") == 600 ]] || { echo 'Environment file must have mode 0600' >&2; exit 1; }
[[ $# == 1 && -f $1 ]] || { echo 'Usage: tools/db-restore-check.sh backup.dump' >&2; exit 1; }
root=$(cd "$(dirname "$0")/.." && pwd)
args=(--env-file "$RANDOMAYZER_ENV_FILE" -f "$root/compose.production.yml")
export RANDOMAYZER_ENV_FILE
# A fresh temporary DB, never the production database. Avoid concurrent drill collisions.
drill="randomayzer_restore_${$}"
cleanup() { docker compose "${args[@]}" exec -T postgres dropdb -U randomayzer --if-exists "$drill" > /dev/null; }
trap cleanup EXIT
docker compose "${args[@]}" exec -T postgres createdb -U randomayzer "$drill"
docker compose "${args[@]}" exec -T postgres pg_restore -U randomayzer -d "$drill" --exit-on-error --no-owner --no-privileges < "$1"
docker compose "${args[@]}" exec -T postgres psql -U randomayzer -d "$drill" -v ON_ERROR_STOP=1 -c 'SELECT COUNT(*) FROM "_prisma_migrations";' -c 'SELECT COUNT(*) FROM "Giveaway";'
echo 'PASS backup restored to clean drill database'