Snapshotting Windows EC2 Instances with Powershell – Part 2

In Part 1, we successfully initiated a snapshot with Powershell.  Here we’ll continue building out the script.

 

Cleaning Up Old Snapshots

Anyone who’s ever worked with regularly scheduled snapshots knows you need regularly scheduled cleanup or your AWS invoices will slowly creep up over time.

I’ll be filtering by Description to determine which snapshots do delete. Adding the instance ID in the description helps as a safeguard against unintended deletions.

Appending the cleanup to our snapshotting process, now we have:


#setup for cmdlets to use creds
Initialize-AWSDefaultConfiguration -ProfileName Administrator -Region us-east-1

# Pull InstanceID from EC2 metadata
$MyInstanceID = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")

# Query for volumes that are attached to my Instance Id
$volumes = (Get-EC2Volume).Attachment | where {$_.InstanceId -eq $MyInstanceID } | Select VolumeId

# Snapshot each attached volume
foreach ($volume in $volumes)
{
    New-EC2Snapshot $volume.VolumeId -Description "PS Snapshot for instance $MyInstanceID"
}

#cleanup snapshots older than 3 days with same Description

$Snapshots = Get-EC2Snapshot -OwnerID self | where {$_.Description -eq "PS Snapshot for instance $MyInstanceID"}

$RetentionDays = 3

foreach ($Snapshot in $Snapshots)
    {
    $Retention = ([DateTime]::Now).AddDays(-$RetentionDays)
    if ([DateTime]::Compare($Retention, $Snapshot.StartTime) -gt 0)
        {
        Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
        }
    }
</span>

While I took a different filtering approach, I referenced this in helping get the cleanup done.

A shortcoming of this approach is that if someone were to create a manual backup with the same description as the automated process, it would be subjected to the same retention.  For my purposes, this isn’t a concern.

 

Attaching to a Scheduled Task

Let’s automate this.  I’ve copied the script to C:\SCRIPTS. We’ll launch Task Scheduler and create a basic task that will run daily.  This is fairly straightforward to explain with pictures alone…

snap3.png

snap4.png

snap5.png

snap6.png

snap8.png

snap9.png

Manually running the scheduled task should initiate another snapshot…and it does!

snap10.png

 

Final Thoughts

It’s a more common practice to manage snapshots via tags. However, this method can be just as appropriate depending on the scenario.

While the script only runs if the instance is running…for most of the scenarios I support, this is fine. No change = no need for more snapshots, IMO.

Additional logging (to file or Windows Event Log), email notifications, or cross region replication would dress the script up a bit, but the essential mechanics are there with what has been presented.

All in all, I’d say its a handy approach to keep in the toolkit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s