Files
bakery-ia/infrastructure/scripts/production_restore.sh

48 lines
1.3 KiB
Bash
Raw Normal View History

2025-09-30 08:12:45 +02:00
#!/bin/bash
# Restore script for production database
set -e
SERVICE_NAME="production"
BACKUP_FILE="$1"
if [ -z "$BACKUP_FILE" ]; then
echo "Usage: $0 <backup_file>"
echo "Example: $0 ./backups/${SERVICE_NAME}_backup_20240101_120000.sql"
exit 1
fi
if [ ! -f "$BACKUP_FILE" ]; then
echo "Error: Backup file not found: $BACKUP_FILE"
exit 1
fi
echo "Starting restore for $SERVICE_NAME database from: $BACKUP_FILE"
# Get database credentials from Kubernetes secrets
DB_USER=$(kubectl get secret database-secrets -n bakery-ia -o jsonpath='{.data.PRODUCTION_DB_USER}' | base64 -d)
DB_NAME=$(kubectl get configmap bakery-config -n bakery-ia -o jsonpath='{.data.PRODUCTION_DB_NAME}')
# Get the pod name
POD_NAME=$(kubectl get pods -n bakery-ia -l app.kubernetes.io/name=production-db -o jsonpath='{.items[0].metadata.name}')
if [ -z "$POD_NAME" ]; then
echo "Error: Could not find production database pod"
exit 1
fi
# Check if file is compressed
if [[ "$BACKUP_FILE" == *.gz ]]; then
echo "Decompressing backup file..."
zcat "$BACKUP_FILE" | kubectl exec -i "$POD_NAME" -n bakery-ia -- psql -U "$DB_USER" "$DB_NAME"
else
kubectl exec -i "$POD_NAME" -n bakery-ia -- psql -U "$DB_USER" "$DB_NAME" < "$BACKUP_FILE"
fi
if [ $? -eq 0 ]; then
echo "Restore completed successfully"
else
echo "Restore failed"
exit 1
fi