Files
mtusi2026lab2/scripts/backup_restore.sh

61 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
RESTORE_VOLUME_NAME="mtusi2026lab2_restore_$(date +%Y%m%d%H%M%S)_$$"
export RESTORE_VOLUME_NAME
work="$(mktemp -d)"
RESTORE_DATABASE_URL_FILE="$PWD/secrets/database_url_restore_${RESTORE_VOLUME_NAME}"
export RESTORE_DATABASE_URL_FILE
source_stopped=0
cleanup() {
if [ "$source_stopped" -eq 1 ]; then
docker compose --env-file .env start postgres >/dev/null
fi
docker compose --env-file .env -f compose.yaml -f compose.restore.yaml stop api_restore postgres_restore >/dev/null 2>&1 || true
docker compose --env-file .env -f compose.yaml -f compose.restore.yaml rm -sf api_restore postgres_restore >/dev/null 2>&1 || true
rm -f "$RESTORE_DATABASE_URL_FILE"
rm -rf "$work"
}
trap cleanup EXIT
# Derive a private URL for the new service without printing the password.
python3 - "$RESTORE_DATABASE_URL_FILE" <<'PY'
from pathlib import Path
import sys
from urllib.parse import urlsplit, urlunsplit
url = urlsplit(Path('secrets/database_url').read_text().strip())
assert url.hostname == 'postgres', 'expected source hostname postgres'
host = url.netloc.replace('@postgres:', '@postgres_restore:')
assert host != url.netloc
Path(sys.argv[1]).write_text(urlunsplit(url._replace(netloc=host)))
PY
chmod 600 "$RESTORE_DATABASE_URL_FILE"
compose=(docker compose --env-file .env -f compose.yaml -f compose.restore.yaml)
docker compose --env-file .env up -d --build --wait api >/dev/null
docker compose --env-file .env exec -T -e PYTHONPATH=/app api python student-tests/test_restore.py prepare > "$work/expected.json"
chmod 600 "$work/expected.json"
echo "UTC start: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Source: mtusi2026lab2_postgres_data; new volume: $RESTORE_VOLUME_NAME"
backup_start=$(date +%s)
docker compose --env-file .env exec -T postgres pg_dump -Fc -U messenger -d messenger > "$work/backup.dump"
backup_end=$(date +%s)
echo "pg_dump custom-format backup: $(wc -c < "$work/backup.dump") bytes, $((backup_end-backup_start)) seconds"
recovery_start=$(date +%s)
"${compose[@]}" up -d --wait postgres_restore >/dev/null
restore_start=$(date +%s)
"${compose[@]}" exec -T postgres_restore pg_restore --exit-on-error -U messenger -d messenger --no-owner --no-acl < "$work/backup.dump"
restore_end=$(date +%s)
echo "pg_restore into fresh volume: $((restore_end-restore_start)) seconds"
"${compose[@]}" up -d --build --wait api_restore >/dev/null
recovery_end=$(date +%s)
echo "New PostgreSQL + pg_restore + restored API ready: $((recovery_end-recovery_start)) seconds"
docker compose --env-file .env stop postgres >/dev/null
source_stopped=1
echo "Source PostgreSQL stopped; verification uses only postgres_restore and api_restore"
"${compose[@]}" exec -T -e PYTHONPATH=/app api_restore python student-tests/test_restore.py verify < "$work/expected.json"
echo "UTC verified: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Recovery point: pg_dump snapshot; writes committed afterward are outside this backup"