Skip to content

Instantly share code, notes, and snippets.

@a-kbv
Last active September 10, 2024 08:53
Show Gist options
  • Save a-kbv/ce7e798d5b36cda951eb7b8ac725833a to your computer and use it in GitHub Desktop.
Save a-kbv/ce7e798d5b36cda951eb7b8ac725833a to your computer and use it in GitHub Desktop.

This Bash script is designed to recursively search for .env files starting from a specified directory and create a centralized backup structure for these files within a main backup directory called env-backup.

Features and Workflow

  1. Initial Setup:

    • The script identifies the directory from which it is run (the current working directory) using the pwd command.
    • It creates a main backup directory named env-backup in this current directory. All individual backups will be contained within this directory.
  2. Recursive Search:

    • The script accepts a directory path as an argument. It starts searching from this specified directory and recursively finds all files named .env.
    • It uses the find command to gather all .env files within the given directory hierarchy.
  3. Backup Process:

    • For each .env file found, the script:
      • Determines the directory containing the .env file and extracts its base name.
      • Creates a dedicated backup directory within env-backup with a name derived from the base name of the folder containing the .env file, appended with _backup.
      • Copies the .env file into the corresponding backup directory.
  4. Progress and Error Reporting:

    • As the script processes each file, it outputs informative messages to the terminal, indicating the progress of file discovery, backup directory creation, and file copying.
    • It handles errors gracefully, reporting if there are any issues creating directories or copying files.
#!/bin/bash

# Get the current working directory where the script is run
current_dir=$(pwd)

# Create the main backup directory
main_backup_dir="$current_dir/env-backup"
mkdir -p "$main_backup_dir" || { echo "Failed to create main backup directory"; exit 1; }

# Function to recursively find and process .env files
process_directory() {
    local dir="$1"

    echo "Searching for .env files in '$dir'..."

    # Find all .env files in the current directory
    find "$dir" -type f -name ".env" | while read -r env_file; do
        # Display the currently processed .env file
        echo "Processing: $env_file"

        # Get the directory containing the .env file
        env_dir=$(dirname "$env_file")

        # Get the base directory name for the backup
        base_name=$(basename "$env_dir")

        # Create a backup directory under main_backup_dir for this .env file
        backup_dir="$main_backup_dir/${base_name}_backup"
        mkdir -p "$backup_dir" || { echo "Failed to create backup directory for $base_name"; continue; }

        # Copy the .env file to the appropriate backup directory
        if cp "$env_file" "$backup_dir/"; then
            echo "Copied .env file from '$env_dir' to '$backup_dir/'"
        else
            echo "Failed to copy .env file from '$env_dir'"
        fi
    done

    echo "Completed searching and backing up .env files in '$dir'"
}

# Check if a directory is specified
if [[ -z "$1" ]]; then
    echo "Please provide a directory to search for .env files."
    exit 1
fi

# Start processing from the specified directory
process_directory "$1"

How to Use the Script

  1. Save the script to a file, e.g., backup_env.sh.
  2. Give execution permission to the script:
    chmod +x backup_env.sh
  3. Run the script with the directory you want to search as an argument:
    ./backup_env.sh /path/to/directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment