PowerDNS Zone backups

There is a wonderful bash script to back up PowerDNS zone files, written by the author who posts as ‘msb’, here:

https://stack-source.com/2021/01/25/powerdns-zone-backups/

I am reproducing it here because I found it very useful for keeping text version backups of some of zone files which are stored in a mysql database by powerdns.

I’ve updated the script to make a few modifications. Namely, I would like to store each set of daily backups to a separate folder. Here is my updated script:

#!/bin/bash

# creates dump/export/backup of all DNS zones

today=`date +%Y%m%d`

if [ ! -d /info/backup/zones/$today ]; then
  if [ ! -d /info/backup/zones/$today ]; then
    mkdir /info/backup/zones/$today
  fi
  mkdir /info/backup/zones/$today
fi

zones=(`/usr/bin/pdnsutil list-all-zones`)

for z in "${!zones[@]}"
do

  /usr/bin/pdnsutil list-zone ${zones[$z]} > "/info/backup/zones/$today/${zones[$z]}-$today.zone"

done

If you compare the two scripts, you can see that I moved the ‘today’ variable higher up in the script so it can be used to create subdirectories for the daily backup when the pdnsutil command is used near the bottom.

When the script is run, it will now create a subdirectory in the backup directory for today’s date and place all the zone files with a timestamp in that directory.

I also chose to remove the find command and place it separately on its own as a cronjob.