✓ Solved

Export a DNS Zone

Linode Staff

I need to export a DNS zone file, how can I do that?

5 Replies

✓ Best Answer

I see that the Linode API now has the ability to extract the zone file for a domain as an array of lines. I wrote a script using bash, jq, and the Linode CLI to generate a file for each of your zones, called db.$DOMAIN

Instructions

  • Deploy a temporary new Ubuntu 20.04 LTS Linode

  • In the Linode Manager, create a new access token with read-only access to domains.

# Install the prerequisites
apt update
apt install python3-pip
apt install jq
pip3 install linode-cli
linode-cli configure

Copy and paste the script into export_zones.sh

#!/bin/bash

#
# export_zones.sh
#    Use the Linode CLI and jq to export all of your zones
#
# Usage: ./export_zones.sh
#
# Author: Harold Phillips
#

# Get all of your Domain IDs (One per Line)
IDS=$(linode-cli domains list --json | jq '.[] | .id')

# Set the field separator to newline
IFS=$'\n'

for ID in $IDS
do
        # Get the domain
        DOMAIN=$(linode-cli domains view $ID --json | jq -r '.[] | .domain')
        echo $DOMAIN
        echo "" > "db.${DOMAIN}"
        # Get the lines of the zone file
        LINES=$(linode-cli domains zone-file $ID --json | jq -r '.[] | .zone_file | .[]')
        for LINE in $LINES
        do
                echo $LINE
                echo $LINE >> "db.${DOMAIN}"
        done
        echo "---------------------"
done

Make the file executable and run the file:

chmod +x ./export_zones.sh
./export_zones.sh
ls

There isn’t a specific 'Export Zone' feature in the DNS Manager, but it looks like other services do this by saving the BIND formatted zone file as a regular text file. You can click the Zone File link in the Classic DNS Manager, then copy and paste to a .txt file on your local machine. This would also be considered a backup of your zone file.

Update: This answer not the best, but is still a useful query in a pinch.

As noted and as @hazymat mentioned, the following dig command may miss records. Instead, see the script which uses the Linode CLI to extract your zone files.


According to https://serverfault.com/questions/138949/list-all-dns-records-in-a-domain-using-dig, you can use the following dig query to get most records:

dig +noall +answer +multiline example.com any

Please do NOT follow instructions in the link to Serverfault provided by hphillips.

The premise of the question in that link is not the same as what's required here, and respondents were doing their best to give answers based on that question.

It is NOT a suitable way of transferring domain records and can - and most likely will - result in lost records. One lost subdomain record, for example, can be the difference between nobody noticing anything, and 6500 users in a company not receiving email for 2 days…

Another option is temporary enabling zone transfer for the domain and doing a zone transfer query using dig (I haven't tried this for big domains though).
I.e.

  1. Install dig (e.g. sudo apt install dnsutils on Ubuntu)
  2. In Linode Manager select the domain (e.g. mydomain.com), click Edit for the SOA record
  3. Specify the IP address of computer running dig under Domain Transfer IPs
  4. Run something like dig axfr mydomain.com @axfr1.linode.com > zone.txt
  5. Verify the results and remove IP from Domain Transfer IPs in Linode Manager

More info https://www.linode.com/docs/products/networking/dns-manager/guides/transfer-domain-zones/

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct