Christophe Casalegno

Serial DNS Updater : how to update automatically serial in dns zonefiles massively (or not)

When you work in a hosting company, DNS is probably one of the services with you need to interact every week. With this script you can update automatically one or more of dns zone files : for the serial number : just let bash doing it for u.

Because most of time we are not alone in a hosting company, it’s important to verify that nobody is using the script in the same time. It’s also important to have a tractability of the modifications, so all modifications are archived.

PS : don’t forget to install gawk on your server before !

Now, place to the shell : just launch it like :

./sdu.sh /var/lib/named/master/myzone.com.hosts

or

ls /var/lib/master/*.hosts |xargs -l ./sdu.sh for a massive update

You can also use domains list like : cat zonelist.txt |xargs -l ./sdu.sh[code]

You can also put it directly in your path

The script :

[code]
#!/bin/sh
# Serial DNS Updater by Christophe Casalegno (Brain 0verride)
# christophe.casalegno@digital-network.net

tmpdir="/tmp"
backupdir="/backupdns"
dirlock="/var/run"
datename=`date +"%d-%m-%y-%T"`
zonefile=$1

SCRIPTNAME=`basename $0`

# First verification, process already launched ?

export LOCKSYNC="$dirlock/$SCRIPTNAME.lock"
PID=$$
echo "["`date +"%Y/%m/%d %H:%M:%S"`"] Trying to launch script $0"
if [ -f "$LOCKSYNC" ]
then
echo "Wait : lockfile "$LOCKSYNC" is present. we must verify..."
PIDLOCK=`cat $LOCKSYNC`
if [ -f /proc/$PIDLOCK/exe ]
then
echo "The process is currently running. Launching $SCRIPTNAME aborted."
exit
else
echo "The process doesn't seems currently running anymore. Lauching $SCRIPTNAME"...
rm $LOCKSYNC
fi
fi
echo "Lockingfile creation... "$LOCKSYNC
echo $PID > $LOCKSYNC

# Second verification : backupdir exist ?

if [ -r "$backupdir" ]

then

echo "The backup directory already exist : it's cool, don't worry"
echo "Processing..."
else
echo "The backup directory doesn't exist : Creating backup directory..."
mkdir $backupdir
fixdate=$datename
mkdir $backupdir/$fixdate

fi

cat $zonefile > $backupdir/$fixdate/$zonefile
gawk 'BEGIN{ser=strftime("%Y%m%d",systime() )*100} /[0-9]{9,10}/{sub($1,$1 < ser ? ser : $1+1)}1' "$zonefile" > $tmpdir/$zonefile.tmp
cat $tmpdir/$zonefile.tmp > $zonefile
rm $tmpdir/$zonefile.tmp

You can also download the script from : sdu.sh

--
Christophe Casalegno
https://twitter.com/Brain0verride

Leave a Comment