Back up DNS zones
DNS records are configuration that breaks things when they change, and they rarely have a history. This recipe copies a zone from its DNS server with a zone transfer (AXFR), sorts the records and stores them, so a changed address, a new record or a deleted one shows in rConfig as a single changed line.
When to use it
- Your DNS servers allow zone transfers to an approved address, as BIND, Windows DNS, PowerDNS, Infoblox and most others can.
- You want a record of every change to internal or external zones.
What you need
dig, frombind-utils(RHEL family) ordnsutils(Ubuntu).- The DNS server set to allow zone transfers to the rConfig server’s address, for each zone you collect.
- Optionally, a TSIG key for the transfer, saved as
keys/dns-transfer.keyin the scripts folder. Many servers require one.
The template
main: name: "SIE - DNS zone" desc: "Copies a DNS zone by zone transfer and stores it sorted"
connect: timeout: 120 idletimeout: 60 idleTimeout: 60 protocol: script
# exit_codes is deliberately left out: any non-zero exit fails the run.failure_criteria: error_patterns: - "ERROR:" success_patterns: - "SIE-OK: zone"The command
The device’s IP address is the DNS server. Make one command per zone, with the zone name as the last word:
/bin/bash {script_path}dns-zone.sh {device_ip} example.comThe script
Save it as dns-zone.sh in the scripts folder.
#!/usr/bin/env bash## Copy a DNS zone by zone transfer and store it sorted, for the rConfig Script# Integration Engine.## Usage: dns-zone.sh <dns_server> <zone>## The zone goes to stdout. Progress, errors and the success marker go to stderr.# Exit code 0 on success, 1 on any failure.
set -o pipefail
TSIG_KEY="/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys/dns-transfer.key"DIG="/usr/bin/dig"
# Record types that change on their own (DNSSEC signatures). Remove to keep them.SKIP_TYPES='RRSIG|NSEC|NSEC3'# Set to 1 to hide the SOA serial, for zones updated by DHCP or other dynamic updates.MASK_SERIAL=0
fail() { echo "ERROR: $1" >&2; exit 1; }
SERVER="${1:-}"; ZONE="${2:-}"[ -n "$SERVER" ] && [ -n "$ZONE" ] || fail "usage: dns-zone.sh <dns_server> <zone>"
KEY_ARGS=()[ -r "$TSIG_KEY" ] && KEY_ARGS=(-k "$TSIG_KEY")
echo "transferring $ZONE from $SERVER" >&2zone=$("$DIG" "${KEY_ARGS[@]}" @"$SERVER" "$ZONE" AXFR +nocmd +nocomments +nostats +noquestion +onesoa) \ || fail "dig failed for $ZONE on $SERVER"
case "$zone" in *"Transfer failed"*|*"communications error"*|*"connection timed out"*|"") fail "transfer of $ZONE from $SERVER refused or failed" ;;esac
# One record per line, single spaces, types we don't want removed, sorted.zone=$(printf '%s\n' "$zone" \ | awk 'NF' \ | tr -s ' \t' ' ' \ | awk -v skip="^($SKIP_TYPES)$" '$4 !~ skip' \ | sort -k1,1 -k4,4 -k5)
if [ "$MASK_SERIAL" = "1" ]; then zone=$(printf '%s\n' "$zone" | awk '$4 == "SOA" { $7 = "<serial>" } { print }')fi
count=$(printf '%s\n' "$zone" | wc -l)[ "$count" -gt 1 ] || fail "$ZONE came back with no records"
printf '%s\n' "$zone"echo "SIE-OK: zone $ZONE, $count records from $SERVER" >&2What gets stored
example.com. 3600 IN MX 10 mail.example.com.example.com. 3600 IN NS ns1.example.com.example.com. 3600 IN SOA ns1.example.com. hostmaster.example.com. 2026111501 3600 900 1209600 300mail.example.com. 3600 IN A 192.0.2.25www.example.com. 300 IN A 192.0.2.80Sorting by name, then type, then value means the order doesn’t depend on how the server sends the zone, so only real changes show.
Adapting it
- Dynamic zones. Zones updated by DHCP change their serial and records all day. Set
MASK_SERIAL=1, and consider leaving those zones out: their history is mostly noise. - TTL changes you don’t care about. Drop the second column with
awk '{ $2 = ""; print }'before sorting. - Several zones on one server. One command per zone keeps a separate history for each. One script printing several zones under
===== zone =====lines keeps them together.