deadhead-space-freight-tycoon/deploy/backup/restore.sh

16 lines
644 B
Bash
Executable File

#!/bin/sh
# Restores a dump into a database (Step 13.3):
# restore.sh [dump] [database]
# Defaults: the newest dump in /backups, into a new database "deadhead_restore"
# (created if missing), so the live database is untouched. To replace the live
# database, stop the server first and pass its name.
set -eu
dump="${1:-$(ls -1 /backups/deadhead-*.dump | sort | tail -n 1)}"
database="${2:-deadhead_restore}"
if ! psql -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$database'" | grep -q 1; then
createdb "$database"
fi
pg_restore --clean --if-exists --no-owner --dbname="$database" "$dump"
echo "restore: $dump -> $database"