Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use aws config value for region if not specified #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ec2-automate-backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ec2-automate-backup requires one of the following two parameters be provided:

`-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,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 <region>` - the region that contains the EBS volumes for which you wish to have a snapshot created.
`-r <region>` - the region that contains the EBS volumes for which you wish to have a snapshot created (by default uses aws config region).

`-s <selection_method>` - 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.

Expand Down
18 changes: 10 additions & 8 deletions ec2-automate-backup/ec2-automate-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down