-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-restore.sh
More file actions
55 lines (47 loc) · 2.05 KB
/
Copy pathdb-restore.sh
File metadata and controls
55 lines (47 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
# Chateando - PostgreSQL restore helper
#
# Restores a pg_dump produced by ops/db-backup.sh into the running `postgres`
# service. The target database is dropped and recreated; existing data is lost.
#
# Usage:
# ./ops/db-restore.sh backups/chateando_20260528T120000Z.dump
#
# Environment overrides:
# CHATEANDO_COMPOSE_FILE Docker compose file (default: docker-compose.yml)
# CHATEANDO_DB_SERVICE Compose service running Postgres (default: postgres)
# CHATEANDO_DB_NAME Database name (default: chateando)
# CHATEANDO_DB_USER Postgres role (default: chateando)
# CHATEANDO_RESTORE_FORCE Set to 1 to skip the confirmation prompt.
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <dump-file>" >&2
exit 64
fi
dump_file="$1"
compose_file="${CHATEANDO_COMPOSE_FILE:-docker-compose.yml}"
db_service="${CHATEANDO_DB_SERVICE:-postgres}"
db_name="${CHATEANDO_DB_NAME:-chateando}"
db_user="${CHATEANDO_DB_USER:-chateando}"
if [[ ! -f "$dump_file" ]]; then
echo "[restore] ERROR: $dump_file does not exist." >&2
exit 1
fi
if [[ "${CHATEANDO_RESTORE_FORCE:-0}" != "1" ]]; then
echo "[restore] About to DROP and RECREATE '${db_name}' on service '${db_service}'."
echo "[restore] Source dump: ${dump_file}"
read -r -p "Type the database name to confirm: " confirm
if [[ "$confirm" != "$db_name" ]]; then
echo "[restore] confirmation mismatch; aborting." >&2
exit 1
fi
fi
echo "[restore] dropping and recreating database..."
docker compose -f "$compose_file" exec -T "$db_service" psql -v ON_ERROR_STOP=1 \
--username="$db_user" --dbname=postgres -c "DROP DATABASE IF EXISTS ${db_name};"
docker compose -f "$compose_file" exec -T "$db_service" psql -v ON_ERROR_STOP=1 \
--username="$db_user" --dbname=postgres -c "CREATE DATABASE ${db_name} OWNER ${db_user};"
echo "[restore] restoring from ${dump_file}..."
docker compose -f "$compose_file" exec -T "$db_service" \
pg_restore --no-owner --no-privileges --username="$db_user" --dbname="$db_name" < "$dump_file"
echo "[restore] done."