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" | |
$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