Files
2026-01-16 19:04:48 +02:00

77 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Database Restore Script for Wedding App
# This script restores a backup of the PostgreSQL database
# Usage: ./restore-db.sh <backup-file>
set -e # Exit on error
if [ $# -eq 0 ]; then
echo "Usage: $0 <backup-file>"
echo "Example: $0 /mnt/synology/wedding-app/backups/wedding_app_backup_20250115_030000.sql.gz"
exit 1
fi
BACKUP_FILE="$1"
if [ ! -f "$BACKUP_FILE" ]; then
echo "Error: Backup file not found: $BACKUP_FILE"
exit 1
fi
# Database connection from environment
if [ -z "$DATABASE_URL" ]; then
echo "Error: DATABASE_URL environment variable is not set"
exit 1
fi
DB_URL="$DATABASE_URL"
# Extract database name from URL
DB_NAME=$(echo "$DB_URL" | sed -n 's/.*\/\([^?]*\).*/\1/p')
if [ -z "$DB_NAME" ]; then
echo "Error: Could not extract database name from DATABASE_URL"
exit 1
fi
echo "WARNING: This will restore the database from backup: $BACKUP_FILE"
echo "Database: $DB_NAME"
echo ""
read -p "Are you sure you want to continue? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Restore cancelled."
exit 0
fi
echo "Starting database restore..."
# Create a backup before restoring (safety measure)
SAFETY_BACKUP="/tmp/wedding_app_safety_backup_$(date +%Y%m%d_%H%M%S).sql.gz"
echo "Creating safety backup to: $SAFETY_BACKUP"
if command -v pg_dump &> /dev/null; then
pg_dump "$DB_URL" | gzip > "$SAFETY_BACKUP"
elif command -v docker &> /dev/null; then
CONTAINER_NAME="${DB_CONTAINER_NAME:-postgres}"
docker exec "$CONTAINER_NAME" pg_dump -U postgres "$DB_NAME" | gzip > "$SAFETY_BACKUP"
fi
echo "Safety backup created. Proceeding with restore..."
# Restore from backup
if command -v psql &> /dev/null; then
# Direct psql (if running on host)
gunzip -c "$BACKUP_FILE" | psql "$DB_URL"
elif command -v docker &> /dev/null; then
# Docker-based restore
CONTAINER_NAME="${DB_CONTAINER_NAME:-postgres}"
gunzip -c "$BACKUP_FILE" | docker exec -i "$CONTAINER_NAME" psql -U postgres "$DB_NAME"
else
echo "Error: Neither psql nor docker found. Cannot restore backup."
exit 1
fi
echo "Database restore completed successfully!"
echo "Safety backup saved at: $SAFETY_BACKUP"