Backup/Restore all DNS zones

These scripts backup and restore all DNS zones on specific DNS server (source).
The backup script uses the Powershell cmdlet get-wmiobject to list the DNS zone present in the WMI database and then export each zone using the dnscmd tool.
The restore script uses the dnscmd tool to create the zone and his content.

Useful link on how to export and import AD integrated zone files.

Backup script :

#——————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4 - Intergrated comments from Jeffrey Hicks
# Date : October 2011
# Created by Griffon
#——————————————————————————————-##– DEFINE VARIABLE——#
# Get Name of the server with env variable

$DNSSERVER=get-content env:computername

#—Define folder where to store backup  —–#
$BkfFolder=”c:\windows\system32\dns\backup”

#—Define file name where to store Dns Settings
$StrFile=Join-Path $BkfFolder “input.csv”

#—-Check if folder exists. if exists, delete contents–#
if (-not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {

Remove-Item $BkfFolder”\*” -recurse
}

#—- GET DNS SETTINGS USING WMI OBJECT ——–#
#– Line wrapped should be only one line –#
$List = get-WmiObject -ComputerName $DNSSERVER
-Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

#—-Export information into input.csv file —#
#– Line wrapped should be only one line –#
$list | Select Name,ZoneType,AllowUpdate,@{Name=”MasterServers”;Expression={$_.MasterServers}},
DsIntegrated | Export-csv $strFile -NoTypeInformation

#— Call Dnscmd.exe to export dns zones
$list | foreach {
$path=”backup\”+$_.name
$cmd=”dnscmd {0} /ZoneExport {1} {2}” -f $DNSSERVER,$_.Name,$path
Invoke-Expression $cmd
}

# End of Script
#——————————————————————————————-#

 

Restore script :

#———————————————————————-#
# Script_Name : RestoreDNS.ps1
# Description : Perform Restore of DNS Zones of a Windows 2008 DNS SErver
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4
# Date : October 2011
# Created by Griffon
#———————————————————————–##– DEFINE VARIABLE——#
#———————————#
# Get Name of the server with env variable

$DNSSERVER=gc env:computername

#—DEFINE WHERE TO Look for DNS BACKUP FILES —–#

$BkfFolder=”c:\windows\system32\dns\backup”

#—Define file name where Dns Settings are Stored

$StrFile=Join-Path $BkfFolder “input.csv”

#—-  RESTORE ZONES BASED ON SETTINGS FOUND IN INPUT.CSV —— #

$Zone=import-csv $StrFile
$Zone | foreach {

$path=”backup\”+$_.name
$Zone=$_.name
$IP=$_.MasterServers
$Update=$_.AllowUpdate

#—– Checking if AD Integrated or Not ——-#if ($_.DsIntegrated -eq $True) {
Switch ($_.ZoneType)
{
1 {
#—– Need to Create Zone As Primary to get all records imported ——-#
$cmd0=”dnscmd {0} /ZoneAdd {1} /primary /file {2} /load” -f $DNSSERVER,$Zone,$path
Invoke-Expression $cmd0
$cmd1=”dnscmd {0} /ZoneResetType {1} /dsprimary” -f $DNSSERVER,$Zone
}

3 { $cmd1=”dnscmd {0} /ZoneAdd {1} /dsstub {2} /load” -f $DNSSERVER,$Zone,$IP }
4 { $cmd1=”dnscmd {0} /ZoneAdd {1} /dsforwarder {2} /load” -f $DNSSERVER,$Zone,$IP }
}
} else {

Switch ($_.ZoneType)
{
1 {$cmd1=”dnscmd {0} /ZoneAdd {1} /primary /file {2} /load” -f $DNSSERVER,$Zone,$path}
2 {$cmd1=”dnscmd {0} /ZoneAdd {1} /secondary {2}” -f $DNSSERVER,$Zone,$IP }
3 {$cmd1=”dnscmd {0} /ZoneAdd {1} /stub {2}” -f $DNSSERVER,$Zone,$IP }
4 {$cmd1=”dnscmd {0} /ZoneAdd {1} /forwarder {2}” -f $DNSSERVER,$Zone,$IP }
}
}

#Restore DNS Zones
Invoke-Expression $cmd1

Switch ($_.AllowUpdate)
{
#No Update
0 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}
#Secure and non secure
1 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}
#Only Secure Updates
2 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}

}

#Reset DNS Update Settings
Invoke-Expression $cmd2

}

# End of Script
#———————————————————————–#

Backup/Restore all DNS zones

Leave a Reply

Your email address will not be published. Required fields are marked *