Microsoft DNS zones backup
Microsoft DNS zones backup

This script uses the DNS powershell cmdlets available since Windows 2012. It exports the DNS zones hosted on a Microsoft DNS servers. First, a csv file is created (csv file called _-_domain_info.csv) with the following zone details:

  • NotifyServers
  • SecondaryServers
  • AllowedDcForNsRecordsAutoCreation
  • DistinguishedName
  • IsAutoCreated
  • IsDsIntegrated
  • IsPaused
  • IsReadOnly
  • IsReverseLookupZone
  • IsShutdown
  • ZoneName
  • ZoneType
  • DirectoryPartitionName
  • DynamicUpdate
  • IsPluginEnabled
  • IsSigned
  • IsWinsEnabled
  • Notify
  • ReplicationScope
  • SecureSecondaries
  • ZoneFile
  • PSComputerName

After that each zone is exported to a csv file with the following properties :

  • DistinguishedName
  • HostName
  • RecordClass
  • RecordData
  • RecordType
  • Timestamp
  • TimeToLive
  • PSComputerName

The cmdlet Set-DnsServerResourceRecord can be used to restore records by parsing the csv files

$domains = "domain.local" , "sub.domain.local"
$BkfFolderRootDom = "\\backup_server\share\backups\DNSbkp-"
$retention = 7
$date = Get-Date -format "yyyy-MM-dd"
$domain_info_output = "_-_domain_info.csv"

$domains | % {
	$ADdomain = $_
	$BkfFolderRoot = $BkfFolderRootDom + $ADdomain
	$BkfFolder = $BkfFolderRoot + "\" + $date
	
	if (-not(test-path $BkfFolderRoot)) {
		new-item $BkfFolderRoot -Type Directory | Out-Null
	}
	
	if (-not(test-path $BkfFolder)) {
		new-item $BkfFolder -Type Directory | Out-Null
	}
	
	Filter Select-FileAge {
		param($days)
		If ($_.creationtime -lt (Get-Date).AddDays($days * -1))
		{$_}
	}
	get-Childitem $BkfFolder -Directory | Select-FileAge $retention |Remove-Item -Force -Recurse
	
	$zones = Get-DnsServerZone -ComputerName $ADdomain
	$zones | export-Csv "$BkfFolder\$CSVZoneExport" -Delimiter ";" -NoClobber -NoTypeInformation
	
	$zones | % {
		$DNSZone = $_.ZoneName
		$CSVZoneExport = $DNSZone+".csv"
		Get-DnsServerResourceRecord -ZoneName $dnszone -ComputerName $ADdomain | export-Csv "$BkfFolder\$domain_info_output" -Delimiter ";" -NoClobber -NoTypeInformation
	}
}
<>
Microsoft DNS zones backup

Leave a Reply

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