When you work in a hosting company, DNS is probably one of the services you interact with every week. This script automatically updates one or more DNS zone files. For the serial number, just let Bash do it for you.
Because we rarely work alone in a hosting company, it is important to make sure nobody else is running the script at the same time. It is also important to keep track of changes, so the original files are archived.
Script update
This article was first published in 2015. The principle has not changed, but the script itself has been seriously cleaned up since then. The current version is kept with the StackX tools and remains compatible with the historical command:
sdu <zone-file>
You no longer need to install gawk. The current implementation uses AWK code that works with both mawk and gawk, avoiding an unnecessary dependency on Debian.
Usage
To update one zone:
sdu /var/lib/named/master/myzone.com.hosts
The public file keeps its historical sdu.sh name, so you can also call it directly:
./sdu.sh /var/lib/named/master/myzone.com.hosts
To process several files sequentially:
find /var/lib/named/master -maxdepth 1 -type f -name '*.hosts' -print0 \
| xargs -0 -r -n1 sdu
Or from a list containing one path per line:
xargs -r -n1 sdu < zonelist.txt
What SDU does
The script:
– refuses a missing, unreadable or unwritable file;
– uses flock to prevent concurrent updates;
– backs up the original file under /backupdns/ before changing it;
– works through a temporary file created with mktemp;
– looks for the serial inside the SOA block instead of replacing the first number it happens to find;
– starts from YYYYMMDD00 when the current serial is older, otherwise increments it;
– validates the zone with named-checkzone when that command is installed;
– refuses the update when no SOA serial is found or validation fails.
The success message reports the backup location. The lock is global: the command deliberately processes one zone at a time.
Requirements and precautions
You need Bash, AWK, flock and the usual GNU utilities. named-checkzone is optional in the script but strongly recommended on a DNS server: without it, SDU can verify the presence of the SOA serial, not the complete validity of the zone.
The account running SDU must be able to read and rewrite the zone file, create backups under /backupdns, and open the lock under /var/run. As always with DNS data, verify the actual BIND reload using your own operating procedure afterwards.
Download
You can download the current version here: sdu.sh
--
Christophe Casalegno
https://all.bo

Leave a comment
Add a comment