infra: add PostgreSQL daily backup via pg_dump with rotation
PR Checks / test-and-build (pull_request) Successful in 6m24s
PR Checks / test-and-build (pull_request) Successful in 6m24s
- Add db-backup service to compose.yaml (postgres:17-alpine + cron) - Add pgbackups volume for backup storage - Add scripts/restore.sh for manual restore from latest backup - Update .env.example with BACKUP_RETENTION_DAYS and BACKUP_VOLUME_NAME - Document backup/restore flow in README Bump version -> 1.15.0 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
# GM-Relay PostgreSQL Backup Restore Script
|
||||
# Usage: ./scripts/restore.sh [backup_file]
|
||||
# If no file is provided, uses the most recent backup.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
|
||||
# Check required env
|
||||
if [ -z "${POSTGRES_PASSWORD:-}" ]; then
|
||||
if [ -f "${PROJECT_ROOT}/.env" ]; then
|
||||
export $(grep -v '^#' "${PROJECT_ROOT}/.env" | xargs) 2>/dev/null || true
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "${POSTGRES_PASSWORD:-}" ]; then
|
||||
echo "ERROR: POSTGRES_PASSWORD is not set. Please set it in your environment or .env file."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BACKUP_DIR="${PROJECT_ROOT}/backups"
|
||||
|
||||
# Determine backup file
|
||||
if [ $# -ge 1 ]; then
|
||||
BACKUP_FILE="$1"
|
||||
else
|
||||
BACKUP_FILE=$(find "${BACKUP_DIR}" -name 'gmrelay_db_*.sql.gz' -type f -printf '%T+ %p\n' 2>/dev/null | sort -r | head -n1 | cut -d' ' -f2-)
|
||||
if [ -z "${BACKUP_FILE}" ]; then
|
||||
echo "ERROR: No backup files found in ${BACKUP_DIR}."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -f "${BACKUP_FILE}" ]; then
|
||||
echo "ERROR: Backup file not found: ${BACKUP_FILE}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=================================================="
|
||||
echo " GM-Relay PostgreSQL Restore"
|
||||
echo "=================================================="
|
||||
echo ""
|
||||
echo "Backup file: ${BACKUP_FILE}"
|
||||
echo "Database: gmrelay_db"
|
||||
echo "User: gmrelay"
|
||||
echo ""
|
||||
read -p "This will OVERWRITE the current database. Are you sure? [y/N] " CONFIRM
|
||||
|
||||
if [[ ! "${CONFIRM}" =~ ^[Yy]$ ]]; then
|
||||
echo "Restore cancelled."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Restoring database from ${BACKUP_FILE}..."
|
||||
|
||||
# Restore using docker compose exec to leverage the running postgres container
|
||||
docker compose -f "${PROJECT_ROOT}/compose.yaml" exec -T db psql \
|
||||
-U gmrelay \
|
||||
-d gmrelay_db \
|
||||
-c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" 2>/dev/null || true
|
||||
|
||||
gunzip -c "${BACKUP_FILE}" | docker compose -f "${PROJECT_ROOT}/compose.yaml" exec -T db psql \
|
||||
-U gmrelay \
|
||||
-d gmrelay_db
|
||||
|
||||
echo ""
|
||||
echo "=================================================="
|
||||
echo " Restore completed successfully!"
|
||||
echo "=================================================="
|
||||
Reference in New Issue
Block a user