Infoblox Community
Reply
Highlighted
Accepted Solution

Automate copying zones/records to a different DNS view

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

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: 35
Registered: ‎11-17-2016
Authority
Posts: 35

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: 35
Registered: ‎11-17-2016
Authority
Posts: 35

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: 35
Registered: ‎11-17-2016
Authority
Posts: 35

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: 35
Registered: ‎11-17-2016
Authority
Posts: 35

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

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

Authority
Posts: 16
Registered: ‎05-12-2015
Authority
Posts: 16

Hey Clark...

Just curious if you know of anything changing in curl that would affect your script.  I've been trying to get it to work this past week and just not having any luck.  I keep getting the following errors

 

Processing zone:    1800thezah.com
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
./ibzoneXport.sh: line 52: -u: command not found
Token:              
URL:                
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
Filename:           1800thezah.com.csv
mv: cannot stat 'Zonechilds.csv': No such file or directory
<HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your requested host "gridprod.thezah.corp" could not be resolved by DNS.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">

</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>

</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

 

 

Here's the variables if it helps, otherwise its identical to what you have above

#!/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="dennis"
PASSWORD="superpass"

# Grid Master
SERVER="gridprod.thezah.corp"

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

# Define file that will contain results of curl command
OUTFILE="zahresult.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="External DNS View"

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

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

In the error msg it says "no url specified" so I would check your variables to see if they contain what you think they contain.  I had this same problem when I was writing the script and it turned out one of my variables was not set.  Just before the curl command I used the echo command to list all my variables and I found the problem.

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

[ Edited ]
Authority
Posts: 35
Registered: ‎11-17-2016
Authority
Posts: 35

.

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

[ Edited ]
Authority
Posts: 35
Registered: ‎11-17-2016
Authority
Posts: 35

Another thing I would look at is to check if any of your variables contain a space.  If they contain spaces it will invalidate the URL.   The $VIEW variable contains spaces but you may have just done that for an example view name.  If you need to use spaces you have to escape them.

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

[ Edited ]
Authority
Posts: 35
Registered: ‎11-17-2016
Authority
Posts: 35

Do you need me to post the upload script?