Files
Moonbase/scripts/db-restore.sh
2026-06-15 21:45:09 -05:00

36 lines
870 B
Bash
Executable File

#!/bin/sh
# Usage: ./scripts/db-restore.sh <backup-file>
# Uses DATABASE_URL from environment (or .env if present).
# WARNING: drops and recreates the public schema — all existing data is replaced.
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <backup-file.sql>"
exit 1
fi
INFILE="$1"
if [ ! -f "$INFILE" ]; then
echo "Error: file not found: $INFILE"
exit 1
fi
# Load .env if present and DATABASE_URL not already set
if [ -z "$DATABASE_URL" ] && [ -f ".env" ]; then
export $(grep -v '^#' .env | grep DATABASE_URL | xargs)
fi
if [ -z "$DATABASE_URL" ]; then
echo "Error: DATABASE_URL is not set."
exit 1
fi
echo "Restoring from $INFILE ..."
echo " -> dropping and recreating public schema..."
psql "$DATABASE_URL" -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
echo " -> loading backup..."
psql "$DATABASE_URL" -f "$INFILE" -q
echo "Done."