#!/bin/bash # Restore script for recipes database set -e SERVICE_NAME="recipes" BACKUP_FILE="$1" if [ -z "$BACKUP_FILE" ]; then echo "Usage: $0 " 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.RECIPES_DB_USER}' | base64 -d) DB_NAME=$(kubectl get configmap bakery-config -n bakery-ia -o jsonpath='{.data.RECIPES_DB_NAME}') # Get the pod name POD_NAME=$(kubectl get pods -n bakery-ia -l app.kubernetes.io/name=recipes-db -o jsonpath='{.items[0].metadata.name}') if [ -z "$POD_NAME" ]; then echo "Error: Could not find recipes 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