Axfr to Git

May 15, 2011

dns

The problem: you want to keep a historical reference of zone changes. This little script helps you do that, it:

  1. AXFRs the zone you want;
  2. Cleans the zone a bit, sorts it and feeds it through named-compilezone to make it look “nice”;
  3. Puts it in a git repository;
  4. Checks it in with a date tag (date -u '+%Y-%m-%d_%H%M%S').

You do need to define the directory where the git repository is located in the script ($gitdir).

Axfr2git:

#!/bin/bash

# Define gitdir and check if git is initialized.
gitdir=/tmp/stats
if [ ! -d $gitdir ]; then
    mkdir -p $gitdir
fi
if [ ! -e $gitdir/.git ]; then
    ( cd $gitdir; git init )
fi

# 2 args: @nameserver zonename
ns=$1
zone=$2
shift 2
if [ -z "$ns" ]; then
    echo Synopsis: $0 @nameserver zonename
    exit 1
fi
if [ -z "$zone" ]; then
    echo Synopsis: $0 @nameserver zonename
    exit 1
fi

# perform the dig
dig +norec AXFR "$ns" "$zone" | grep -v '^;' | sort | \
named-compilezone -s relative -i none -o $gitdir/$zone-zone $zone /dev/stdin
if [ $? -ne 0 ]; then
    echo 0: Failure to AXFR $zone from $ns >&2
    exit 1
fi
( cd $gitdir
git add $zone-zone
git commit -a -m"$zone zone of $(date -u)" >/dev/null
git tag "$(date -u '+%Y-%m-%d_%H%M%S')" )
None