Updated the Cloudflare script to create a folder with the date and time that the script is being ran, in the even it is ran multiple times in a day. Updated the script to use the directory where the script itself is located rather than the current working directory for looking for the export and env folders.

This commit is contained in:
2026-07-19 13:49:04 +01:00
parent 0a90e4c305
commit 85f6d2f960
+17 -9
View File
@@ -1,28 +1,36 @@
#!/bin/bash
set -euo pipefail
ENV_DIR="./envs"
EXPORT_FOLDER="export"
# Get the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_DIR="$SCRIPT_DIR/envs"
EXPORT_FOLDER="$SCRIPT_DIR/export"
# Create export folder and a dated subfolder with time (YYYY-MM-DD_HH-MM-SS)
mkdir -p "$EXPORT_FOLDER"
DATE_DIR="$(date +"%Y-%m-%d_%H-%M-%S")"
OUTPUT_DIR="$EXPORT_FOLDER/$DATE_DIR"
mkdir -p "$OUTPUT_DIR"
# Find all .env files in the ENV_DIR
env_files=("$ENV_DIR"/*.env)
if [ ${#env_files[@]} -eq 0 ]; then
echo "No .env files found in $ENV_DIR"
echo "No .env files found in $ENV_DIR"
exit 1
fi
for env_file in "${env_files[@]}"; do
echo "🔄 Processing environment file: $env_file"
echo "Processing environment file: $env_file"
# Load environment variables from .env file
set -o allexport
source "$env_file"
set +o allexport
if [[ -z "${CLOUDFLARE_API_TOKEN:-}" ]]; then
echo "⚠️ CLOUDFLARE_API_TOKEN not set in $env_file. Skipping."
echo "CLOUDFLARE_API_TOKEN not set in $env_file. Skipping."
continue
fi
@@ -42,7 +50,7 @@ for env_file in "${env_files[@]}"; do
SITE_NAME=$(echo "$zones_json" | jq -r ".result[$i].name")
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
OUTPUT_FILE="$EXPORT_FOLDER/${SITE_NAME}_$TIMESTAMP.txt"
OUTPUT_FILE="$OUTPUT_DIR/${SITE_NAME}_$TIMESTAMP.txt"
echo "Exporting DNS records for $SITE_NAME..."
@@ -53,9 +61,9 @@ for env_file in "${env_files[@]}"; do
echo " -> Saved to $OUTPUT_FILE"
done
echo "Export complete for $env_file"
echo "Export complete for $env_file"
echo ""
done
echo "🎉 All exports finished. Files are in the '$EXPORT_FOLDER' folder."
echo "All exports finished. Files are in the '$OUTPUT_DIR' folder."