Menü schliessen
Created: January 16th 2017
Last updated: May 1st 2020
Categories: Linux,  Miscellaneous Online Services
Author: Marcus Fleuti

BuddyNS - Script to reinitialize and resync all domains (zones) with BuddyNS & Bind

Tags:  bind,  buddyNS,  curl,  DNS,  jq,  named,  script,  secondary DNS,  Virtualmin
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Why reinitialization?

It might happen that the BuddyNS zonedata is out of sync with your local system or that you would like to clean BuddyNS zone data from unused domains. We've created a script which automates this procedure. It does the following:

  1. Fetch a list of all zones hosted on BuddyNS. This is mandatory because the BuddyNS API does not support a "DELETE ALL" command
  2. Go through the list of all zones on BuddyNS and delete every zone individually
  3. Fetch a list of all zones being used by your local BIND service
  4. Go through the list of your local zones and add each and everyone of it on BuddyNS
  5. Resync all zones

Please use at your own risk! We tested it several times and it worked nicely on our Debian server.

#! /bin/bash
MASTER_NS=IP.OF.YOUR-MASTER.DNS
PATH_TO_BIND_CONFIG_FILES=/etc/bind
TOKEN="Enter Your BuddyNS Token Here. Go to BuddyNS account settings to generate one"
if [ -z "$1" ] ; then
        echo "This script removes ALL ZONES from BuddyNS secondary nameserver, fetches a list of all zones currently set up on the MASTER, recreates all these zones on BuddyNS and resyncs zone data - be careful!!!:"
        echo "Syntax: $0 ok_go4it"
        echo ""
        exit 1
else
        if [ "$1" = "ok_go4it" ]; then
                ### Fetch a list of all zones on BuddyNS
                declare -a arr_buddyns_zonenames="($(curl -ss -H 'Authorization: Token ${TOKEN}' -XGET https://www.buddyns.com/api/v2/zone/ | jq '.[].name_idn' -r))"
                ### Iterate through that list and delete every single entry
                for currentzonename in ${arr_buddyns_zonenames[@]}; do
                        ### Remove DNS entry from BuddyNS list
                        echo "Deleting zone from BuddyNS: ${currentzonename}..."
                        curl -ss -H "Authorization: Token ${TOKEN}" -XDELETE https://www.buddyns.com/api/v2/zone/${currentzonename} |jq ".[]"
                done
                ### Fetching a list of all Bind DNS master zones from the DNS master...
                declare -a arr_masterns_zonenames="($(ls ${PATH_TO_BIND_CONFIG_FILES}/*.hosts | while read zf; do basename $zf .hosts ; done))"
                 ### Iterate through that list and add every single entry on BuddyNS
                for currentzonename in ${arr_masterns_zonenames[@]}; do
                        ### Remove DNS entry from BuddyNS list
                        echo "Adding zone to BuddyNS: ${currentzonename}..."
                        curl -ss -H "Authorization: Token ${TOKEN}" -XPOST -F "name=${currentzonename}" -F "master=$MASTER_NS" https://www.buddyns.com/api/v2/zone/ |jq ".[]"
                done
                ### Force sync of all zone files NOW
                echo "Forcing immediate RESYNC of all zones ..."
                curl -ss -H "Authorization: Token ${TOKEN}" -XGET https://www.buddyns.com/api/v2/sync/ |jq ".[]"
        fi
fi