-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnapMaint.ps1
102 lines (80 loc) · 6.65 KB
/
SnapMaint.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# The purpose of this script is to find all backups associated
# with the calling instance-ID with the intent of expiring any images
# that are older than the threshold date. The script will pull the
# instance-ID from the instance meta-data URL, then search for
# Snapshots that were previously generated by the backup scripts
# found elsewhere in this tool-set.
#
# License:
# - This script released under the Apache 2.0 OSS License
#
######################################################################
# Commandline argument parsing...
Param (
[int]$keepdays = 7,
[string]$snapgrp
)
# Set generic variables
$DaysBack = $keepdays
$SnapGroup = $snapgrp
$DateHorizon = ([DateTime]::Now).AddDays(-$DaysBack)
$DateStmp = $(get-date -format "yyyyMMddHHmm")
$LogDir = "C:/TEMP/EBSbackup"
$LogFile = "${LogDir}/backup-$DateStmp.log"
$instMetaRoot = "http://169.254.169.254/latest/"
# Make sure AWS cmdlets are available
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"
# Capture instance identy "document" data
$docStruct = Invoke-RestMethod -Uri ${instMetaRoot}/dynamic/instance-identity/document/
# Extract info from $docStruct
$instRegion = $docStruct.region
$instId = $docStruct.instanceId
# Set AWS region fo subsequent AWS cmdlets
Set-DefaultAWSRegion $instRegion
# Grab a filtered list candidate snapshots and dump to an array
# * Filter for "Created By" equals "Automated Backup"
# * Filter for "Description" contains "<INSTANCE_ID>-bkup"
function SnapListToArray {
[System.Collections.Generic.List[System.String]]$global:DeleteList = ''
# Fer realz, Microsoft? I can't declare an modifiable-length list as being empty (i.e., one without nulls trashing it up)???
$DeleteList.Remove("") | Out-Null
# Query for this instance's snapshots
$SnapStructList = Get-EC2Snapshot -Filter @(
@{ Name="tag:Created By" ; Values="Automated Backup" }, `
@{ Name="description" ; Values="*${instId}*" }, `
@{ Name="tag:Snapshot Group" ; Values="*$SnapGroup*" }
)
# Examine each snapshot's data structures
foreach ($SnapStruct in $SnapStructList) {
$SnapId = $SnapStruct.SnapshotId
$SnapStart = $SnapStruct.StartTime
Write-Host -NoNewline "Found snapshot: $SnapId (started @ ${SnapStart})"
if ([DateTime]::Compare($DateHorizon, $SnapStart) -gt 0) {
Write-Host " - $SnapId is older than defined horizon (adding to delete-list)"
$DeleteList.Add("$SnapId") | out-null
} else {
Write-Host
}
}
}
function NukeSnaps {
foreach ($NukeSnap in $DeleteList) {
Write-Host -NoNewline "Deleting snapshot: $NukeSnap..."
# Assign to var for diagnostics ...even though no exit info is printed
# Not a fan of "Force" flags, but seems the only option, here
$RemovalStatus = Remove-EC2Snapshot -SnapshotId $NukeSnap -Force -PassThru
if ($RemovalStatus = '') {
Write-Host "FAILED!"
} else {
Write-Host "Success"
}
}
}
SnapListToArray
if ($DeleteList.Count -eq 0) {
Write-Host "Nothing to delete: no snapshots older than keep-horizon"
} else {
Write-Host "Snapshot(s) to delete: $DeleteList"
NukeSnaps
}