Nish
Nish

Reputation: 83

How to Automate Azure DNS zones backup into Storage account using PowerShell runbook

I am trying to automate the Azure DNS Zones Backup into Azure Storage account using automation account.I wanted to create a PowerShell runbook which gets all DNS zones and export the zone file of zones and upload them into storage account.

#Authentication
Connect-AzAccount
Set-AzContext -Subscription mysubscriptiom

#Variables
$storageAccountName = "dnszonesbackup"
$storageAccountKey = "key"
$containerName = "dns-backups"

#Logic
$context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$zones = Get-AzDnsZone
foreach ($zone in $zones) {
    $zoneFile = "Here i am stuck, How to export a zone files of DNS Zones and pass into blob storage"
    $blobName = $zone.Name + ".zone"
    Set-AzStorageBlobContent -Context $context -Container $containerName -File $zoneFile -Blob $blobName
}

However when I am trying export the zone file I couldn't find any export cmdlet. Is there any possible way to achieve this.

Upvotes: 0

Views: 891

Answers (1)

Venkat V
Venkat V

Reputation: 7669

However when I am trying export the zone file I couldn't find any export cmdlet. Is there any possible way to achieve this.

I have uploaded Azure DNS information to Azure Blob using an automation runbook.

Note: You can schedule the script to run automatically at a specific time to upload the Azure DNS information to Azure blob.

Powershell Code.

$Appid = "APPID"
$PSWord = ConvertTo-SecureString -String "APSecret" -AsPlainText -Force
$tenantID = "TenantID"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Appid,$PSWord
Connect-AzAccount -Credential $Credential -Tenant   $tenantID -ServicePrincipal -Subscription "SubscriptionID"
Set-AzContext -Tenant $tenantID
$storageAccountName = "storageaccount name"
$storageAccountKey = "storageaccount key"
$containerName = "containername"
$context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$zones = Get-AzDnsZone
foreach ($zone in $zones) {
    $recordSets = Get-AzDnsRecordSet -ZoneName $zone.Name -ResourceGroupName $zone.ResourceGroupName
    $json = $recordSets | ConvertTo-Json
    $jsonR = ConvertFrom-Json $json
    $jsonR | Export-Csv "DNS.csv" -Append -NoTypeInformation
  $result = Set-AzStorageBlobContent -Context $context -Container "venkat" -File "DNS.csv" -Blob "DNS.csv" -Force
}
$result

Automation runbook output:

enter image description here

After running the code from the azure automation account, the file was successfully uploaded to the Azure blob.

enter image description here

Thanks to Anthony Norwood for suggesting the same.

Upvotes: 1

Related Questions