EC2 Backup Manager : Python Scripts

24 / Apr / 2015 by Hitesh Bhatia 7 comments

EC2 Backup Manager : Python Scripts

One of the most important requirements in any projects is regular backups. On one of our projects we were using Netflix’s backup monkey and graffiti monkey combined to take snapshots of all the volumes in the account. But this deviated slightly from our requirement, we only required the backups of Production instances which were not Autoscaled (Autoscaled instances are anyway independent of the instance/disk, so no backups needed).

We (me (@hiteshBhatia) and my colleague Aakash Garg @aakash21) wrote couple Python scripts (publicly available on Github) which are more suited to our needs and might help others too. These scripts are part of a  public repo (on Github) which compromises of scripts which are helpful for managing an AWS account.

This custom backup-manager of ours includes two scripts, one for the creation of images and other for deletion of old images. Both of the script are based on tags which are applied to the instances.

Following scripts only manage backups of instances that have been marked with tag {“AutomaticBackup”:”True”}.

1. – This script creates the backup of those instances which are marked with the tag “AutomaticBackup” with value “True” {“AutomaticBackup”:”True”}. The script also tags the resultant backups (ami) with the instance_id tag, which also helps in searching of ami associated with the instance.

The script also uses one more tag named “Frequency”.  It defines the frequency of backups you require in a day, default value is 1. If you are running this script twice a day use “Frequency” tag with value 2. The script would not take backups more than the Frequency in a day. I.e If Frequency is 2 and you run this script 4 times a day. It will only create backup twice because the value of Frequency tag is 2.

2. – It reads the instance tag RetentionCount (in days) and deletes the older images. If no tag is specified by default it retains the backups of last 30 days and deletes the older images.

So now if you create a new instance and require its regular backups managed, just add the tag “AutomaticBackup” with value true and its backups will managed. So to start managing backups, tag your instances, schedule the script with cron scheduler and its done. And do not forget that Tags are case sensitive.

PS : These scripts are part of “aws-boto-scripts“, which is a repo of Python scripts written with boto for managing an AWS account.


comments (7)

  1. amritha

    how to pass multiple region instead of just one in region variable declared at beginning , so that it can run in all regions.

  2. Rohit

    Hi all
    I written a same python script just in different manner My script works on tags.
    Tags are “Daily” and “weekly” as per condition we can add tag {“AMI”:”daily”} or {“AMI”: “weekly”}
    As per this tags it will create AMI and as per the input given period it deletes the old AMI’s.

  3. Hitesh Bhatia

    hi Devashish,
    I believe that you are missing one of following information in the script. Edit the script and enter the values of following variables.
    -> region
    -> account-id

  4. Aakash Garg

    Make changes in the region and account id to specify in which region and by which account you want to run.

  5. Devashish

    Hi Hitesh

    Thanks for the script .
    While I am trying to use it I am getting an error
    “AttributeError: ‘NoneType’ object has no attribute ‘get_all_reservations'”

    Is there something that I am missing ?
    Appreciate your support !!



Leave a Reply

Your email address will not be published. Required fields are marked *