#!/bin/bash # Author: Julien Gautier # Organization: AdAstra # Project: adastra_scripts # ################################################################################ # MongoDB Dump Utility Script (Standalone) # # This script performs backups (dumps) of MongoDB databases from remote or local servers. # It manages multiple environments (prod, uat, etc.) with stored credentials. # # Usage: ./db-dump.sh # # Commands: # dump Select environment and perform a dump # list List all configured environments # add Add a new environment # delete Delete an environment # help Show this help message # # Examples: # ./db-dump.sh dump # ./db-dump.sh list # ./db-dump.sh add # # Configuration: # Each environment is stored in a separate .env. file. # All dumps are stored in ./dumps/// # ################################################################################ set -e # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" DUMPS_DIR="${SCRIPT_DIR}/dumps" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # Helper functions print_info() { echo -e "${BLUE}┣━${NC} ${CYAN}${1}${NC}" } print_success() { echo -e "${GREEN}✓ ${1}${NC}" } print_error() { echo -e "${RED}✗ ${1}${NC}" } print_warning() { echo -e "${YELLOW}⚠ ${1}${NC}" } print_separator() { echo -e "${BLUE}┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" } print_separator_up() { echo -e "${BLUE}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" } print_separator_down() { echo -e "${BLUE}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" } print_section() { echo print_separator_up print_info "$1" print_separator_down echo } check_mongodump() { if ! command -v mongodump &> /dev/null; then print_error "mongodump is not installed or not in PATH" print_info "Install MongoDB Database Tools from: https://www.mongodb.com/try/download/database-tools" exit 1 fi print_success "mongodump found: $(mongodump --version)" } get_environments() { local envs=() for env_file in "$SCRIPT_DIR"/.env.*; do if [[ -f "$env_file" ]]; then local env_name=$(basename "$env_file" | sed 's/^\.env\.//') # Exclude the example file if [[ "$env_name" != "example" ]]; then envs+=("$env_name") fi fi done echo "${envs[@]}" } env_file_path() { echo "${SCRIPT_DIR}/.env.$1" } create_environment() { print_section "Create New Environment" # Check if environment name already exists read -p "Enter environment name (prod, uat, etc.): " env_name env_name="${env_name,,}" # Convert to lowercase if [[ -z "$env_name" ]]; then print_error "Environment name cannot be empty" return 1 fi if [[ -f "$(env_file_path "$env_name")" ]]; then print_error "Environment '$env_name' already exists" return 1 fi print_info "Enter MongoDB connection details for '$env_name'" echo # Read credentials (no defaults) read -p "MongoDB username: " mongo_username if [[ -z "$mongo_username" ]]; then print_error "Username cannot be empty" return 1 fi read -sp "MongoDB password: " mongo_password echo if [[ -z "$mongo_password" ]]; then print_error "Password cannot be empty" return 1 fi read -p "MongoDB host (e.g., cluster0.mongodb.net): " mongo_host if [[ -z "$mongo_host" ]]; then print_error "Host cannot be empty" return 1 fi read -p "MongoDB port (e.g., 27017): " mongo_port if [[ -z "$mongo_port" ]]; then print_error "Port cannot be empty" return 1 fi read -p "Application name (appName): " app_name if [[ -z "$app_name" ]]; then print_error "Application name cannot be empty" return 1 fi # Create .env file for this environment local env_file=$(env_file_path "$env_name") cat > "$env_file" << EOF # Environment: $env_name # Created: $(date) MONGO_USERNAME=${mongo_username} MONGO_PASSWORD=${mongo_password} MONGO_HOST=${mongo_host} MONGO_PORT=${mongo_port} APP_NAME=${app_name} EOF chmod 600 "$env_file" # Restrict permissions for security print_success "Environment '$env_name' created successfully" print_info "Configuration stored in: $(basename "$env_file")" } # Global variable to hold selected environment SELECTED_ENVIRONMENT="" select_environment() { SELECTED_ENVIRONMENT="" local envs=($(get_environments)) local count=${#envs[@]} print_separator_up if [[ $count -eq 0 ]]; then print_info "No Environments Configured" print_separator echo print_warning "No existing environments found" echo echo " 1) Create a new environment" echo " 2) Exit" echo echo -n "Enter your choice (1-2): " read choice if [[ "$choice" == "1" ]]; then if create_environment; then # After creating an environment, recursively call to select it select_environment return $? else return 1 fi else return 1 fi fi print_info "Select Environment" print_separator_down echo for i in "${!envs[@]}"; do echo " $((i + 1))) ${envs[$i]}" done echo " $((count + 1))) Create new environment" echo echo -n "Enter your choice (1-$((count + 1))): " read choice if [[ "$choice" =~ ^[0-9]+$ ]] && [[ $choice -ge 1 ]] && [[ $choice -le $((count + 1)) ]]; then if [[ $choice -eq $((count + 1)) ]]; then if create_environment; then # After creating an environment, recursively call to select it select_environment return $? else return 1 fi else SELECTED_ENVIRONMENT="${envs[$((choice - 1))]}" return 0 fi else print_error "Invalid choice" return 1 fi } load_environment() { local env_name="$1" local env_file=$(env_file_path "$env_name") if [[ ! -f "$env_file" ]]; then print_error "Environment file not found: $env_file" return 1 fi # shellcheck source=/dev/null source "$env_file" } perform_dump() { local env_name="$1" load_environment "$env_name" || return 1 print_section "MongoDB Dump: $env_name" # Create output directory with timestamp local timestamp=$(date +%Y%m%d_%H%M) local output_dir="${DUMPS_DIR}/${APP_NAME}/${timestamp}" mkdir -p "$output_dir" print_separator_up print_info "Environment: $env_name" print_info "App Name: $APP_NAME" print_info "Host: $MONGO_HOST:$MONGO_PORT" print_info "Output: $output_dir" print_separator_down # Construct connection string local connection_uri="mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@${APP_NAME}@" # Perform dump if mongodump --uri "$connection_uri" -o "$output_dir" 2>&1; then print_success "Dump completed successfully" print_info "Output directory: $output_dir" # Show dump summary local dump_size=$(du -sh "$output_dir" | cut -f1) print_info "Total size: $dump_size" else print_error "Dump failed" print_warning "Removing incomplete dump directory..." rm -rf "$output_dir" return 1 fi } cmd_dump() { print_separator_up print_info "MongoDB Dump Utility" print_separator_down check_mongodump select_environment if [[ $? -ne 0 ]]; then print_error "No environment selected" return 1 fi if perform_dump "$SELECTED_ENVIRONMENT"; then print_success "Dump operation completed" else print_error "Dump operation failed" return 1 fi } cmd_list() { print_section "Configured Environments" local envs=($(get_environments)) if [[ ${#envs[@]} -eq 0 ]]; then print_warning "No environments configured yet" return 0 fi for env in "${envs[@]}"; do local env_file=$(env_file_path "$env") # shellcheck source=/dev/null source "$env_file" echo " • $env" echo " Host: $MONGO_HOST:$MONGO_PORT" echo " App: $APP_NAME" echo done } cmd_add() { if create_environment; then print_success "Environment added successfully" else print_error "Failed to add environment" return 1 fi } cmd_delete() { print_section "Delete Environment" local envs=($(get_environments)) if [[ ${#envs[@]} -eq 0 ]]; then print_warning "No environments configured" return 0 fi for i in "${!envs[@]}"; do echo " $((i + 1))) ${envs[$i]}" done echo read -p "Enter environment number to delete: " choice if [[ "$choice" =~ ^[0-9]+$ ]] && [[ $choice -ge 1 ]] && [[ $choice -le ${#envs[@]} ]]; then local env_name="${envs[$((choice - 1))]}" read -p "Confirm deletion of '$env_name'? (yes/no): " confirm if [[ "$confirm" == "yes" ]]; then local env_file=$(env_file_path "$env_name") rm -f "$env_file" print_success "Environment '$env_name' deleted" else print_info "Deletion cancelled" fi else print_error "Invalid choice" return 1 fi } cmd_help() { cat << EOF MongoDB Dump Utility Script (Standalone) Usage: ./db-dump.sh Commands: dump Select an environment and perform a backup dump list List all configured environments add Add a new environment configuration delete Delete an environment configuration help Show this help message Examples: ./db-dump.sh dump # Interactive environment selection and dump ./db-dump.sh list # Show all environments ./db-dump.sh add # Add new environment ./db-dump.sh delete # Remove an environment Environment Configuration: - Each environment is stored in a separate .env. file - Credentials are encrypted (file permissions: 600) - Supported environments: prod, uat, dev, staging, etc. Dump Output: - Dumps are stored in: ./dumps/// - Each dump is timestamped for easy identification - Connection string includes MongoDB Atlas parameters Requirements: - MongoDB Database Tools (mongodump) - Bash shell MongoDB Connection String Format: mongodb://:@:/ ?ssl=true&replicaSet=globaldb&retrywrites=false &maxIdleTimeMS=120000&appName=@@ EOF } # Main COMMAND="${1:-help}" case "$COMMAND" in dump) cmd_dump ;; list) cmd_list ;; add) cmd_add ;; delete) cmd_delete ;; help|--help|-h) cmd_help ;; *) print_error "Unknown command: $COMMAND" echo cmd_help exit 1 ;; esac