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

Support aws profiles #127

Open
wants to merge 3 commits 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: 2 additions & 0 deletions ec2-automate-backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ ec2-automate-backup requires one of the following two parameters be provided:

`-u` - the -u flag will tag snapshots with additional data so that snapshots can be more easily located. Currently the two user tags created are Volume="ebs_volume" and Created="date." These can be easily modified in code.

`-i <profile>` - Use a specific profile from your credential file

# Potential Uses and Methods of Use:
* To backup multiple EBS volumes use ec2-automate-backup as follows: `ec2-automate-backup.sh -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 * * * ec2-automate-backup.sh -s tag -t "Backup,Values=true"`
Expand Down
26 changes: 16 additions & 10 deletions ec2-automate-backup/ec2-automate-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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')
#takes the output of the previous command
ebs_backup_list=$(aws ec2 describe-volumes --region $region $ebs_selection_string --output text --query 'Volumes[*].VolumeId' --profile $profile)
#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
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 $region $tags_argument --output text 2>&1 --profile $profile)
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 $region --filters Name=tag:PurgeAllow,Values=true --output text --query 'Snapshots[*].SnapshotId' --profile $profile)

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 $region --snapshot-ids $snapshot_id_evaluated --output text --profile $profile| 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 $region --snapshot-id $snapshot_id_evaluated --output text --profile $profile 2>&1)
fi
fi
done
Expand All @@ -151,14 +151,15 @@ user_tags=false
purge_snapshots=false
#handles options processing

while getopts :s:c:r:v:t:k:pnhu opt; do
while getopts :s:c:r:v:t:k:i:pnhu opt; do
case $opt in
s) selection_method="$OPTARG" ;;
c) cron_primer="$OPTARG" ;;
r) region="$OPTARG" ;;
v) volumeid="$OPTARG" ;;
t) tag="$OPTARG" ;;
k) purge_after_input="$OPTARG" ;;
i) profile="$OPTARG" ;;
n) name_tag_create=true ;;
h) hostname_tag_create=true ;;
p) purge_snapshots=true ;;
Expand Down Expand Up @@ -186,6 +187,11 @@ if [[ -z $region ]]; then
fi
fi

#if profile is not set then:
if [[ -z $profile ]]; then
profile="default"
fi

#sets date variable
current_date=$(date -u +%s)

Expand All @@ -205,10 +211,10 @@ 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 $region --description $ec2_snapshot_description --volume-id $ebs_selected --output text --query SnapshotId --profile $profile 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

Expand Down