#!/usr/bin/env bash set -euo pipefail # ============================================================================== # MongoDB Volume Restore Script (Docker Swarm) # ============================================================================== # # PURPOSE: # Restores the complete mongo-volume directory from a volume backup tarball. # The MongoDB service is scaled down during the restore to ensure consistency. # # WHEN TO USE: # - For Docker Swarm deployments (not standalone Docker) # - To restore from backups created by db-backup--swarm.sh # - For disaster recovery scenarios # - When you need to restore the complete MongoDB data directory # # RESTORE TYPE: # - Offline (cold) restore - DB service is scaled to 0 during restore # - Volume-level restore - entire mongo-volume directory # - Extracts from compressed tarball (.tar.gz) # # INPUT: # - Requires backup filename as parameter # - Looks for file in: ./mongo-backup/ # - Optional: --pre-backup flag to create safety backup before restore # # USAGE: # ./db-restore-from-backup--swarm.sh # ./db-restore-from-backup--swarm.sh mongo-volume-backup-2025-11-26-14-30.tar.gz # ./db-restore-from-backup--swarm.sh --pre-backup mongo-volume-backup-2025-11-26-14-30.tar.gz # # WARNING: # This will COMPLETELY REPLACE the mongo-volume directory contents! # Use --pre-backup flag to create a safety backup before restore. # # NOTE: # The MongoDB service will be scaled down to 0 and then back up to 1. # Expect downtime during the restore process. # # ============================================================================== # Configuration MONGO_SERVICE="utility-bills-tracker_mongo" # Parse command line options DO_PRE_BACKUP="" while [[ $# -gt 0 ]]; do case $1 in --do-pre-backup=*) DO_PRE_BACKUP="${1#*=}" shift ;; *) TIMESTAMP="$1" shift ;; esac done # Usage: ./restore.sh [options] # Example: ./restore.sh 2025-08-24-14-30 # Options: --do-pre-backup=true/false (skip interactive prompt) if [ -z "${TIMESTAMP:-}" ]; then echo "Usage: $0 [options] " echo "Example: $0 2025-08-24-14-30" echo "" echo "Options:" echo " --do-pre-backup=true Skip interactive prompt, create pre-restore backup" echo " --do-pre-backup=false Skip interactive prompt, no pre-restore backup" echo "" echo "Available backups:" ls -1t mongo-backup/mongo-volume-backup-*.tar.gz 2>/dev/null | sed 's/.*mongo-volume-backup-\(.*\)\.tar\.gz/ \1/' || echo " No backups found" exit 1 fi BACKUP_DIR="${BACKUP_DIR:-mongo-backup}" BACKUP_FILE="$BACKUP_DIR/mongo-volume-backup-$TIMESTAMP.tar.gz" # Check if backup file exists if [ ! -f "$BACKUP_FILE" ]; then echo "Error: Backup file '$BACKUP_FILE' not found" echo "" echo "Available backups:" ls -1t "$BACKUP_DIR"/mongo-volume-backup-*.tar.gz 2>/dev/null | sed 's/.*mongo-volume-backup-\(.*\)\.tar\.gz/ \1/' || echo " No backups found" exit 1 fi echo "Restoring from backup: $BACKUP_FILE" echo "WARNING: This will replace the current mongo-volume data!" read -p "Are you sure you want to continue? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Restore cancelled" exit 0 fi # Scale down mongo service echo "Scaling down mongo service..." docker service scale "$MONGO_SERVICE"=0 # Handle pre-restore backup CREATE_PRE_BACKUP=true if [ "$DO_PRE_BACKUP" = "false" ]; then CREATE_PRE_BACKUP=false elif [ "$DO_PRE_BACKUP" = "true" ]; then CREATE_PRE_BACKUP=true elif [ -z "$DO_PRE_BACKUP" ]; then # Ask user interactively read -p "Create pre-restore safety backup? (Y/n): " -n 1 -r echo if [[ $REPLY =~ ^[Nn]$ ]]; then CREATE_PRE_BACKUP=false fi else echo "Error: --do-pre-backup must be 'true' or 'false'" exit 1 fi # Create pre-restore backup if requested if [ "$CREATE_PRE_BACKUP" = true ]; then SAFETY_BACKUP="$BACKUP_DIR/mongo-volume-pre-restore-$(date +%Y-%m-%d-%H-%M).tar.gz" echo "Creating safety backup: $SAFETY_BACKUP" sudo tar -czf "$SAFETY_BACKUP" mongo-volume 2>/dev/null || true else echo "Skipping pre-restore backup" SAFETY_BACKUP="" fi # Remove current volume echo "Removing current mongo-volume..." sudo rm -rf mongo-volume # Extract backup echo "Extracting backup..." sudo tar -xzf "$BACKUP_FILE" # Set proper permissions echo "Setting permissions..." sudo chown -R 999:999 mongo-volume # Scale mongo service back up echo "Scaling mongo service back up..." docker service scale "$MONGO_SERVICE"=1 echo "Restore completed successfully!" if [ -n "$SAFETY_BACKUP" ]; then echo "Safety backup saved as: $SAFETY_BACKUP" fi