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 contain constant.js.

  • Backup config.ts and config.prod.ts files.

  • Backup Angular-like environments/ folders, whether directly under the project or under src/.

  • 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

  1. Save the script to a file, e.g., backup_envs.sh

  2. Make it executable:

     chmod +x backup_envs.sh
    
  3. 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.

0
Subscribe to my newsletter

Read articles from Vikas Rajpurohit directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vikas Rajpurohit
Vikas Rajpurohit