Skip to content

Instantly share code, notes, and snippets.

Last active May 2, 2019 20:33
What would you like to do?
Powershell wrapper for the cli53 utility to backup Route53 zones.
if ($null -eq (Get-Module -Name AWSPowerShell)) {
Import-Module AWSPowerShell
$AwsDnsBucketName = "NAME_OF_AWS_BUCKET"
$AwsCredProfileName = "NAME_OF_AWS_PROFILE"
$SmtpServer = "SMTP_SERVER"
$FromAddress = "FROM_ADDRESS"
function Send-Email {
param (
$Priority = "Normal",
Send-MailMessage -SmtpServer $SmtpServer -From $FromAddress -To $ToAddress -Subject $Subject -Body $Body -Priority $Priority
Set-AWSCredential -ProfileName $AwsCredProfileName -ProfileLocation "$PSScriptRoot\.aws\credentials"
Set-Location -Path $PSScriptRoot
$HostedZoneList = Get-R53HostedZoneList
$TargetDirectoryName = (Get-Date).ToString("MM-dd-yyyy")
if (-not(Test-Path -Path "$PSScriptRoot\$TargetDirectoryName" -PathType Container)) {
$TargetDirectoryObj = New-Item -Path "$PSScriptRoot\$TargetDirectoryName" -ItemType Directory
foreach ($HostedZone in $HostedZoneList) {
$Exe = '.\cli53-windows-amd64.exe'
$ArgList = @(
& $Exe $ArgList | Tee-Object -Variable cmdOutput
if ($LASTEXITCODE -ne 0) {
$Msg += "`r`nERROR: $($Error[0].Exception.Message)`r`nFailed on zone $($HostedZone.Name.TrimEnd(".")).`r`n`r`n$($env:COMPUTERNAME) - $(Get-Date)"
Send-Email -Body $Msg -Subject "AWS Route53 Backup Script FAILED" -Priority "High"
$cmdOutput | Out-File -FilePath "$($TargetDirectoryObj.FullName)\$($HostedZone.Name.TrimEnd(".")).txt" -Force
$Msg += "Enumerated $($HostedZone.Name.TrimEnd("."))`r`n"
try {
Write-S3Object -BucketName $AwsDnsBucketName -KeyPrefix $TargetDirectoryName -Folder $TargetDirectoryObj.FullName
$Msg += "`r`nSuccessfully copied $TargetDirectoryName to $AwsDnsBucketName S3 bucket."
$Msg += "`r`nScript completed successfully.`r`n`r`n$($env:COMPUTERNAME) - $(Get-Date)"
Send-Email -Body $Msg -Subject "AWS Route53 Backup Script Completed"
catch {
$Msg += "`r`nFailed to write zones from server to AWS bucked.`r`nERROR: $($_.Exception.Message).`r`n`r`n$($env:COMPUTERNAME) - $(Get-Date)"
Send-Email -Body $Msg -Subject "AWS Route53 Backup Script FAILED" -Priority "High"
foreach ($Folder in (Get-ChildItem -Path $PSScriptRoot -Directory)) {
if ($Folder.Name -notmatch ".aws") {
if ([datetime]$Folder.Name -lt (Get-Date).AddDays(-60).ToString("MM-dd-yyyy")) {
Remove-Item -Path $Folder.FullName -Force
Get-S3Object -BucketName $AwsDnsBucketName -KeyPrefix $TargetDirectoryName | Remove-S3Object -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment