Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active September 26, 2023 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Rugby-Ball/951e5bc7399de4b2256b2f74a3da6396 to your computer and use it in GitHub Desktop.
Save Rugby-Ball/951e5bc7399de4b2256b2f74a3da6396 to your computer and use it in GitHub Desktop.
Backs up all AWS Route 53 Hosted Zones into individual HostedZone JSON files. #Utility #AWS #Route_53 #Backup #Public
# rt53-hosted-zone-entries-backup.ps1
<#
Description: Backs up all AWS Route 53 Hosted Zones into individual HostedZone JSON files.
Written: Ed Walsh
PowerShell.Core tested: Not Tested
MS-Graph: No
Version: 2.0.0
Create Date: 11/17/2020
Revised Date: 11/16/2022
#>
# Set the TLS to 1.2
[Net.ServicePointManager]::SecurityProtocol = "tls12"
# Check for and Install Powershell module if it is missing
if((Get-Module -Name AwsPowershell).count -gt 0){
Write-Output "AWS Powershell module 'AwsPowershell' is already installed"
}else{
Write-Output "AWS Powershell module 'AwsPowershell' is missing. Installing the module now"
Import-Module -Name AwsPowershell -Force
}
# Set the Date variable.
$date = Get-Date -UFormat "%m-%d-%Y_%H%M%S"
$jsonData = @{}
#Find All R53 Hosted Zones
try{
$Zones = Get-R53HostedZones #-ProfileName AWSProfile
foreach ($zone in $Zones)
{
$Zonename = ($zone).Name.Replace('.','_').TrimEnd("_")
# Set the Path for output files.
$outfilepath = "$home\rt53-backup\"
## Check if $outfilepath exists, if it doesnt create it.
If(-not(Test-Path -Path $outfilepath))
{New-Item -ItemType Directory -Force -Path $outfilepath}
# Set the file name variable
$outFileName = "r53Backup-$($ZoneName)-$($date).json"
# Set the Full Path and file name variable
$outJsonFile = Join-Path $outfilepath $outFileName
if(Test-Path $outJsonFile){
Remove-Item $outJsonFile -Force
}
New-Item -Path $outJsonFile -ItemType File
#Iterate through each hosted zone and generate JSON file with each record
$recordDataSets = @()
foreach ($Recordset in (Get-R53ResourceRecordSet <# -ProfileName AWSProfile #> -HostedZoneId $zone.Id).ResourceRecordSets)
{
$data = ""
if ($Recordset.ResourceRecords.Count -gt 1)
{
foreach ($entry in $Recordset.ResourceRecords)
{
$data += $($entry.value) + ";"
}
}
else
{
$data = $Recordset.ResourceRecords.Value
}
if($data){$data = $data.TrimEnd(";")}
#Generate Custom Object of record sets
$recordDataSet = @{}
$recordDataSet.Add('RecordSetName',$Recordset.Name)
$recordDataSet.Add('CallerReference',$Recordset.CallerReference)
$recordDataSet.Add('Type',$Recordset.Type)
$recordDataSet.Add('SetIdentifier',$Recordset.SetIdentifier)
$recordDataSet.Add('Weight',$Recordset.Weight)
$recordDataSet.Add('Region',$Recordset.Region)
$recordDataSet.Add('GeoLocation',$Recordset.GeoLocation)
$recordDataSet.Add('TTL',$Recordset.TTL)
$recordDataSet.Add('ResourceRecord',$data)
$recordDataSet.Add('AliasTarget',(($Recordset.AliasTarget).DNSName)) # Original code from website would not put in info, it would be blank. Updated to drop in the correct info.
$recordDataSet.Add('TrafficPolicyInstanceID',$Recordset.TrafficPolicyInstanceId)
$recordDataSets += $recordDataSet
} # RecordSet
$jsonData | Add-Member -Type NoteProperty -Name $zone.Name -Value $recordDataSets
$jsonData | ConvertTo-Json | Out-File $outJsonFile
$jsonData = @{}
} # Zone
}
catch{
Write-Error -Message $_.Exception
throw $_.Exception
}
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
Write-Output "Exported to: $outfilepath"
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment