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.