Amazon Web Services Code: Amazon Web Services AWS backup EBS EC2 ec2-create-snapshot ec2-delete-snapshots ec2-describe-snapshot ec2-manage-snapshots php snapshot
by The Cloud Walker
18 comments
ec2-manage-snapshots
Based on code from Oren Solomianik’s ec2-delete-old-snapshots ec2-manage-snapshots does things a little differently.
Oren’s script would delete any snapshots older than n days for (a) given volume(s). I improved the code a little bit so as to handle a –region parameter ( –region eu-west-1 is an example) an –noop so as to tell us what it would do but not actually do it. That’s the ec2-delete-old-snapshots you’ll find in the included archive.
Because I wanted to manage snapshots differently I heavily modified the original script to create ec2-manage-snapshots so that for a given volumes, it would keep snapshots made:
- in the last 7 days
- the past 4 sundays
- every first day of the month
and erase the rest.
Its behavior can be easily modified but I wanted to make sure I kept monthly snapshots, last 4 weekly & last 7 days worth. It assumes you’re doing one daily snapshot of each volume.
Download here: ec2-manage-snapshots
Failsafe
Like its ancestor, the program checks to see that each vol-id entered on the command line has at least one newer snapshot than the deletion date, to prevent deletion of all snapshots of a certain volume. The deletion process will commence for a volume only if such a snapshot was found. More importantly, you can use the --noop command line option or the constant in the code (NOOP) and the script will tell you what it would have deleted but won’t actually delete anything.
Disclaimer: This program is in development. Although it has been tested and worked on production environments, it can’t be guaranteed to perform without unexpected results. Use at your own risk.
I’ll put it under Apache License as soon as I have the time. Consider it so for now. The original had no license info nor did its google code site.
Hi Erik,
Thanks for these improvements! I really neglected the development of this script…
If you would like to commit your changes, please send me an email with your Google code user and I’ll add you to the team there.
Oren
On my system, I was getting a fatal error:
Fatal error: Class ‘XsltProcessor’ not found
This was on Ubuntu 9.10 desktop. It was because I didn’t have the package php5-xsl installed. To get around this:
sudo apt-get install php5-xsl
Then I re-ran the script and all was working.
Great, thanks for the insight on php5-xsl package requirement.
It’s also worth noting that on most systems running PHP prior to 5.3, it is not possible to use long options, so the –noop and –region will not work. It’s actually quite dangerous for –noop to fail unexpectedly, for obvious reasons! I saw this error:
Warning: getopt(): No support for long options in this build
I would recommend updating to PHP 5.3, but we’re using Drupal 6 which is not compatible with it yet.
As a workaround, I suggest adding short versions of each option, so at least people have the choice.
Good point, I’ll soon release a version that supports short options for region and noop
I found Oren’s script a little too cumbersome to install and run (you have to download a bunch of PHP libraries and it works with time period not number of backups to keep!) so I’ve created a simpler alternative here if anyone is interested:
http://www.sambastream.com/blogs/dgildeh/12-03-10/implementing-revolving-backups-aws-ec2
Hope this helps!
Well, Oren’s script main goal is to delete the last n days. Yours deletes n backups. The script I added (based on Oren) sees things more like backups and always keeps the monthlies, a month worth of weeklies and the last seven days of dailies. I guess it depends on what you need.
As for the library requirements, I have to say I didn’t look too much into it since my home system and my servers seemed to have all that it needed. Your mileage (did) may varies.
Hi Cloud Walker,
I noticed one issue with the script… It will fail if the snapshot is part of an EBS AMI snapshot. The issue with 5.3 compatibility could probably be overcome by using Zend Framework’s Zend_Console_Getopt class. I write a lot of PHP and could help really spruce this up!
BTW, thanks for the post.
Nick, feel free to do so. Make changes to http://github.com/edasque/ec2-manage-snapshots ? Does using the Zend getOpt class mean bringing in a slew of dependencies though?
Hey Cloud Walker! Sorry for the long delay. I am going to take a look at this today to see if there’s anything I can help contribute.
Howdy Again,
I spent the last 8 hours really putting some polish on this script. Here’s a link to the project files. (http://www.upsellnetwork.com/ec2-manage-snapshots.export.tar.gz)
Some of the improvements are:
=========================================
* OO Redesign of the logic
* Accounts for more than one backup a day
* Uses Amazon’s newest API
* Handles snapshots that are in use by AMI images
* access key and secret access key can now be included as command line arguments
* configuration has been moved outside of the script, so there should be no editing there.
* added validation for amazon regions with graceful exit when a bad region is supplied
* added quiet mode
Right now the entire Zend Framework is included under the library directory, but I bet I can weed that down.
When unzipping the script, you will have to create a file called config.ini under the /etc directory. There is a config.ini-dist to be used as an example. To see all of the options, just run ec2-manage-snapshots.php –help
Let me know what you guys think!!!!
Best,
Nick
Posted the code on GoogleCode http://code.google.com/p/ec2-prune-snapshots/
Great work but I would have rather you put it on github since we already have a project there. Also, I’d rather we keep the same name otherwise there will be confusion?
I haven’t really played with GitHub before. I’ll take a look in a few hours and try to integrate what I’ve done with your project. If that all works, I’ll just close the project at Google. Were you able to run the script?
Hi Erik,
I just forked your project and uploaded the files. You can see the project here now. http://github.com/NicholasMiller/ec2-manage-snapshots
Awesome, I’ll take a look very soon
This is great. What license is is released under?