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
.
-
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.
- The script identifies the directory from which it is run (the current working directory) using the
-
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.
- The script accepts a directory path as an argument. It starts searching from this specified directory and recursively finds all files named
-
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.
- Determines the directory containing the
- For each
-
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"
- Save the script to a file, e.g.,
backup_env.sh
. - Give execution permission to the script:
chmod +x backup_env.sh
- Run the script with the directory you want to search as an argument:
./backup_env.sh /path/to/directory