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

Add ability to configure a label on ec2-automate-backup #55

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
4 changes: 4 additions & 0 deletions ec2-automate-backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ ec2-automate-backup requires one of the following two parameters be provided:

`-c <cron_primer_file>` - running with the -c option and a providing a file will cause ec2-automate-backup to source a file for environmental configuration - ideal for running ec2-automate-backup under cron. An example cron primer file is located in the "Resources" directory and is called cron-primer.sh.

`-l` - label a snapshot "Name" tag with a user-defined value

`-n` - tag snapshots "Name" tag as well as description

`-h` - tag snapshots "InitiatingHost" tag to specify which host ran the script
Expand All @@ -45,6 +47,8 @@ ec2-automate-backup requires one of the following two parameters be provided:
- `0 0 1 * * ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Monthly=true"`
* To perform daily backup using cron and to load environment configuration with a "cron-primer" file:
- `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -c /home/ec2-user/cron-primer.sh -s tag -t "Backup=True"`
* To perform a daily backup with cron and loading environment configuration with a user-defined label:
- `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -c /home/ec2-user/cron-primer.sh -s tag -t "Backup=True" -l "Production Site Backup"`

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

Expand Down
94 changes: 54 additions & 40 deletions ec2-automate-backup/ec2-automate-backup-awscli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,43 @@ get_EBS_List() {
ebs_backup_list=$(echo "$ebs_backup_list_complete" | grep ^VOLUMES | cut -f 7)
}

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 $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"
fi
#if $hostname_tag_create is true then append --tag InitiatingHost=$(hostname -f) to the variable $snapshot_tags
if $hostname_tag_create; then
snapshot_tags="$snapshot_tags Key=InitiatingHost,Value='$(hostname -f)'"
fi
#if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags
if [[ -n $purge_after_date_fe ]]; then
snapshot_tags="$snapshot_tags Key=PurgeAfterFE,Value=$purge_after_date_fe Key=PurgeAllow,Value=true"
fi
#if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags
if $user_tags; then
snapshot_tags="$snapshot_tags Key=Volume,Value=${ebs_selected} Key=Created,Value=$current_date"
fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using aws ec2 create-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)
fi
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 $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"
fi
#if $hostname_tag_create is true then append --tag InitiatingHost=`hostname -f` to the variable $snapshot_tags
if $hostname_tag_create
then
snapshot_tags="$snapshot_tags Key=InitiatingHost,Value='`hostname -f`'"
fi
#if $purge_after_date_fe is true, then append $purge_after_date_fe to the variable $snapshot_tags
if [[ -n $purge_after_date_fe ]]
then
snapshot_tags="$snapshot_tags Key=PurgeAfterFE,Value=$purge_after_date_fe Key=PurgeAllow,Value=true"
fi

if [[ -n $label ]]
then
snapshot_tags="$snapshot_tags Key=Name,Value=$label"
fi

#if $user_tags is true, then append Volume=$ebs_selected and Created=$current_date to the variable $snapshot_tags
if $user_tags
then
snapshot_tags="$snapshot_tags Key=Volume,Value=${ebs_selected} Key=Created,Value=$current_date"
fi

#if $snapshot_tags is not zero length then set the tag on the snapshot using aws ec2 create-tags
if [[ -n $snapshot_tags ]]
then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags: $snapshot_tags"
tags_arugment="--tags $snapshot_tags"
aws_ec2_create_tag_result=`aws ec2 create-tags --resources $ec2_snapshot_resource_id --region $region $tags_arugment --output text 2>&1`
fi
}

get_date_binary() {
Expand Down Expand Up @@ -151,21 +163,23 @@ user_tags=false
purge_snapshots=false
#handles options processing

while getopts :s:c:r:v:t:k: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" ;;
n) name_tag_create=true ;;
h) hostname_tag_create=true ;;
p) purge_snapshots=true ;;
u) user_tags=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
while getopts :l:s:c:r:v:t:k:pnhu opt
do
case $opt in
l) label="$OPTARG";;
s) selection_method="$OPTARG";;
c) cron_primer="$OPTARG";;
r) region="$OPTARG";;
v) volumeid="$OPTARG";;
t) tag="$OPTARG";;
k) purge_after_input="$OPTARG";;
n) name_tag_create=true;;
h) hostname_tag_create=true;;
p) purge_snapshots=true;;
u) user_tags=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

#sources "cron_primer" file for running under cron or other restricted environments - this file should contain the variables and environment configuration required for ec2-automate-backup to run correctly
if [[ -n $cron_primer ]]; then
Expand Down