This is the Part 2 of the two part series of Backup of AWS Route53 DNS records and restoring in Azure DNS using an Azure Automation Runbook.
Please refer Part 1 for Backup of DNS records to Azure Blob storage.
You should have following prerequisites set up before you run the Runbook:
- Azure Blob storage containing DNS Backup in JSON format(Refer Part 1)
- An Azure Automation account with permission to the Key Vault , Storage Account and Azure DNS
Keep this Runbook handy when you need to restore DNS records or migrate DNS Hosted zones from AWS to Azure.
Param(
[Parameter(Mandatory=$true)]
[String]$saResourceGroup,
[Parameter(Mandatory=$true)]
[String]$storageAccount,
[Parameter(Mandatory=$true)]
[String]$storageContainer,
[Parameter(Mandatory=$true)]
[String]$dnsResourceGroup
)
#declare some variables to be used in the script
$dirName = $fName = $destPath = ""
#endregion
#region - login to Azure
$connectionName = "AzureRunAsConnection"
try{
#Get the connection 'AzureRunAsConnection'
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch{
if(!$ServicePrincipalConnection){
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
}else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#endregion
#region - Find the lastest backup file to restore from
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $saResourceGroup -Name $storageAccount).Context
$fileName = (Get-AzureStorageBlob -Container $storageContainer -Context $saContext | Sort-Object LastModified -Descending | Select-Object Name -First 1).Name
Write-Output "File Name : $($fileName)"
if([string]::IsNullOrEmpty($fileName)){
Write-Output "ERROR: No file found.Restoration will Terminate now."
Exit
}
if($fileName.Contains("/")){
$dirName = $fileName.Split("/")[0]
$fName = $fileName.Split("/")[1]
}
#download blob contents to local file
$download = Get-AzureStorageBlobContent -Blob $fileName -Container $storageContainer -Context $saContext -Destination $env:TEMP -Force -CheckMd5
if([string]::IsNullOrEmpty($download)){
Write-Output "ERROR: Blob content was not downloaded successfully. Please check the issue.Script will Exit now!"
Exit
}
if(!([string]::IsNullOrEmpty($dirName))){
$destPath = "$($env:TEMP)\$($dirName)\$($fName)"
}
else{
$destPath = "$($env:TEMP)\$($fName)"
}
if(!(Test-Path $destPath)){
Write-Output "ERROR: File was not downloaded to $($destPath) successfully.Script will Exit now!"
Exit
}
$fileData = Get-Content $destPath | ConvertFrom-Json
[PSCustomObject]$fileData | Get-Member -MemberType NoteProperty | ForEach-Object {
$dnsKey = $_.Name
[PSCustomObject]@{Key = $dnsKey; Value = $fileData."$dnsKey"} | ForEach-Object {
$dnsValue = $_.Value
$dnsHostedZone = $dnsKey.TrimEnd(".")
Write-Output "Creating Azure DNS Zone Name - $($dnsHostedZone) in Resource Group $($dnsResourceGroup)"
New-AzureRmDnsZone -Name $dnsHostedZone -ResourceGroupName $dnsResourceGroup
foreach($dnsRecord in $dnsValue){
$recordName = $dnsRecord.RecordSetName
$recordName = $recordName.TrimEnd(".")
$recordType = $dnsRecord.Type
$resourceRecord = $dnsRecord.ResourceRecord
$recordTTL = $dnsRecord.TTL
if($recordType -notin "NS","SOA"){
Write-Output "Adding A DNS Record $($recordName) of Record Type $($recordType) and Resource Records $($resourceRecord) with TTL value as $($recordTTL)"
switch ($recordType.ToUpper()){
"A" {
if($resourceRecord){
$aRecords = @()
foreach($a in $resourceRecord.Split(";")){
$aRecords += New-AzureRmDnsRecordConfig -IPv4Address $a
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType A -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $aRecords
}
}
"PTR" {
if($resourceRecord){
$ptrRecords = @()
foreach($ptr in $resourceRecord.Split(";")){
$ptrRecords += New-AzureRmDnsRecordConfig -Ptrdname $ptr
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType PTR -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $ptrRecords
}
}
"CNAME" {
if($resourceRecord){
$cnameRecords = @()
foreach($cname in $resourceRecord.Split(";")){
$cnameRecords += New-AzureRmDnsRecordConfig -Cname $cname
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType CNAME -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $cnameRecords
}
}
"TXT" {
if($resourceRecord){
$txtRecords = @()
foreach($txt in $resourceRecord.Split(";")){
$txtVal = $txt.replace("\","").replace('""','"')
$txtRecords += New-AzureRmDnsRecordConfig -Value $txtVal
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType TXT -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $txtRecords
}
}
"MX" {
if($resourceRecord){
$mxRecords = @()
foreach($mx in $resourceRecord.Split(";")){
$mxRecords += New-AzureRmDnsRecordConfig -Preference $mx.Split(" ")[0] -Exchange $mx.Split(" ")[1]
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType MX -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $mxRecords
}
}
"SRV" {
if($resourceRecord){
$srvRecords = @()
foreach($srv in $resourceRecord.Split(";")){
$srvRecords += New-AzureRmDnsRecordConfig -Priority $srv.Split(" ")[0] -Weight $srv.Split(" ")[1] -Port $srv.Split(" ")[2] -Target $srv.Split(" ")[3]
}
}
New-AzureRmDnsRecordSet -Name $recordName -RecordType SRV -ZoneName $dnsHostedZone -ResourceGroupName $dnsResourceGroup -Ttl $recordTTL -DnsRecords $srvRecords
}
}
}
}
}
}
#endregion
#region -clean Up File from Temp location
If(Test-Path $destPath){
Remove-Item $destPath -Force
}
#endregion
Login to Azure Portal using https://portal.azure.com and open Azure Automation Accounts from left pane.
Open configured Azure Automation account and select Runbooks from Process Automation option in the left pane.
Create a new Runbook using the script above and save it. Once, it is saved run the Runbook from the list.