Compare commits

...

3 Commits
1.2 ... main

4 changed files with 89 additions and 41 deletions

View File

@ -27,7 +27,10 @@ done
# --- Fetch current IP from Mullvad ---
log "[*] Fetching IP from Mullvad..."
IP_INFO=$(curl -sf https://ipv4.am.i.mullvad.net/json)
IP_INFO=$(curl -sf https://ipv4.am.i.mullvad.net/json) || {
log "[!] Failed to fetch IP from Mullvad"
exit 1
}
if ! echo "$IP_INFO" | jq -e '.ip' >/dev/null; then
log "[!] Invalid Mullvad response or missing IP."
@ -61,7 +64,6 @@ for ENV_FILE in "${ENV_FILES[@]}"; do
missing_vars=()
[[ -z "${ZONE_ID:-}" ]] && missing_vars+=("ZONE_ID")
[[ -z "${DNS_NAME:-}" ]] && missing_vars+=("DNS_NAME")
[[ -z "${CLOUDFLARE_EMAIL:-}" ]] && missing_vars+=("CLOUDFLARE_EMAIL")
[[ -z "${CLOUDFLARE_API_KEY:-}" ]] && missing_vars+=("CLOUDFLARE_API_KEY")
if (( ${#missing_vars[@]} )); then
@ -70,31 +72,46 @@ for ENV_FILE in "${ENV_FILES[@]}"; do
continue
fi
# --- Fetch DNS records for the zone ---
# --- Check if DNS record exists ---
log "[*] Checking DNS record for $DNS_NAME..."
DNS_LOOKUP=$(curl -sf -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$DNS_NAME" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json")
CURL_EXIT_CODE=$?
if [[ $CURL_EXIT_CODE -ne 0 ]]; then
log "[!] Failed to query DNS record for $DNS_NAME (curl exit code $CURL_EXIT_CODE)"
log "$DNS_LOOKUP"
log ""
continue
fi
RECORD_ID=$(echo "$DNS_LOOKUP" | jq -r '.result[0].id // empty')
EXISTING_IP=$(echo "$DNS_LOOKUP" | jq -r '.result[0].content // empty')
if [[ -z "$RECORD_ID" ]]; then
log "[!] No existing record found. Creating new A record for $DNS_NAME..."
CREATE_RESPONSE=$(curl -sf -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-d "{
\"type\": \"A\",
\"name\": \"$DNS_NAME\",
\"content\": \"$CURRENT_IP\",
\"ttl\": 3600,
\"proxied\": true,
\"comment\": \"Created via script\"
\"type\": \"A\",
\"comment\": \"Domain verification record\",
\"content\": \"$CURRENT_IP\",
\"proxied\": true
}")
CURL_EXIT_CODE=$?
if [[ $CURL_EXIT_CODE -ne 0 ]]; then
log "[!] curl failed creating DNS record (exit code $CURL_EXIT_CODE)"
log "$CREATE_RESPONSE"
log ""
continue
fi
if [[ $(echo "$CREATE_RESPONSE" | jq -r '.success') == "true" ]]; then
log "[+] Successfully created DNS record for $DNS_NAME$CURRENT_IP"
@ -102,6 +119,7 @@ for ENV_FILE in "${ENV_FILES[@]}"; do
log "[!] Failed to create DNS record for $DNS_NAME"
echo "$CREATE_RESPONSE" | tee -a "$LOG_FILE"
fi
log ""
continue
fi
@ -118,17 +136,24 @@ for ENV_FILE in "${ENV_FILES[@]}"; do
log "[*] Updating existing DNS record via PATCH..."
UPDATE_RESPONSE=$(curl -sf -X PATCH "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $CLOUDFLARE_API_KEY" \
-d "{
\"type\": \"A\",
\"name\": \"$DNS_NAME\",
\"content\": \"$CURRENT_IP\",
\"ttl\": 3600,
\"proxied\": true,
\"comment\": \"Updated via script\"
\"type\": \"A\",
\"comment\": \"Domain verification record\",
\"content\": \"$CURRENT_IP\",
\"proxied\": true
}")
CURL_EXIT_CODE=$?
if [[ $CURL_EXIT_CODE -ne 0 ]]; then
log "[!] curl failed updating DNS record (exit code $CURL_EXIT_CODE)"
log "$UPDATE_RESPONSE"
log ""
continue
fi
if [[ $(echo "$UPDATE_RESPONSE" | jq -r '.success') == "true" ]]; then
log "[+] Successfully updated $DNS_NAME to $CURRENT_IP"

View File

@ -1,5 +1,4 @@
ZONE_ID="abc123zoneid"
DNS_NAME="subdomain.example.com"
CLOUDFLARE_EMAIL="you@example.com"
CLOUDFLARE_API_KEY="your_api_key_here"

View File

@ -1,2 +1,3 @@
# Cloudflare API Token (must have Zone:Read + DNS:Read permissions)
# Example env file (envs/site1.env)
CLOUDFLARE_API_TOKEN=your_api_token_here

View File

@ -1,38 +1,61 @@
#!/bin/bash
set -euo pipefail
# Load config
source config.conf
ENV_DIR="./envs"
EXPORT_FOLDER="export"
mkdir -p "$EXPORT_FOLDER"
# Step 1: Fetch all zones from Cloudflare
echo "Fetching zone list from Cloudflare..."
zones_json=$(curl -s https://api.cloudflare.com/client/v4/zones \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
# Find all .env files in the ENV_DIR
env_files=("$ENV_DIR"/*.env)
# Step 2: Parse zones (needs jq)
zone_count=$(echo "$zones_json" | jq '.result | length')
echo "Found $zone_count zones."
if [ ${#env_files[@]} -eq 0 ]; then
echo "❌ No .env files found in $ENV_DIR"
exit 1
fi
# Step 3: Loop through zones
for ((i=0; i<zone_count; i++)); do
ZONE_ID=$(echo "$zones_json" | jq -r ".result[$i].id")
SITE_NAME=$(echo "$zones_json" | jq -r ".result[$i].name")
for env_file in "${env_files[@]}"; do
echo "🔄 Processing environment file: $env_file"
# Load environment variables from .env file
set -o allexport
source "$env_file"
set +o allexport
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
OUTPUT_FILE="$EXPORT_FOLDER/${SITE_NAME}_$TIMESTAMP.txt"
if [[ -z "${CLOUDFLARE_API_TOKEN:-}" ]]; then
echo "⚠️ CLOUDFLARE_API_TOKEN not set in $env_file. Skipping."
continue
fi
echo "Exporting DNS records for $SITE_NAME..."
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/export" \
# Step 1: Fetch all zones from Cloudflare
echo "Fetching zone list from Cloudflare..."
zones_json=$(curl -s https://api.cloudflare.com/client/v4/zones \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" | tee "$OUTPUT_FILE"
-H "Content-Type: application/json")
echo " -> Saved to $OUTPUT_FILE"
# Step 2: Parse zones (needs jq)
zone_count=$(echo "$zones_json" | jq '.result | length')
echo "Found $zone_count zones."
# Step 3: Loop through zones
for ((i=0; i<zone_count; i++)); do
ZONE_ID=$(echo "$zones_json" | jq -r ".result[$i].id")
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"
echo "Exporting DNS records for $SITE_NAME..."
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/export" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" | tee "$OUTPUT_FILE"
echo " -> Saved to $OUTPUT_FILE"
done
echo "✅ Export complete for $env_file"
echo ""
done
echo "✅ All exports complete. Files are in the '$EXPORT_FOLDER' folder."
echo "🎉 All exports finished. Files are in the '$EXPORT_FOLDER' folder."