initial commit of ec2-automate-backup

This commit is contained in:
Colin Johnson 2012-09-08 01:54:15 +00:00
parent 0bc5985caf
commit 811aece964
2 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,34 @@
# Introduction:
ec2-automate-backup was created to provide easy backup/snapshot functionality for EC2 EBS volumes. The typical use would be to run ec2-automate-backup with a list of volumes for which a snapshot is desired. Another common use would be to run ec2-automate-backup with cron (example: "0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -v vol-6d6a0527 > /home/ec2-user/ec2-automate-backup_`date +"%Y%m%d"`.log") or to snapshot all EBS volumes that contain the tag "Backup=true" (example: "0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup=true" > /home/ec2-user/ec2-automate-backup_`date +"%Y%m%d"`.log")
# Directions For Use:
#
## Example of Use:
#
ec2-automate-backup -v vol-6d6a0527
----
the above example would provide a single backup of the EBS volumeid vol-6d6a0527. The snapshot would be created with the description "vol-6d6a0527_2012-09-07".
## Required Parameters:
#
ec2-automate-backup requires one of the following two parameters be provided:
-v <volumeid> - the "volumeid" parameter is required to select EBS volumes for snapshot if ec2-automate-backup is run using the "volumeid" selection method - the "volumeid" selection method is the default selection method.
-t <tag> - the "tag" parameter is required if the "method" of selecting EBS volumes for snapshot is by tag (-s tag). The format for tag is key=value (example: Backup=true) and the correct method for running ec2-automate-backup in this manner is ec2-automate-backup -s tag -t Backkup=true.
#
## Optional Parameters:
#
-r <region> - the region that contains the EBS volumes that you wish to have a snapshot created for.
-s <selection_method> - the selection method for which EBS volumes will be selected. Currently supported selection methods are "volumeid" and "tag." The selection method "volumeid" identifies EBS volumes for which a snapshot should be taken by volumeid whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a user provided "tag".
#
# Potential Uses and Methods of Use:
#
* Backup multiple EBS volumes using as follows: ec2-automate-backup -v "vol-6d6a0527 vol-636a0112"
* Backup a selected group of EBS volumes on a schedule tag each volume you wish to backup with the tag "backup=true" and run ec2-automate-backup using cron as follows: ec2-automate-backup -s tag -t "backup=true"
* Backup a selected group of EBS volumes on a schedule tag each volume you wish to backup with the tag "Backup-Daily=true" and/or "Backup-Monthly=true" and run ec2-automate-backup using cron as follows:
- 0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Daily=true"
- 0 0 1 * * ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Monthly=true"
#
# Additional Information:
#
Author: Colin Johnson / colin@cloudavail.com
Date: 2012-09-07
Version 0.1
License Type: GNU GENERAL PUBLIC LICENSE, Version 3

View File

@ -0,0 +1,79 @@
#!/bin/bash -
# Author: Colin Johnson / colin@cloudavail.com
# Date: 2012-09-07
# Version 0.1
# License Type: GNU GENERAL PUBLIC LICENSE, Version 3
#
#confirms that executables required for succesful script execution are available
prerequisite_check()
{
for prerequisite in basename ec2-create-snapshot
do
#use of "hash" chosen as it is a shell builtin and will add programs to hash table, possibly speeding execution. Use of type also considered - open to suggestions.
hash $prerequisite &> /dev/null
if [[ $? == 1 ]] #has exits with exit status of 70, executable was not found
then echo "In order to use `basename $0`, the executable \"$prerequisite\" must be installed." 1>&2 ; exit 70
fi
done
}
#get_EBS_List gets a list of available EBS instances depending upon the selection_method of EBS selection that is provided by user input
get_EBS_list()
{
case $selection_method in
volumeid)
if [[ -z $volumeid ]]
then echo "The selection method \"volumeid\" (which is $app_name's default selection_method of operation or requested by using the -s volumeid parameter) requires a volumeid (-v volumeid) for operation. Correct usage is as follows: \"-v vol-6d6a0527\",\"-s volumeid -v vol-6d6a0527\" or \"-v \"vol-6d6a0527 vol-636a0112\"\" if multiple volumes are to be selected." 1>&2 ; exit 64
fi
ebs_selection_string="$volumeid"
;;
tag)
if [[ -z $tag ]]
then echo "The selected selection_method \"tag\" (-s tag) requires a valid tag (-t key=value) for operation. Correct usage is as follows: \"-s tag -t backup=true\" or \"-s tag -t Name=my_ebs_volume.\"" 1>&2 ; exit 64
fi
ebs_selection_string="--filter tag:$tag"
;;
*) echo "If you specify a selection_method (-s selection_method) for selecting EBS volumes you must select either \"volumeid\" (-s volumeid) or \"tag\" (-s tag)." 1>&2 ; exit 64 ;;
esac
#creates a list of all ebs volumes that match the selection string from above
ebs_backup_list_complete=`ec2-describe-volumes --show-empty-fields --region $region $ebs_selection_string 2>&1`
#takes the output of the previous command
ebs_backup_list_result=`echo $?`
if [[ $ebs_backup_list_result -gt 0 ]]
then echo -e "An error occured when running ec2-describe-volumes. The error thrown is below:\n$ebs_backup_list_complete" 1>&2 ; exit 64
fi
ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2`
#code to right will output list of EBS volumes to be backed up: echo -e "Now outputting ebs_backup_list:\n$ebs_backup_list"
}
#calls prerequisitecheck function to ensure that all executables required for script execution are available
prerequisite_check
app_name=`basename $0`
#sets defaults
selection_method="volumeid"
region="us-east-1"
#sets date variable
date_current=`date -u +%Y-%m-%d`
#handles options processing
while getopts :s:r:v:t: opt
do
case $opt in
s) selection_method="$OPTARG";;
r) region="$OPTARG";;
v) volumeid="$OPTARG";;
t) tag="$OPTARG";;
*) echo "Error with Options Input. Cause of failure is most likely that an unsupported parameter was passed or a parameter was passed without a corresponding option." 1>&2 ; exit 64;;
esac
done
#get_EBS_List gets a list of EBS instances for which a snapshot is desired. The list of EBS instances depends upon the selection_method that is provided by user input
get_EBS_list
#the loop below is called once for each volume in $ebs_backup_list - the currently selected EBS volume is passed in as "ebs_selected"
for ebs_selected in $ebs_backup_list
do
echo "ec2-create-snapshot --region $region -d ${ebs_selected}_$date_current $ebs_selected"
done