Project Name: DNS / BIND named.conf syncronization
Author: Saint Keimond
Date: 01/28/2013

I needed a way to sync my existing DNS server's named.conf file to multiple slave DNS servers. All of my DNS servers are set up with the named-chroot packaged as well.. you must have that or you'll want to modify any paths within the script to match your servers path locations... I'll clean this up later so that variables like root paths and dns server addresses can be set at the top of the file.

For those of you who are starting out with a brand new server I would look in to rndc, it is already a utility that allows you to make modifications to multiple servers. I'm lazy and didn't want to learn something new so for the moment I wrote this script instead. It looks at the servers named.conf and pulls all of the zones from it. (Any zone statement that contains "ZONE" and "MASTER") The script then takes the list of domains and builds a named.conf file for the slave server... USUALLY the named.conf for all of the slave servers is the same. You'll notice that I had the script output to a file for each server.... this wasn't nessesary but I did this so I could make modifications to each server's slave file afterwards if I wanted. (keeping in mind those modifications will be overwritten by the script if it's run again)

If you need special statements in certain server config files it would be easy to write something at the bottom to insert statements in to certain config files before sending them off to the slave servers.

Another item to note is that the server is using passwordless login via SSH to communicate with servers. It would be a very good idea to set up a user account just for this purpose. Make sure permissions are correct to that it can write the named.conf.

#!/bin/sh
cd /var/named/chroot/etc/zones
for server in DNS-SERVER2-V DNS-SERVER3-V DNS-SERVER4-V DNS-SERVER5-V
do cat > /var/named/chroot/scripts/configs/$server.conf <<zEND
###
### THIS FILE WAS AUTO GENERATED FROM A SCRIPT ON HJS-DNS1-V
### IF YOU EDIT IT, IT WILL BE OVERWRITTEN
###
acl ORGANIZATION {
10.40.80.0/24;
10.45.80.0/24;
};
acl DNSSERVERS {
10.40.80.50;
10.40.80.51;
10.45.80.50;
10.45.80.51;
};

options {
statistics-file "/var/named_stats.txt";
directory "/etc/zones";
pid-file "/var/run/named.pid";
recursion yes;
allow-recursion { ORGANIZATION; };
allow-notify { DNSSERVERS; };
allow-transfer { DNSSERVERS; };
version "0.10.2a";
};

##########################################################
#####################
#1# CACHE / ROOTS ###
#####################
zone "." IN { type hint; file "named.root"; };
##############
### .ZONES ###
##############
zEND

getzones="cat /var/named/chroot/etc/named.conf | sed -n '/^zone.*master/p' | gawk '{print \$2}' | sed 's/^\"//' | sed 's/.\"//'"
eval $getzones>/var/named/chroot/scripts/zones.txt
for zone in `cat /var/named/chroot/scripts/zones.txt`; do
echo 'ZONE "'$zone'." IN { TYPE SLAVE; FILE "'$zone'.DS"; masters { 10.40.80.50; }; };'>>/var/named/chroot/scripts/configs/$server.conf
done
echo '#############################################################################'
echo '### Sending a config file to' $server
echo '### config/'$server'.conf --> /var/named/chroot/etc/named.conf'
echo '############################################################################'
echo
scp -q /var/named/chroot/scripts/configs/$server.conf $server:/var/named/chroot/etc/named.conf
echo '############################################################################'
echo '### Restarting named on ' $server
echo '############################################################################'
ssh -q $server 'service named restart'
echo
echo 'I copied config files to each Secondary DNS server and reloaded their DNS.. you should manually check the domain via nslookup against that server to make sure it loaded!'
done
echo 'I processed ' `cat /var/named/chroot/scripts/zones.txt | wc -l` ' zone files.'