Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyphlar/60d3ecc774e210d5e76d to your computer and use it in GitHub Desktop.
Save zyphlar/60d3ecc774e210d5e76d to your computer and use it in GitHub Desktop.
Amazon AWS EC2 snapshot cron job
# To set up these environment vars correctly, follow the instructions in:
# http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/set-up-ec2-cli-linux.html
# this is necessary to load your AWS credentials from bashrc.
SHELL=/bin/bash
# double-check this and make sure it's the correct path for ec2-describe-volumes
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin
# this needs to be the path above, minus /bin
EC2_HOME=/opt/aws
# this also needs to be set/correctly
JAVA_HOME=/usr/lib/jvm/jre
MAILTO=root
HOME=/
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# US-EAST-1 AWS SNAPSHOTS
1 8 * * 0 root for a in $(ec2-describe-volumes --region us-east-1 |grep -Ev "ATTACHMENT|TAG" |cut -f2);do ec2-create-snapshot $a -d "Created by CRON on $(hostname -s) for $a -- see https://gist.github.com/zyphlar/60d3ecc774e210d5e76d" --region us-east-1; done;
# US-WEST-1 AWS SNAPSHOTS
2 8 * * 0 root for a in $(ec2-describe-volumes --region us-west-1 |grep -Ev "ATTACHMENT|TAG" |cut -f2);do ec2-create-snapshot $a -d "Created by CRON on $(hostname -s) for $a -- see https://gist.github.com/zyphlar/60d3ecc774e210d5e76d" --region us-west-1; done;
@zyphlar
Copy link
Author

zyphlar commented Nov 7, 2014

Credit to http://www.techkismet.com/uncategorized/a-quick-way-to-automate-aws-ec2-snapshots.html/comment-page-1 for the original. Note you'll want to test this as root and make sure you've got your credentials set up properly: http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/set-up-ec2-cli-linux.html

@zyphlar
Copy link
Author

zyphlar commented Nov 8, 2014

Note, I kept trying this and kept getting Java class errors when run via CRON. I ended up just using ec2-automate-backup instead: https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment