alestic / ec2-expire-snapshots

Delete expired EBS snapshots in Amazon EC2. Install on Ubuntu with: sudo add-apt-repository -y ppa:alestic && sudo apt-get update && sudo apt-get install -y ec2-expire-snapshots
Other
107 stars 36 forks source link

h1. NAME

ec2-expire-snapshots - Delete expired EBS snapshots in Amazon EC2

h1. SYNOPSIS

 ec2-expire-snapshots [opts] VOLUMEID...

h1. OPTIONS

h1. ARGUMENTS

h1. INSTALLATION

On Ubuntu, the ec2-expire-snapshots package can be installed directly from the Alestic.com PPA using the following commands:

 sudo add-apt-repository -y ppa:alestic
 sudo apt-get update
 sudo apt-get install -y ec2-expire-snapshots

h1. DESCRIPTION

This program deletes expired EBS snapshots of the specified EBS volumes on Amazon EC2. Different expiration strategies and specifications are supported.

When deciding what options to use, it's easier to think of which EBS snapshots should be preserved instead of which should be expired and deleted.

As a general rule, all EBS snapshots that you have not requested to be preserved will be deleted.

Unless overridden, the software always preserves the most recent EBS snapshot, along with any EBS snapshots where it isn't clear what was intended (e.g., unrecognized expiration tag values).

Please read the descriptions of the preservation options carefully. They might not mean what they look like on first glance. For example, these mean two very different things:

h2. PRESERVATION OPTIONS

The following options control which EBS snapshots are preserved. If an EBS snapshot is not flagged as one to be preserved, then it is considered expired and will be deleted.

There are three basic expiration methodologies supported by this program.

It is acceptable and encouraged to include options from across multiple of these strategies. This program will preserve all EBS snapshots that match any of the conditions.

For example, you may want to keep 24 hourly, 7 daily, 4 weekly, and 12 monthly EBS snapshots as a base strategy.

Then, you could add the last 3 hours of all EBS snapshots just in case you end up creating some EBS snapshots manually during a sensitive file modification procedure and you don't want your EBS snapshots inadvertently deleted in the event you need to undo some recent work.

Then, you could add in some optional expiration tags that can be set to override normal expiration and preserve for a longer time period any special EBS snapshots that you care to flag from time to time.

h2. BACKGROUND

The creation of EBS snapshots on EC2 is a risk reduction and safety improvement measure in a few ways:

An EBS snapshot is a form of backup and disaster recovery preparedness, allowing you to restore data that may have been deleted, corrupted, or in any other way lost on the EBS volume due to hardware, system, environmental, or human error at a time after the EBS snapshot was created.

EBS snapshots are available from multiple availability zones in a given EC2 region even if the availability zone for the source EBS volume is inaccessible.

An EBS snapshot automatically and transparently reduces the rate of failure of the underlying EBS volume, due to the way that Amazon has designed and implemented the EBS system.

The EBS system can automatically recover parts of a failing EBS volume from an EBS snapshot, if the blocks that are failing have not been modified since the EBS snapshot was taken. The more frequently an EBS volume is snapshotted, the lower its potential rate of failure.

With backup strategies using physical media, we normally introduce a backup rotation strategy because we have a limited amount of disk or tape to store the backups. With EC2, the available EBS snapshot space is perceived as limitless at the level a single organizion could use it.

So, why do we want to expire and delete EBS snapshots?

EBS snapshots cost money to store on EC2. Even though multiple EBS snapshots share the same copies of unchanged blocks, and even though the block contents are stored in a compressed format, these charges can add up over time when you have a lot of EBS snapshots.

There is a limit on the number of EBS snapshots a single EC2 account can have at any point in time. This limit can easily be increased by submitting a request to Amazon with an explanation of why you need more, but eventually you're probably going to want to trim back on how many EBS snapshots you retain.

Depending on the tools you are using to manage your AWS resources, it can get unwieldy to manage large numbers of EBS snapshots.

This program tries to help manage your EBS snapshot storage costs by deleting EBS snapshots that you believe are the least helpful to your backup needs, while preserving a select set of EBS snapshots that you believe are likely to be the most important in the near and distant future.

Just remember: When you (or this software) delete an EBS snapshot, it is gone forever and is completely irrecoverable!

h2. SAFETY

This software attempts to be somewhat conservative and to protect you in a few ways:

This program will exit with error unless you specify at least one of the preservation options to give this program an indication of what your expiration strategy is and what EBS snapshots should be preserved.

If you use the @--expiration-tag-name@ option and this program cannot make sense of the value for that tag on one of the EBS snapshots, then that EBS snapshot is not deleted.

If you specify @--expiration-tag-name@ and @--expiration-tag-optional@ without any of the @--keep@ options, and the tag name(s) are not found on any EBS snapshots, then the program exits with an error instead of deleting all your EBS snapshots.

The most recent EBS snapshot for an EBS volume is always preserved unless you explicitly request it to be deleted with:

 --keep-most-recent 0

Deleting the most recent EBS snapshot reduces the reliability of the EBS volume and increases the time, IO, and cost required to create a new EBS snapshot in the future.

If any of the options indicate to this program that an EBS snapshot should be preserved, then it will not be deleted. For example, even if there is an expiration tag on an EBS snapshot that indicates it should expire yesterday, it will not be deleted if it is also the first EBS snapshot of this month and you specified @--keep-first-monthly@ greater than zero.

EBS snapshots in the "pending" or other non-"completed" states are completely ignored by this program. For example, only "completed" snapshots are counted when deciding what is the first snapshot of a calendar period to preserve.

We have no guarantee that a "pending" snapshot will ever complete successfully, but if it does complete before the next time you run this program, then it will be taken into account in the calculations and may become the new "first" EBS snapshot to be preserved for a time period.

h2. DATE/TIME FORMATS

This software supports and interprets a number of different date formats, but if you have a choice, here are some samples of recommended formats for absolute dates and times:

 "2011-12-31"
 "2012-01-15 14:56"
 "2015-07-22 09:23:45"

Dates specified without times are assumed to be at "00:00:00" (midnight starting that date).

This software assumes that dates and times without time zones are in UTC.

Here are some samples of recommended formats for relative time offsets for "expires in" tags on EBS snapshots:

 "+1 year"
 "+10 weeks"
 "+3 days"
 "+12 hours"

Here are some examples of recommended formats for relative time offsets for the @--keep-all-since@ option: [NOT YET IMPLEMENTED]

 "1 year ago"
 "10 weeks ago"
 "1 day ago"
 "12 hours ago"

h1. EXAMPLES

This simple example saves the most recent 10 snapshots:

 ec2-expire-snapshots                   \
     --keep-most-recent 10              \
     vol-11111111

This example saves the last 7 days of snapshots: [NOT YET IMPLEMENTED]

 ec2-expire-snapshots                   \
     --keep-all-since "7 days ago"     \
     vol-22222222

This example keeps one snapshot per day for the last 7 days, and one snapshot per month for the last 12 months. There is also an implicit saving of the most recent snapshot by default:

 ec2-expire-snapshots                   \
     --keep-first-daily 7               \
     --keep-first-monthly 12            \
     vol-33333333

If you always determine how long a snapshot should be saved when you take the EBS snapshot and you store this value in a specific tag named "Expiration", then you may leave out all other "keep" options only pay attention to your tag. Any volume without the tag will be preserved forever in this example.

 ec2-expire-snapshots                   \
     --expiration-tag-name "Expiration" \
     vol-44444444

This example combines a number of preservation strategies, any one of which could trigger the preservation of a given EBS snapshot:

 ec2-expire-snapshots                   \
     --keep-most-recent 1               \
     --keep-first-hourly 24             \
     --keep-first-daily 7               \
     --keep-first-weekly 4              \
     --keep-first-monthly 12            \
     --keep-first-yearly all            \
     --expiration-tag-optional          \
     --expiration-tag-name "Expiration" \
     --expiration-tag-name "Expires"    \
     --expiration-tag-name "Keep-For"   \
     --expiration-tag-name "Keep-Until" \
     vol-55555555                       \
     vol-66666666                       \
     vol-77777777

Delete all EBS snapshots associated with an EBS volume, no matter when they were taken or what their tags say. Make sure you really want to do this. There is no way to recover.

 ec2-expire-snapshots   \
     --force-delete-all \
     vol-88888888

h1. ENVIRONMENT

h1. FILES

h1. SEE ALSO

EBS snapshots reduce risk. This program deletes EBS snapshots. Therefore, the use of this program increases risk.

This program has NOT been tested in all possible environments with all possible combinations of options, settings, EBS volume dates, AWS/EC2 API responses, operating systems, Perl versions, CPAN package versions, AWS accounts, AWS credential settings, etc.

This documentation may not accurately convey to you how this program really works. This documentation may not be up to date with how the program really works.

This program may contains defects that could cause it to delete one or more EBS snapshot that you did not intend to be deleted.

It is possible that this program could leave undeleted some EBS snapshots that you intended to be deleted, causing you to spend more than you want in AWS/EC2 fees.

Please test your command line options, EBS snapshot tags, environment, configuration files and other parameters carefully. Examine closely what EBS snapshots are being deleted and not deleted by this program to make sure it's what you want.

You are responsible for what happens in your EC2 account. This software is intended, but not guaranteed, to help in that effort.

This program tries hard to figure out some values are for the AWS key and AWS secret access key. In fact, it tries too hard. This results in possibly using some credentials it finds that are not the correct ones you wish to use, especially if you are operating in an environment where multiple sets of credentials are in use.

h1. BUGS

Please report bugs at https://github.com/alestic/ec2-expire-snapshots/issues

h1. CREDITS

Thanks to the following for performing tests on early versions, providing feedback, and patches:

  Christian Marquardt
  varunwy
  Anthony Tonns
  Paul Gibson
  yhuyasha

h1. AUTHOR

Eric Hammond ehammond@thinksome.com

h1. LICENSE

Copyright 2009-2012 Eric Hammond ehammond@thinksome.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.