From e69fda405d613ed5de0e0b8a22561ad5f7b1a733 Mon Sep 17 00:00:00 2001 From: Colin Johnson Date: Fri, 14 Sep 2012 23:59:56 +0000 Subject: [PATCH] add enhancement - ec2-automate-backup.sh now writes tag Name=value if requested and handles ec2-create-snapshot errors. --- ec2-automate-backup/README.txt | 30 ++++++++++++++-------- ec2-automate-backup/ec2-automate-backup.sh | 19 +++++++++++--- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/ec2-automate-backup/README.txt b/ec2-automate-backup/README.txt index f317a2c..45f9cc4 100644 --- a/ec2-automate-backup/README.txt +++ b/ec2-automate-backup/README.txt @@ -1,29 +1,37 @@ -= Introduction = +# Introduction: ec2-automate-backup was created to provide easy backup/snapshot functionality for EC2 EBS volumes. Common uses would include the following: * run ec2-automate-backup with a list of volumes for which a snapshot is desired * run ec2-automate-backup using cron to produce a daily backup (example: "0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -v "vol-6d6a0527 vol-636a0112" > /home/ec2-user/ec2-automate-backup_`date +"%Y%m%d"`.log") * run ec2-automate-backup 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 +# 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 == +## Required Parameters: +# ec2-automate-backup requires one of the following two parameters be provided: -v - 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 - 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 Backup=true. -== Optional Parameters == +# +## Optional Parameters: +# -r - the region that contains the EBS volumes for which you wish to have a snapshot created. -s - the selection method by 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 volume id whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a key=value format tag. - -= Potential Uses and Methods of Use = +-n - tag snapshots "Name" tag as well as description +# +# Potential Uses and Methods of Use: +# * To backup multiple EBS volumes use ec2-automate-backup as follows: ec2-automate-backup -v "vol-6d6a0527 vol-636a0112" * To backup a selected group of EBS volumes on a daily schedule tag each volume you wish to backup with the tag "Backup=true" and run ec2-automate-backup using cron as follows: 0 0 * * 0 ec2-automate-backup -s tag -t "Backup=true" * To backup a selected group of EBS volumes on a daily and/or monthly 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 = +# +# Additional Information: +# Author: Colin Johnson / colin@cloudavail.com Date: 2012-09-07 Version 0.1 diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 5607485..8c9b1e6 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -1,6 +1,6 @@ #!/bin/bash - # Author: Colin Johnson / colin@cloudavail.com -# Date: 2012-09-07 +# Date: 2012-09-14 # Version 0.1 # License Type: GNU GENERAL PUBLIC LICENSE, Version 3 # @@ -40,7 +40,7 @@ get_EBS_list() #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 + then echo -e "An error occured when running ec2-describe-volumes. The error returned is below:\n$ebs_backup_list_complete" 1>&2 ; exit 70 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" @@ -56,15 +56,18 @@ selection_method="volumeid" region="us-east-1" #sets date variable date_current=`date -u +%Y-%m-%d` +#sets the "Name" tag set for a snapshot to false - using "Name" requires that ec2-create-tags be called in addition to ec2-create-snapshot +name_tag_set=false #handles options processing -while getopts :s:r:v:t: opt +while getopts :s:r:v:t:n opt do case $opt in s) selection_method="$OPTARG";; r) region="$OPTARG";; v) volumeid="$OPTARG";; t) tag="$OPTARG";; + n) name_tag_set=true;; *) 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 @@ -75,5 +78,13 @@ 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 - ec2-create-snapshot --region $region -d ${ebs_selected}_$date_current $ebs_selected + ec2_snapshot_description="ec2ab_${ebs_selected}_$date_current" + ec2_create_snapshot_result=`ec2-create-snapshot --region $region -d $ec2_snapshot_description $ebs_selected 2>&1` + if [[ $? != 0 ]] + then echo -e "An error occured when running ec2-create-snapshot. The error returned is below:\n$ec2_create_snapshot_result" 1>&2 ; exit 70 + elif $name_tag_set + then + ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2` + ec2-create-tags $ec2_snapshot_resource_id --region $region --tag Name=$ec2_snapshot_description + fi done \ No newline at end of file