Created
July 23, 2026 13:44
-
-
Save joshuafredrickson/de88d70ae5b692a644b211d3c1599509 to your computer and use it in GitHub Desktop.
Cloudflare DNS Zone Export bash script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| API_TOKEN="" # Add a valid API key with `DNS: Read` permissions | |
| API_BASE="https://api.cloudflare.com/client/v4" | |
| DOMAIN="$1" | |
| if [[ -z "$DOMAIN" ]]; then | |
| echo "Usage: $0 <domain>" | |
| exit 1 | |
| fi | |
| cf_api() { | |
| curl -s \ | |
| -H "Authorization: Bearer $API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| "$@" | |
| } | |
| echo "Looking up Zone ID for $DOMAIN..." | |
| ZONE_ID=$(cf_api \ | |
| "$API_BASE/zones?name=$DOMAIN&status=active" \ | |
| | grep -o '"id":"[^"]*"' \ | |
| | head -n 1 \ | |
| | cut -d'"' -f4) | |
| if [[ -z "$ZONE_ID" ]]; then | |
| echo "✖ Zone ID not found for $DOMAIN" | |
| exit 1 | |
| fi | |
| echo "✔ Zone ID found: $ZONE_ID" | |
| OUTPUT_FILE="$HOME/Downloads/${DOMAIN}-$(date +%Y%m%d-%H%M%S).zone" | |
| echo "Exporting DNS zone file for $DOMAIN..." | |
| RESPONSE=$(cf_api "$API_BASE/zones/$ZONE_ID/dns_records/export") | |
| if echo "$RESPONSE" | grep -q '"success":false'; then | |
| echo "✖ Zone file export failed" | |
| echo "$RESPONSE" | |
| exit 1 | |
| fi | |
| echo "$RESPONSE" > "$OUTPUT_FILE" | |
| echo "✔ Zone file saved to $OUTPUT_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment