From e712cfe4f5057001871bbe9027505c6700557b23 Mon Sep 17 00:00:00 2001 From: cwturley Date: Tue, 4 Nov 2014 15:29:53 -0500 Subject: [PATCH] Added InstanceName tag to volumes on backup Added the option "i" to tag volumes with the Instance Name from the attached instance ID. --- .../ec2-automate-backup-awscli.sh | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ec2-automate-backup/ec2-automate-backup-awscli.sh b/ec2-automate-backup/ec2-automate-backup-awscli.sh index 52fe606..680497a 100755 --- a/ec2-automate-backup/ec2-automate-backup-awscli.sh +++ b/ec2-automate-backup/ec2-automate-backup-awscli.sh @@ -43,7 +43,7 @@ get_EBS_List() { esac #creates a list of all ebs volumes that match the selection string from above ebs_backup_list=$(aws ec2 describe-volumes --region $region $ebs_selection_string --output text --query 'Volumes[*].VolumeId') - #takes the output of the previous command + #takes the output of the previous command ebs_backup_list_result=$(echo $?) if [[ $ebs_backup_list_result -gt 0 ]]; then echo -e "An error occurred when running ec2-describe-volumes. The error returned is below:\n$ebs_backup_list_complete" 1>&2 ; exit 70 @@ -53,6 +53,10 @@ get_EBS_List() { create_EBS_Snapshot_Tags() { #snapshot tags holds all tags that need to be applied to a given snapshot - by aggregating tags we ensure that ec2-create-tags is called only onece snapshot_tags="" + #if $instance_name_tag_create is true then append ec2_volume_instance_name to the variable $snapshot_tags + if $instance_name_tag_create; then + snapshot_tags="$snapshot_tags Key=InstanceName,Value=$ec2_volume_instance_name" + fi #if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags if $name_tag_create; then snapshot_tags="$snapshot_tags Key=Name,Value=ec2ab_${ebs_selected}_$current_date" @@ -112,7 +116,7 @@ purge_EBS_Snapshots() { # snapshot_purge_allowed is a string containing the SnapshotIDs of snapshots # that contain a tag with the key value/pair PurgeAllow=true snapshot_purge_allowed=$(aws ec2 describe-snapshots --region $region --filters Name=tag:PurgeAllow,Values=true --output text --query 'Snapshots[*].SnapshotId') - + for snapshot_id_evaluated in $snapshot_purge_allowed; do #gets the "PurgeAfterFE" date which is in UTC with UNIX Time format (or xxxxxxxxxx / %s) purge_after_fe=$(aws ec2 describe-snapshots --region $region --snapshot-ids $snapshot_id_evaluated --output text | grep ^TAGS.*PurgeAfterFE | cut -f 3) @@ -145,9 +149,11 @@ hostname_tag_create=false user_tags=false #sets the Purge Snapshot feature to false - if purge_snapshots=true then snapshots will be purged purge_snapshots=false +#sets the "InstanceName" tag set for a snapshot to be false +instance_name_tag_create=false #handles options processing -while getopts :s:c:r:v:t:k:pnhu opt; do +while getopts :s:c:r:v:t:k:pnhui opt; do case $opt in s) selection_method="$OPTARG" ;; c) cron_primer="$OPTARG" ;; @@ -159,6 +165,7 @@ while getopts :s:c:r:v:t:k:pnhu opt; do h) hostname_tag_create=true ;; p) purge_snapshots=true ;; u) user_tags=true ;; + i) instance_name_tag_create=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 @@ -203,11 +210,19 @@ 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 + if $instance_name_tag_create; then + #get the id of the instance the volume is attached to + ec2_volume_instance_id=$(aws ec2 describe-volumes --output text --volume-id $ebs_selected --query 'Volumes[*].[Attachments[*].InstanceId]') + #get the name of the instance for the instance id + ec2_volume_instance_name=$(aws ec2 describe-instances --output text --instance-id $ec2_volume_instance_id --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value') + #replace spaces with underscores (issue with tagging) + ec2_volume_instance_name=$(echo "$ec2_volume_instance_name"|sed -e 's/ /_/g') + fi ec2_snapshot_description="ec2ab_${ebs_selected}_$current_date" ec2_snapshot_resource_id=$(aws ec2 create-snapshot --region $region --description $ec2_snapshot_description --volume-id $ebs_selected --output text --query SnapshotId 2>&1) if [[ $? != 0 ]]; then echo -e "An error occurred when running ec2-create-snapshot. The error returned is below:\n$ec2_create_snapshot_result" 1>&2 ; exit 70 - fi + fi create_EBS_Snapshot_Tags done