Last active
September 14, 2017 13:49
-
-
Save Sam-Martin/a532770f3b206d0585f6220c1dfcef74 to your computer and use it in GitHub Desktop.
Copy All snapshot for the last 24hrs to another region
This file contains hidden or 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
$Env:AWSAccountID = "ACCOUNTID" | |
$Env:SourceRegion = "eu-central-1" | |
$ENV:DestinationRegion = "eu-west-1" | |
$env:AgeOfSnapshotsDays=1 | |
$SourceSnapshots = Get-EC2Snapshot -region $Env:SourceRegion -OwnerId $Env:AWSAccountID | |
$SourceFiltered = $SourceSnapshots | ?{$_.starttime -gt (get-date).AddDays(-1*$env:AgeOfSnapshotsDays)} | |
Write-Verbose "Found $($SourceFiltered.count) snapshots to copy from $Env:SourceRegion to $Env:DestinationRegion" | |
$DestinationSnapshots = Get-EC2Snapshot -Region $Env:DestinationRegion -OwnerId $Env:AWSAccountID | |
foreach($snapshot in $SourceFiltered){ | |
if($DestinationSnapshots | ?{$_.description -like "*$($snapshot.snapshotid)*"}){ | |
Write-Verbose "$($snapshot.SnapshotId) already copied to $Env:DestinationRegion" | |
continue | |
} | |
$Description = "[Copied $($snapshot.SnapshotId) from $Env:SourceRegion] $($snapshot.Description)" | |
while(1){ | |
try{ | |
$NewSnapshotID = Copy-EC2Snapshot -SourceSnapshotId $snapshot.SnapshotId -DestinationRegion $Env:DestinationRegion -Description $Description -SourceRegion $Env:SourceRegion -Region $Env:SourceRegion | |
$OriginalTags = Get-EC2Snapshot $snapshot.SnapshotId -Region $Env:SourceRegion | |
New-EC2Tag -Resource $NewSnapshotID -Region $ENV:DestinationRegion -Tag $OriginalTags.Tag | |
Write-Verbose "Created snapshot $NewsnapshotId" | |
break; | |
}catch{ | |
Write-Verbose $_.exception.message | |
Start-Sleep -s 20 | |
} | |
} | |
start-sleep -s 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment