From f65de2912bebc9ee4f28036ea0122aabb3490f88 Mon Sep 17 00:00:00 2001 From: Nochum Sossonko Date: Wed, 27 Apr 2016 13:07:25 -0400 Subject: [PATCH] Use aws config value for region if not specified --- ec2-automate-backup/README.md | 2 +- ec2-automate-backup/ec2-automate-backup.sh | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ec2-automate-backup/README.md b/ec2-automate-backup/README.md index 3ea8a8f..0c50c30 100644 --- a/ec2-automate-backup/README.md +++ b/ec2-automate-backup/README.md @@ -20,7 +20,7 @@ ec2-automate-backup requires one of the following two parameters be provided: `-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,Values=$desired_values (example: Backup,Values=true) and the correct method for running ec2-automate-backup in this manner is ec2-automate-backup -s tag -t Backup,Values=true". ## Optional Parameters: -`-r ` - the region that contains the EBS volumes for which you wish to have a snapshot created. +`-r ` - the region that contains the EBS volumes for which you wish to have a snapshot created (by default uses aws config region). `-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 whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a filter that utilizes a Key and Value pair. diff --git a/ec2-automate-backup/ec2-automate-backup.sh b/ec2-automate-backup/ec2-automate-backup.sh index 2886db7..7800b5b 100755 --- a/ec2-automate-backup/ec2-automate-backup.sh +++ b/ec2-automate-backup/ec2-automate-backup.sh @@ -43,7 +43,7 @@ get_EBS_List() { *) 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=$(aws ec2 describe-volumes --region $region $ebs_selection_string --output text --query 'Volumes[*].VolumeId') + ebs_backup_list=$(aws ec2 describe-volumes $region $ebs_selection_string --output text --query 'Volumes[*].VolumeId') #takes the output of the previous command ebs_backup_list_result=$(echo $?) if [[ $ebs_backup_list_result -gt 0 ]]; then @@ -74,7 +74,7 @@ create_EBS_Snapshot_Tags() { if [[ -n $snapshot_tags ]]; then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags" tags_argument="--tags $snapshot_tags" - aws_ec2_create_tag_result=$(aws ec2 create-tags --resources $ec2_snapshot_resource_id --region $region $tags_argument --output text 2>&1) + aws_ec2_create_tag_result=$(aws ec2 create-tags --resources $ec2_snapshot_resource_id $region $tags_argument --output text 2>&1) fi } @@ -112,11 +112,11 @@ esac 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') + snapshot_purge_allowed=$(aws ec2 describe-snapshots $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) + purge_after_fe=$(aws ec2 describe-snapshots $region --snapshot-ids $snapshot_id_evaluated --output text | grep ^TAGS.*PurgeAfterFE | cut -f 3) #if purge_after_date is not set then we have a problem. Need to alert user. if [[ -z $purge_after_fe ]]; then #Alerts user to the fact that a Snapshot was found with PurgeAllow=true but with no PurgeAfterFE date. @@ -127,7 +127,7 @@ purge_EBS_Snapshots() { # and the snapshot can be safely purged if [[ $purge_after_fe < $current_date ]]; then echo "Snapshot \"$snapshot_id_evaluated\" with the PurgeAfterFE date of \"$purge_after_fe\" will be deleted." - aws_ec2_delete_snapshot_result=$(aws ec2 delete-snapshot --region $region --snapshot-id $snapshot_id_evaluated --output text 2>&1) + aws_ec2_delete_snapshot_result=$(aws ec2 delete-snapshot $region --snapshot-id $snapshot_id_evaluated --output text 2>&1) fi fi done @@ -180,10 +180,12 @@ fi if [[ -z $region ]]; then #if the environment variable $EC2_REGION is not set set to us-east-1 if [[ -z $EC2_REGION ]]; then - region="us-east-1" + region="" else - region=$EC2_REGION + region="--region=$EC2_REGION" fi +else + region="--region=$region" fi #sets date variable @@ -205,7 +207,7 @@ 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_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) + ec2_snapshot_resource_id=$(aws ec2 create-snapshot $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