Infoblox Community
Reply
Highlighted
Accepted Solution

Automate copying zones/records to a different DNS view

Authority
Posts: 29
Registered: ‎11-17-2016
Authority
Posts: 29

I’m playing with DNS views in my lab environment and would like to know if there is an easy and convenient way to copy entire zones from one DNS view to another.  I have a total of 52 zones I need to copy to a different view.  One of the zones will need to have some significant modifications before importing into the new view.  The rest of the zones can be copied over as-is.   I will be doing this many times in the process of finalizing a process to modify our production grid.

 

Right now I’m exporting all 2,000 zones to a CSV file and using a text editor to remove the zones I don’t want to copy.  I run a search and replace on the DNS view name, then I import this CSV into the new DNS view.  This creates the “empty” zones.  I have to manually create a CSV export file for each of the 52 zones in order to populate the records in these zones.  This is tedious and time consuming.

 

I searched the web for some ideas on how to automate this process and found the ibcli package.  It doesn’t look like it’s supported or updated any more.  I also checked out the restful api.  It looks like objects can be created and updated.  I’m pretty sure the perl api can accomplish this too.

 

Automating the process of copying zones to a different DNS view seems like a fairly common thing to do.  Before I sit down and code something, I’d like to see if someone else has already done this.  I don’t want to reinvent the wheel if I don’t have to.  If anybody has any information on this I’d appreciate it.  Otherwise, I’ll have to code something from scratch.

 

Thank you,

Clark

Re: Automate copying zones/records to a different DNS view

Authority
Posts: 29
Registered: ‎11-17-2016
Authority
Posts: 29

Looks like I should have created this post in the "API and Integration" section.  Can someone move it?

Re: Automate copying zones/records to a different DNS view

Authority
Posts: 29
Registered: ‎11-17-2016
Authority
Posts: 29

I've written a bash shell script which uses the RESTful API to do what I need.  I have one minor issue with the upload script.  When I get that worked out I will post the script(s) here.

Re: Automate copying zones/records to a different DNS view

Authority
Posts: 29
Registered: ‎11-17-2016
Authority
Posts: 29

Here is a bash shell script that uses the RESTful API to mass download zone files in csv format.  No need to have perl or python etc.

 

#!/bin/bash -

# This script reads a file, zonelist.txt, that contains a 
# list of zone files to download.  A separate csv file will be
# created for each zone.  The csv file will be named the
# same as the zone name.  Minimal error checking is
# performed.  Use at your own risk.
# All files are located in the same directory as this script.

# Username and password with permission to download csv files
USERNAME="admin"
PASSWORD="password"

# Grid Master
SERVER="gm.example.com"

# Define file containing list of zones to export
ZONELIST="zonelist.txt"

# Define file that will contain results of curl command
OUTFILE="result.txt"

# Location of curl on this system.  Use -s so curl is silent
CURL="/usr/bin/curl -s"

# WAPI version
VERSION="v2.3"

# What view are these zone in?
VIEW="default"

############################################
# No more variables to set below this line #
############################################

# Process the zonelist file one line at a time
while read ZONE
do

   echo
   echo
   echo
   echo
   echo
   echo "Processing zone:    $ZONE"

   # Create CSV file for this zone
   $CURL \
      --tlsv1 \
      --insecure \
      -u "$USERNAME:$PASSWORD" \
      -H "Content-Type: application/json" \
      -X POST https://$SERVER/wapi/$VERSION/fileop?_function=csv_export \
      -d "{\"_object\":\"allrecords\",\"view\":\"$VIEW\",\"zone\":\"$ZONE\"}" \
      > $OUTFILE

   ERROR_COUNT=`grep -c Error $OUTFILE`
   if [ $ERROR_COUNT -gt 0 ]; then
      # Display the error and skip rest of loop
      grep Error $OUTFILE
      continue
   fi

   # Get the "token" and "download URL" for later use
   TOKEN=`grep "token" $OUTFILE | cut -d"\"" -f4`
   URL=`  grep "url"   $OUTFILE | cut -d"\"" -f4`
   echo "Token:              $TOKEN"
   echo "URL:                $URL"

   # Download the CSV file
   $CURL \
      -k1 \
      -u "$USERNAME:$PASSWORD" \
      -H "Content-Type: application/force-download" \
      -O $URL

   # Rename CSV file so the file name matches the zone name
   FILENAME="$ZONE.csv"
   # Reverse zones will contain the / character which will be interpreted
   # as a directory delimiter if included in file name.  Replace with +
   FILENAME=`echo $FILENAME | tr \/ +`
   echo "Filename:           $FILENAME"
   mv Zonechilds.csv $FILENAME

   # Let NIOS know download is complete
   $CURL \
      -k1 \
      -u "$USERNAME:$PASSWORD" \
      -H "Content-Type: application/json" \
      -X POST https://$SERVER/wapi/$VERSION/fileop?_function=downloadcomplete \
      -d "{ \"token\": \"$TOKEN\"}"

done < "$ZONELIST"

exit

Re: Automate copying zones/records to a different DNS view

Techie
Posts: 6
Registered: ‎11-20-2015
verne
Techie
Posts: 6

did you get the bugs worked out of your upload script ?

 

Re: Automate copying zones/records to a different DNS view

Authority
Posts: 29
Registered: ‎11-17-2016
Authority
Posts: 29

Yes, I did get the bugs worked out.  I plan on posting all my scripts in this forum.