Skip to content

A quick way to automate AWS EC2 Snapshots

Earlier I posted a way to cleanup snapshots. I’ve had a few emails asking how I automated the creation. Here is a quick command line (Linux) to automate the creation of the snapshots. Make sure you have your environment setup and the latest version of the EC2 Tools API installed and in the PATH. You can place this command as a cron entry, or simply run it from the command line.

for a in $(ec2-describe-volumes |grep -Ev "ATTACHMENT" |cut -f2);do ec2-create-snapshot $a; done;

All this is doing is grabbing the volume ID’s from the ec2-describe-volumes output – removing the extraneous information, and executing the ec2-create-snapshot command on each volume. The good thing about this is that it doesn’t have to be modified as you add new volumes. Please see my earlier posting on snapshot cleanup to have a full solution from creation to cleanup.

Quick and easy.

{ 5 } Comments

  1. Doug | July 30, 2010 at 4:05 pm | Permalink

    a better tool to use especially if you’re running mysql on any of your instances is the ec2-consistent-backup program. It will freeze your drive and mysql for a couple seconds so you get a clean snapshot that you can safely reload mysql from. Otherwise the binary files can often be corrupted because of database reads/writes happening during the snapshot process.

  2. Alvin Kreitman | July 30, 2010 at 7:18 pm | Permalink

    Very true; thanks for the input Doug.

  3. Phil | April 27, 2011 at 10:18 pm | Permalink

    For some reason this isn’t working when I try to run it in cron – it says I need to specify the key and certificate files. I have these environment variables set in my .bashrc , and the command works OK straight from the command line. What do I have to do to get this to work in cron? (sysadmin newb here)

  4. KEVIN KRUZICH | June 6, 2011 at 9:42 pm | Permalink

    @Phil: The key and cert files here are the x.509 Certificates available here:

    AWS Console -> Account -> Security Credentials -> x.509 Certificates

  5. Will | November 6, 2014 at 8:53 pm | Permalink

    Thanks for this one-liner! Here’s an updated version I made, that excludes tags and adds a description and region:

Post a Comment

Your email is never published nor shared. Required fields are marked *