Wednesday, 20 October 2010

DNS replication through SVN and script to check configuration files

I have made the script below to replicate changes from DNS SVN repository to each of several BIND DNS servers.


#!/bin/bash

# dhs-update.sh
# by Andrey Ivanov
# v 0.2 DD 20101015
# check if any updates of DNS zones on SVN server and, if any
# updates temporary named files, copy to config directory
# restart named and send report to Administrator
#

# Variables
# Can be many addresses separated by whitespaces
admin="mymail@company.com"
# DNS server name to place in report mail. May be change to result of hostname command?
server="server1"
DB=`date '+%c'`
#Working path
wp="/root"
#SVN path
rp="/root/dns"


cd $rp
CODE="$( svn status -u |wc -l )"
if [ $CODE -gt 1 ]; then
echo $DB > $wp/dns-report.txt
/usr/bin/svn update>>$wp/dns-report.txt 2>&1
if [ "$?" -ne "0" ]; then
mail -s "DNS-ERROR: SVN update error !" $admin<$wp/dns-report.txt 2>&1
exit 1
fi
# Check named.conf
/usr/sbin/named-checkconf $rp/etc/named.conf>>$wp/dns-report.txt 2>&1
if [ "$?" -ne "0" ]; then
mail -s "DNS-ERROR: Sorry, named.conf syntax error!" $admin<$wp/dns-report.txt 2>&1
exit 1
else
# Check all files for zones
list=`find $rp/named/ -type f -prune -name \*.hosts`
for i in $list
do
# echo "Processing $i...">>$wp/dns-report.txt 2>&1
zone=`grep SOA $i|awk '{print $1}'`
/usr/sbin/named-checkzone $zone $i>>$wp/dns-report.txt 2>&1
# Error processing
if [ "$?" -ne "0" ]; then
mail -s "DNS-ERROR: Sorry, zone file syntax error!" $admin<$wp/dns-report.txt 2>&1
exit 1
fi
done
/usr/bin/rsync -r --exclude=".*/" --delete /root/dns/etc/* /var/named/chroot/etc/>>$wp/dns-report.txt 2>&1
/usr/bin/rsync -r --exclude=".*/" --delete /root/dns/named/* /var/named/chroot/var/named/>>$wp/dns-report.txt 2>&1
chown -R named:named /var/named/chroot/etc/>>$wp/dns-report.txt 2>&1
chown -R named:named /var/named/chroot/var/>>$wp/dns-report.txt 2>&1
/etc/init.d/named restart
tail -n 200 /var/log/messages|grep named|grep "zone/IN">>$wp/dns-report.txt 2>&1
/usr/sbin/rndc status>>$wp/dns-report.txt 2>&1
mail -s "DNS update for $server at $DB" $admin<$wp/dns-report.txt 2>&1
fi
else
echo "Checked at $DB. No updates">>/$wp/dns-check.log
fi

No comments: