mydns – slave DNS server from master ISPConfig3 database

I wrote a litle bash script for mydns DNS server to act as slave DNS server.
All records are transfered from master ISPConfig 3 Server.

On second server install mydns server, and configure it for ISPConfig use.
then edit file pico /etc/mydns.conf and replace lines
soa-where = server_id = 2 # or any other number
rr-where = server_id = 2 # or any other number

with server id of master server , in my case 1
soa-where = server_id = 1 # Extra WHERE clause for SOA queries
rr-where = server_id = 1 # Extra WHERE clause for RR queries

all user names and password can be found in
/usr/local/ispconfig/server/lib/config.inc.php file

note: you can manualy transfer 1 one zone by issue: dns_transfer.sh zone_id
zone ID you can find in ispconfig database in table dns_soa

save this script in eg: /root/dns_transfer.sh
chmod u+x /root/dns_transfer.sh

and insert in crontab (scrip will be executed every 10 min):
*/10 * * * * root /root/dns_transfer.sh

logfile is created in /var/log/dns_transfer/

please replace needed data and email address.
email address is used when some error generated.

here is the script:

#!/bin/bash
#(c) 2009 by franz@franz-net.info
#
h_x=master_hostname
u_x=master_database_user_name
p_x=master_database_password
db_x=dbispconfig
#
u2_x=local_database_user_name
p2_x=local_database_password
db2_x=local_dababase_dbispconfig2
#
TMP=/tmp/dns_transfer.tmp
path=/var/log
logfile=$path/dns_transfer/ISPconfig3-dns_transfer-`date +'%F'`
error=/tmp/error.log
zamail=/tmp/zamail$datum_x.txt
#Email adderss
email="email@example.com"
#
function check_log {
if [ -d $path/dns_transfer ]; then
log "Log directory exist"
else
mkdir $path/dns_transfer
log "Log directory creted"
fi
}
#
function log {
curenttime_x=`date +"+%Y-%m-%d %H:%M:%S"`
echo "$curenttime_x - $1 $2 $3 $4 $5 $6 $7 $8 $9" >> $logfile
}
#
function sendmail {
echo "" >> $error
echo "for manual retransfer execute '$0 $1'" >> $error
log "Sending mail: `cat $error`"
mail -s"ERROR - DNS transfer on `hostname`" $email < $error } # function transfer { echo "delete from dns_soa where id = $1;" > $TMP
mysqldump --no-create-info -u$u_x -p$p_x -h$h_x $db_x dns_soa -w " id = $1 " >> $TMP 2>> $error
exitcode=$?
if [ $exitcode != 0 ]; then
log "Error connecting to database for dns_soa, ID: $1, or something..."
sendmail $1
exit 1
fi
log "data fatched dns_soa.id=$1."
mysql -u$u2_x -p$p2_x $db2_x < $TMP 2>>$error
exitcode=$?
if [ $exitcode != 0 ]; then
log "Error while inserting data to table dns_soa for id: $1..."
sendmail $1
exit 1
fi
log "Data imported for dns_soa.id=$1"
echo "delete from dns_rr where zone = $1;" > $TMP
mysqldump --no-create-info -u$u_x -p$p_x -h$h_x $db2_x dns_rr -w " zone = $1 " >> $TMP 2>> $error
exitcode=$?
if [ $exitcode != 0 ]; then
log "Error connecting to database for dns_rr, ID: $1, or something..."
sendmail $1
exit 1
fi
log "data fatched dns_rr.id=$1."
mysql -u$u2_x -p$p2_x $db2_x < $TMP 2>> $error
exitcode=$?
if [ $exitcode != 0 ]; then
log "Error while inserting data to table dns_rr, for id: $1..."
sendmail $1
exit 1
fi
log "Data imported for dns_rr.id=$1"
log "Done for ID: $1"
}
#############
# S T A R T #
#############
check_log
log "Start dns_transfer"
#taking all zone ID's
if [ $1 ]; then
log "Manual transfer dor zone $1"
transfer $1
exit 0
fi
ID=`mysql -u$u_x -p$p_x -h$h_x $db_x -Bse 'select id from dns_soa'`
#chechink if all id's have the same serial, if not, delete all data fopr this serial, and inport new one
for id in $ID; do
echo "Start ID=$id" > $error
serial=`mysql -u$u_x -p$p_x -h$h_x $db_x -Bse "select serial from dns_soa where id = $id"`
serial2=`mysql -u$u2_x -p$p2_x $db2_x -Bse "select serial from dns_soa where id = $id"`
if (( serial != serial2 )); then
transfer $id
else
log "No Change for ID: $id"
fi
done

This entry was posted in ISPConfig 3, Linux tips and tagged , , . Bookmark the permalink.

3 Responses to mydns – slave DNS server from master ISPConfig3 database

  1. Ketama says:

    thanks for this script. Ill give thanks later @ sourceforge too you too.

    I was planning on making this script, this saves me a lot of work.

  2. Flevo ict says:

    Great script.

    But I only have one problem. When I delete a record, for example an A record, it doesn’t replicate it to the other servers. I have to delete them manually.

    Do you have a solution for this problem or did I do something wrong.

    Cheers
    Flevo ict

  3. admin says:

    It should replicate records, becouse I search if SOA serial is changed, and replicate all data from master (first delete all data from slave) .

Leave a Reply

Your email address will not be published. Required fields are marked *