Backing up Amazon Route 53 DNS Entries with PowerShell

While AWS is very solid, it's nice  to be able to have a backup of your DNS  zones in Route 53, especially if  you're getting ready to make some major changes. Despite looking, I wasn't able to find anything out there that automated this.

The only required changes on your part would be to populate the $AccessKey, $SecretKey, and Email information (If you  plan on shipping the output into mail). Or, you can simply put this information in at run time.

#requires -Version 3.0 -Modules AWSPowerShell, PSCX
Function Backup-AWSRoute53 
    $AccessKey = '',
    $SecretKey = '',
    $EmailSender = 'Mike@Riston.Me',
    $EmailRecipient = 'Mike@Riston.Me',
    $SMTPServer = ''

  Import-Module -Name AWSPowerShell
  Set-AWSCredentials -AccessKey $AccessKey -SecretKey $SecretKey -StoreAs default
  Initialize-AWSDefaults -ProfileName default -Region us-east-1

  #File System Prep
  $StorageFolder = New-Item -Path "C:\scripts\PowerShell\AWS_Route53\$(Get-Date -Format yyyy.MMM.dd-hhtt)" -ItemType Directory 
  $StorageFolder = Get-Item -Path "C:\scripts\PowerShell\AWS_Route53\$(Get-Date -Format yyyy.MMM.dd-hhtt)"

  #Find All R53 Hosted Zones
  $Zones = Get-R53HostedZones

  foreach ($zone in $Zones) 
    #Create Zone File
    $OutFile = New-Item -Path "$($StorageFolder.FullName)\$($zone.Name).csv" -ItemType File
    foreach ($Recordset in (Get-R53ResourceRecordSet -HostedZoneId $zone.Id).ResourceRecordSets) 
      if ($Recordset.ResourceRecords.Count -gt 1) 
        $data = ''
        foreach ($entry in $Recordset.ResourceRecords) 
          $data = $data + $($entry.value) + ','
        $data = ''
        $data = $Recordset.ResourceRecords.Value

      #Generate Custom Object
      $ReturnArray = New-Object -TypeName PSObject -Property (@{
          'Zone'                  = $zone.Name
          'CallerReference'       = $zone.CallerReference
          'Name'                  = $Recordset.Name
          'Type'                  = $Recordset.Type
          'SetIdentifier'         = $Recordset.SetIdentifier
          'Weight'                = $Recordset.Weight
          'Region'                = $Recordset.Region
          'GeoLocation'           = $Recordset.GeoLocation
          'TTL'                   = $Recordset.TTL
          'ResourceRecord'        = $data
          'AliasTarget'           = $Recordset.AliasTarget
          'TrafficPolicyInstanceID' = $Recordset.TrafficPolicyInstanceId
      $ReturnArray | Export-Csv $OutFile -Append

  Get-Item -Path $StorageFolder.FullName | Write-Zip -Level 9 -OutputPath "C:\scripts\PowerShell\AWS_Route53\$($StorageFolder.BaseName).zip" -ErrorAction SilentlyContinue
  $OutputZip = Get-Item -Path "C:\scripts\PowerShell\AWS_Route53\$($StorageFolder.BaseName).zip"
  Get-ChildItem $StorageFolder -Recurse | Write-Zip -Level 9 -OutputPath "C:\scripts\PowerShell\AWS_Route53\$($StorageFolder.BaseName).zip" -Append
  Remove-Item $StorageFolder -Recurse

  if ($sendEmail) 
    Send-MailMessage -SmtpServer $SMTPServer -To $EmailRecipient  -From $EmailSender -Subject 'AWS Route53 Backup' -Attachments  $OutputZip

Until Next Time!


Popular Posts