Last active
May 16, 2024 21:07
-
-
Save alan-finn/09e9edb65b680b8cd0a24176d6e541e9 to your computer and use it in GitHub Desktop.
Powershell wrapper for the cli53 utility to backup Route53 zones.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
$ToAddress = "RECIPIENT_ADRESS" | |
function Send-Email { | |
param ( | |
$Subject, | |
$Body, | |
$Priority = "Normal", | |
$SmtpServer, | |
$FromAddress, | |
$ToAddress | |
) | |
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) { | |
$Global:LASTEXITCODE = 0 | |
$Error.Clear() | |
$Exe = '.\cli53-windows-amd64.exe' | |
$ArgList = @( | |
"export" | |
$HostedZone.Name.TrimEnd(".") | |
) | |
& $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" | |
return | |
} | |
$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" | |
} | |
#Cleanup | |
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