Get-Ryan

[PowerShell] AWS - Manage Gateway Snapshots

Mar 4th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $error.Clear()
  2.  
  3. $gateway = Get-SGGateway
  4. $volume = Get-SGVolume -GatewayARN $gateway.GatewayARN
  5. $volumeid = $volume.VolumeARN.Substring($volume.VolumeARN.LastIndexOf("/")+1).ToLower()
  6.  
  7. $filter = New-Object Amazon.EC2.Model.Filter
  8. $filter.Name = "volume-id"
  9. $filter.Value.Add($volumeid)
  10.  
  11. $today = [DateTime]::Now.Date
  12. $oldTimeSpan = -7
  13. $expiredTimeSpan = -14
  14.  
  15. $snapshots = Get-EC2Snapshot -Filter $filter
  16.  
  17. ## Get snapshots between 1 and 2 weeks old
  18. $oldSnapshots = $snapshots | Where-Object {$_.StartTime.Date -lt $today.AddDays($oldTimeSpan) -and $_.StartTime.Date -ge $today.AddDays($expiredTimeSpan)}
  19.  
  20. ## Get snapshots older than 2 weeks
  21. $expiredSnapshots = $snapshots | Where-Object {$_.StartTime.Date -lt $today.AddDays($expiredTimeSpan)}
  22.  
  23. $deleted = New-Object System.Collections.ArrayList
  24. $responses = New-Object System.Collections.ArrayList
  25.  
  26.  
  27. ## Remove gateway snapshots older than 2 weeks
  28. ForEach($snapshot in $expiredSnapshots){
  29.     Try{
  30.         Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
  31.     }
  32.     Catch{
  33.         Continue
  34.     }
  35.     $deleted.Add(($snapshot | Select-Object SnapshotId, StartTime)) | Out-Null
  36. }
  37.  
  38. ## Remove all snapshots except the 6am one for snapshots between 1 and 2 weeks old
  39. ForEach($snapshot in $oldSnapshots){
  40.     If($snapshot.StartTime.TimeOfDay.Hours -le 5 -or $snapshot.StartTime.TimeOfDay.Hours -ge 7){
  41.         Try{
  42.             Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
  43.         }
  44.         Catch{
  45.             Continue
  46.         }
  47.         $deleted.Add(($snapshot | Select-Object SnapshotId, StartTime)) | Out-Null
  48.     }
  49. }
  50.  
  51. $deleted | Export-Csv -Path "C:\Logs\AWS-Snapshots\SnapshotLogDeletions-$($today.Date.ToString("yyyyMMdd")).csv" -NoTypeInformation
  52.  
  53. If($error){
  54.     $error | Export-Csv -Path "C:\Logs\AWS-Snapshots\SnapshotLogErrors-$($today.Date.ToString("yyyyMMdd")).csv" -NoTypeInformation
  55. }
Advertisement
Add Comment
Please, Sign In to add comment