#!/bin/bash # Atlas CMMS Database Restore Script # This script restores a backup of the Atlas CMMS PostgreSQL database # Check if backup file is provided if [ $# -eq 0 ]; then echo "Usage: $0 " echo "Example: $0 ./backups/atlas_backup_20240101_120000.sql.gz" exit 1 fi BACKUP_FILE="$1" CONTAINER_NAME="atlas_db" DB_NAME="atlas" DB_USER="rootUser" echo "Starting Atlas CMMS database restore..." echo "Container: ${CONTAINER_NAME}" echo "Database: ${DB_NAME}" echo "Backup file: ${BACKUP_FILE}" # Check if backup file exists if [ ! -f "${BACKUP_FILE}" ]; then echo "Error: Backup file '${BACKUP_FILE}' not found" exit 1 fi # Check if container is running if ! docker ps | grep -q "${CONTAINER_NAME}"; then echo "Error: PostgreSQL container '${CONTAINER_NAME}' is not running" exit 1 fi # Warning message echo "WARNING: This will replace all data in the '${DB_NAME}' database!" 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 # Stop the API container to prevent connections during restore echo "Stopping API container..." docker stop atlas_api 2>/dev/null || true # Determine if backup file is compressed if [[ "${BACKUP_FILE}" == *.gz ]]; then echo "Decompressing and restoring backup..." gunzip -c "${BACKUP_FILE}" | docker exec -i "${CONTAINER_NAME}" psql \ -U "${DB_USER}" \ -d postgres \ --quiet else echo "Restoring backup..." cat "${BACKUP_FILE}" | docker exec -i "${CONTAINER_NAME}" psql \ -U "${DB_USER}" \ -d postgres \ --quiet fi # Check if restore was successful if [ $? -eq 0 ]; then echo "Database restore completed successfully" # Restart the API container echo "Restarting API container..." docker start atlas_api echo "Restore process completed" else echo "Error: Database restore failed" # Try to restart the API container anyway docker start atlas_api 2>/dev/null || true exit 1 fi