SKYCUBE.net

Solutions for Go, MySQL, PHP, Linux and more

synchronize and copy bind9 zone files between servers

Posted — Oct 29, 2013

Running name servers is sometime a funny job as well as keeping them in sync. A usual and simple setup for smaller hosting providers is two separate servers (preferred Linux) which running a bind9 name server service. As we know as good name server admins, synchronizing can be done automatic via Master and Slave setup.

The solution below discusses a simple Master/Master solution where both servers have the same file and that just via rsync/ssh and a cron tab.

Our servers:

What we simply need is a little script which I for testing purposes saved in

/root/bind9sync.sh

on the first server ns1.skycube.net.

NOTE: You have to setup private/public key authentication first!

#!/bin/bash
################################################
#
# script to sync bind9 configs
# @author: Per Lasse Baasch
# @Version: 2013-10-29
# NOTE: PRIVATE KEY AUTHENTICATION IS REQUIRED
# FOR AUTOMATIC SSH
#
################################################
# Binary paths
RSYNCBIN=/usr/bin/rsync
SSHBIN=/usr/bin/ssh
LOGFILE=/var/log/bind9sync.log
#### config
# YES appending SLASHES!!!!
LOCAL_PATH=/etc/bind/
# NO  appending SLASHES!!!!
REMOTE_HOST='ns2.skycube.net'
REMOTE_PATH='/etc/bind'
REMOTE_BIND9RELOADCMD='/etc/init.d/bind9 reload' 
#### DO THE SYNC
# rsync -avz --delete /etc/bind/ -e ssh $REMOTE_HOST:/etc/bind
result=$($RSYNCBIN -aiz --delete $LOCAL_PATH -e $SSHBIN $REMOTE_HOST:$REMOTE_PATH);
count=${#result};
### IF something been transferred, reload the bind on remote host
if [ $count -gt 5 ]
then
  ### RELOAD BIND
  date >> $LOGFILE;
  echo $result >> $LOGFILE;
  echo "TRY To RELOAD Bind on $REMOTE_HOST" >> $LOGFILE;
  $SSHBIN $REMOTE_HOST exec "$REMOTE_BIND9RELOADCMD" >> $LOGFILE;
  echo "-----" >> $LOGFILE;
fi

And to do all above every 5 minutes edit your cron tabs via

crontab -e

and paste in the bottom the following (assuming you saved the file in /root/bind9sync.sh

# Sync NS every 5 min
*/5 * * * * /root/bind9sync.sh > /dev/null 2>&1