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

Added duplicate tag(s) to snapshot tag(s). #138

Open
wants to merge 2 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 @@ -25,6 +25,8 @@ ec2-automate-backup requires one of the following two parameters be provided:

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

`-d <key>` - duplicate specified volume tag(s) to snapshot tag(s). For example, 'ec2-automate-backup.sh -v vol-6d6a0527 -d "Name¤Type"' would duplicate tag key Name and Type with there values from volume to snapshot. As you can see in the example the delimiter between keys is "¤". This function will not, at the moment, work if key or value contains a space.

`-h` - tag snapshots "InitiatingHost" tag to specify which host ran the script

`-k <purge_after_days>` - the period after which a snapshot can be purged. For example, running "ec2-automate-backup.sh -v "vol-6d6a0527 vol-636a0112" -k 31" would allow snapshots to be removed after 31 days. purge_after_days creates two tags for each volume that was backed up - a PurgeAllow tag which is set to PurgeAllow=true and a PurgeAfter tag which is set to the present day (in UTC) + the value provided by -k.
Expand Down
18 changes: 16 additions & 2 deletions ec2-automate-backup/ec2-automate-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ get_EBS_List()
then echo -e "An error occured when running ec2-describe-volumes. The error returned is below:\n$ebs_backup_list_complete" 1>&2 ; exit 70
fi
ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2`
ebs_backup_list_tags_complete=`echo "$ebs_backup_list_complete" | grep ^TAG`
#code to right will output list of EBS volumes to be backed up: echo -e "Now outputting ebs_backup_list:\n$ebs_backup_list"
}

Expand Down Expand Up @@ -73,11 +74,23 @@ create_EBS_Snapshot_Tags()
snapshot_tags="$snapshot_tags --tag Volume=${ebs_selected} --tag Created=$date_current"
fi

#if $tag_duplication is set, then duplicate specified volume tags to snapshot tags
if [[ -n $tag_duplication ]]
then
ebs_selected_tags=`echo "$ebs_backup_list_tags_complete" | grep $ebs_selected`
IFS='¤' read -ra tags_for_duplication <<< "$tag_duplication"
for tag_key in "${tags_for_duplication[@]}"; do
tag_value=`echo "$ebs_selected_tags" | grep "$tag_key" | cut -f 5`
snapshot_tags="$snapshot_tags --tag '$tag_key=$tag_value'" #FIXME script can't handle spances in key or value
done
fi

#if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags
if [[ -n $snapshot_tags ]]
then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags:"
ec2-create-tags $ec2_snapshot_resource_id --region $region $snapshot_tags
fi

}

date_binary_get()
Expand Down Expand Up @@ -168,7 +181,7 @@ user_tags=false
#sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfter" tag that is earlier than current date
purge_snapshots=false
#handles options processing
while getopts :s:c:r:v:t:k:pnhu opt
while getopts :s:c:r:v:t:k:d:pnhu opt
do
case $opt in
s) selection_method="$OPTARG";;
Expand All @@ -177,6 +190,7 @@ while getopts :s:c:r:v:t:k:pnhu opt
v) volumeid="$OPTARG";;
t) tag="$OPTARG";;
k) purge_after_days="$OPTARG";;
d) tag_duplication="$OPTARG";;
n) name_tag_create=true;;
h) hostname_tag_create=true;;
p) purge_snapshots=true;;
Expand Down Expand Up @@ -241,4 +255,4 @@ done
if $purge_snapshots
then echo "Snapshot Purging is Starting Now."
purge_EBS_Snapshots
fi
fi