I recently wrote a script to perform a snapshot of multiple EBS volumes where the metadata tag value matched a certain criteria.
In this case each EBS volume to which a snapshot is required includes the metadata tag value ‘EBS Snapshot: Yes’. Also, there is a requirement to only retain snapshots for the a certain amount of days.
This could be achieved by using Powershell for Amazon Web Services (http://aws.amazon.com/powershell/).
The script will return all EBS volumes using the ‘Get-EC2Volume’ cmdlet and providing the filter argument to limit the returned EBS Volumes to those where the metadata tag ‘EBS Snapshot’ equals ‘Yes’. For each EBS volume returned a snapshot is created using the ‘New-EC2Snapshot’ cmdlet where the description ‘EBS Snapshot’ is included.
Once we have created a snapshot of all the required EBS volumes, we now need to remove those snapshots that are older than five days. This is performed by returning all the snapshots where the description is ‘EBS Snapshot’ and then using a date compare to remove snapshots using the ‘Remove-EC2Snapshot’ cmdlet with the Force parameter that are older than the retention period specified.
The full script can be downloaded from: https://app.box.com/s/xh2bt1laz33c5z0pzy84.
Don’t forget that you need to either have default AWS credentials specified for the shell to use or specify the credentials at run time. Thanks for this – pretty helpful!
LikeLike
Hi Dean,
Thank you for this script.
I have some questions that I’m guessing you can answer:
I created a tag for my snapshots which I called “Name” where I specify the instance which the volume is attached to.
Is it possible to modify this script so it puts the Instance-tag “Name” in the Snapshot-tag called “Name”?
LikeLike
It is not possible using the above as the collection is built from querying all volumes and their is no relationship between the volume and the instance.
You would need to build a collection based on your EC2 instance then loop through each one to return the volumes to invoke the New-EC2Snapshot cmdlet, where you pass the name tag of the instance instead of the description in my example.
LikeLike