Enhancing DevOps Efficiency with Automated Project Environment Backups


When managing multiple Node.js, Angular, or general web projects in a CI/CD pipeline using Jenkins, it becomes essential to backup critical configuration files such as .env
, config.js
, and Angular environment.ts
files. These files often contain sensitive or environment-specific variables, making them vital for redeployment, troubleshooting, or disaster recovery.
This blog post walks you through a simple Bash script that automates the extraction and backup of such files from your Jenkins workspace.
๐ฏ Objective
To automate the following:
Backup
.env
files.Backup
config/
directories only if they containconstant.js
.Backup
config.ts
andconfig.prod
.ts
files.Backup Angular-like
environments/
folders, whether directly under the project or undersrc/
.Skip unwanted deep scanning (like
node_modules
or deeply nested directories).Store all collected files into a centralized backup folder with timestamp.
๐ ๏ธ The Script
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <jenkins_projects_directory>"
exit 1
fi
JENKINS_DIR="$1"
BACKUP_DIR="/path/to/backup/directory/Dev_env_backup_$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
for PROJECT_PATH in "$JENKINS_DIR"/*; do
if [ -d "$PROJECT_PATH" ]; then
PROJECT_NAME=$(basename "$PROJECT_PATH")
DEST_PATH="$BACKUP_DIR/$PROJECT_NAME"
ENV_FOUND=false
# .env file
if [ -f "$PROJECT_PATH/.env" ]; then
mkdir -p "$DEST_PATH"
cp "$PROJECT_PATH/.env" "$DEST_PATH/"
ENV_FOUND=true
fi
# config/ folder (only if it contains constant.js)
CONFIG_DIRS=$(find "$PROJECT_PATH" -mindepth 1 -maxdepth 3 -type d -name "config" ! -path "*/node_modules/*" ! -path "*/dist/*")
for MATCH in $CONFIG_DIRS; do
if find "$MATCH" -maxdepth 1 -name "constant.js" | grep -q .; then
RELATIVE_PATH=$(realpath --relative-to="$PROJECT_PATH" "$MATCH")
mkdir -p "$DEST_PATH/$(dirname "$RELATIVE_PATH")"
cp -r "$MATCH" "$DEST_PATH/$(dirname "$RELATIVE_PATH")/"
ENV_FOUND=true
fi
done
# environments/ folder inside src/ or directly under project
ENV_DIRS=$(find "$PROJECT_PATH" -mindepth 1 -maxdepth 3 -type d \( -path "$PROJECT_PATH/src/environments" -o -path "$PROJECT_PATH/environments" \) ! -path "*/node_modules/*" ! -path "*/dist/*")
for MATCH in $ENV_DIRS; do
RELATIVE_PATH="environments"
mkdir -p "$DEST_PATH/$RELATIVE_PATH"
cp -r "$MATCH/"* "$DEST_PATH/$RELATIVE_PATH/"
ENV_FOUND=true
done
# config.ts and config.prod.ts files
CONFIG_TS_FILES=$(find "$PROJECT_PATH" -mindepth 1 -maxdepth 3 -type f \( -name "config.ts" -o -name "config.prod.ts" \) ! -path "*/node_modules/*" ! -path "*/dist/*")
for FILE in $CONFIG_TS_FILES; do
RELATIVE_FILE_PATH=$(realpath --relative-to="$PROJECT_PATH" "$FILE")
mkdir -p "$DEST_PATH/$(dirname "$RELATIVE_FILE_PATH")"
cp "$FILE" "$DEST_PATH/$RELATIVE_FILE_PATH"
ENV_FOUND=true
done
if [ "$ENV_FOUND" = true ]; then
echo "โ Backed up: $PROJECT_NAME"
fi
fi
done
echo "โ
Backup completed in: $BACKUP_DIR"
๐ Folder Structure Example
If your Jenkins directory contains projects like:
/var/lib/jenkins/
โโโ projectA
โ โโโ .env
โ โโโ config/constant.js
โ โโโ src/environments/environment.ts
โโโ projectB
โ โโโ .env
The backup will create:
/path/to/backup/directory/Dev_env_backup_20250408/
โโโ projectA/
โ โโโ .env
โ โโโ config/constant.js
โ โโโ environments/environment.ts
โโโ projectB/
โ โโโ .env
๐ก Why Limit Search Depth?
Without a -maxdepth
limit, find
would recursively dive into unnecessary folders such as:
node_modules/
deeply nested
src/
directories
By restricting depth to 2โ3 levels, the script remains efficient and avoids collecting unwanted files.
๐ How to Use
Save the script to a file, e.g.,
backup_
envs.sh
Make it executable:
chmod +x backup_envs.sh
Run it with the Jenkins directory path:
./backup_envs.sh /var/lib/jenkins
๐งฉ Conclusion
This script provides a clean and reliable way to automate environment file backups across multiple Jenkins projects. Whether you're rotating environments, preparing for migrations, or simply backing up sensitive configurations โ this solution will keep your DevOps work organized and secure.
Subscribe to my newsletter
Read articles from Vikas Rajpurohit directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
