gendesign/ops/restore.sh
lekss361 4f034bd86b ops: alembic baseline, pre-commit, TIGER cleanup, pg_dump scripts
- backend/alembic/* — alembic infra (env.py, script.py.mako). versions/ empty
  for now; first migration goes in Stage 2a when models are finalized.
- backend/Dockerfile: bake alembic.ini + alembic/ into the image so
  `docker compose exec backend alembic upgrade head` works in prod.
- backend/db/init/99_drop_unused_extensions.sql + bind-mount in both compose
  files: drops postgis-image's TIGER/topology/fuzzystrmatch on fresh volumes.
- .pre-commit-config.yaml + pre-commit in dev deps: ruff/prettier on commit
  to stop CI failures like UP035 from leaking out.
- ops/backup.sh, ops/restore.sh: pg_dump cron script with optional Selectel S3
  upload. 7-day local retention. Restore guard: requires RESTORE_CONFIRM=yes.
- Makefile: new targets `make migration NAME=...`, `make pre-commit-install`.
- backend/.env.example: SENTRY_DSN comment with sentry.io reference.
2026-04-26 13:08:51 +03:00

43 lines
1.3 KiB
Bash

#!/usr/bin/env bash
# Restore a Postgres dump produced by backup.sh.
#
# Usage:
# ops/restore.sh /opt/gendesign/backups/gendesign_20260427_033000.sql.gz
#
# Refuses to run unless RESTORE_CONFIRM=yes is set. This is destructive —
# the dump uses --clean --if-exists, so it DROPs existing tables before recreating.
#
# Example:
# RESTORE_CONFIRM=yes ops/restore.sh ./gendesign_20260427_033000.sql.gz
set -euo pipefail
if [[ "${RESTORE_CONFIRM:-no}" != "yes" ]]; then
echo "Refusing to restore — set RESTORE_CONFIRM=yes to proceed."
echo "This will DROP existing tables and recreate from the dump."
exit 1
fi
DUMP_FILE="${1:-}"
if [[ -z "$DUMP_FILE" || ! -f "$DUMP_FILE" ]]; then
echo "Usage: $0 /path/to/dump.sql.gz"
exit 2
fi
COMPOSE_DIR=/opt/gendesign
COMPOSE_FILE=docker-compose.prod.yml
cd "$COMPOSE_DIR"
DB_USER=$(docker compose -f "$COMPOSE_FILE" exec -T postgres printenv POSTGRES_USER 2>/dev/null | tr -d '\r' || true)
DB_NAME=$(docker compose -f "$COMPOSE_FILE" exec -T postgres printenv POSTGRES_DB 2>/dev/null | tr -d '\r' || true)
DB_USER=${DB_USER:-gendesign}
DB_NAME=${DB_NAME:-gendesign}
echo "Restoring $DUMP_FILE${DB_NAME} as ${DB_USER}"
gunzip -c "$DUMP_FILE" | \
docker compose -f "$COMPOSE_FILE" exec -T postgres \
psql -U "$DB_USER" -d "$DB_NAME" -v ON_ERROR_STOP=1
echo "Restore complete."