I was looking for a simple script that creates a new ebs snapshot and deletes all the previous snapshots except a few newest snapshots. I found a script written in php called manage snapshots at http://www.thecloudsaga.com/aws-ec2-manage-snapshots/. But the script only deletes snapshots. It does not create a new snapshot. That is why I decided to write a script on my own.
In this post I am going to describe how to use my script. You can download my script from manage_snapshots.txt
It’s a python script. Change the extension to be .py. Ubuntu comes with python installed. You will need to install aws python library boto. To install boto all ob ubuntu you can simply execute
sudo apt-get install python-boto
You will need to change the following lines with your aws access key and aws secret key. Replace MY_ACCESS_KEY_HERE with your access key and MY_SECRET_KEY_HERE with your secret key.
# Substitute your access key and secret key here
aws_access_key = 'MY_ACCESS_KEY_HERE'
aws_secret_key = 'MY_SECRET_KEY_HERE'
Here is how you can use the script:
The script takes three arguments:
1) volume-id (required) – This is Amazon’s volume id
2) number of snapshots to preserve (required) – An integer. If you specify 2 it will keep 2 newest snapshots (including the one it just created). If you specify 0, the script will delete all the snapshots (including the one it just created).
3) description (optional) – Description you want to use for your snapshot.
Example:
python manage_snaphots.py vol-dkls343e 2 'log server daily snapshot'
This execution will create a snapshot of vol-dkls343e and delete anything but that snapshot and the one before it. It will make sure that only two latest snapshots are kept. You can simply add a cron per volume.