Skip to content
Closed
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: 3 additions & 1 deletion ec2-automate-backup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ the above example would provide a single backup of the EBS volumeid vol-6d6a0527
ec2-automate-backup requires one of the following two parameters be provided:

`-v <volumeid>` - the "volumeid" parameter is required to select EBS volumes for snapshot if ec2-automate-backup is run using the "volumeid" selection method - the "volumeid" selection method is the default selection method.

`-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.
Expand All @@ -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` - the -i flag will tag with the snapshot attached instance name.

# 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
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeVolumes",
"ec2:CreateSnapshot",
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot",
"ec2:CreateTags",
"ec2:DescribeTags"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:DescribeVolumes",
"ec2:CreateSnapshot",
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot",
"ec2:CreateTags",
"ec2:DescribeTags",
"ec2:DescribeInstances"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
}
23 changes: 19 additions & 4 deletions ec2-automate-backup/ec2-automate-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ get_EBS_List() {
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
#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 All @@ -54,6 +54,10 @@ get_EBS_List() {
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="Key=CreatedBy,Value=ec2-automate-backup"
#if $instance_name_tag_create is true then append ec2_volume_instance_name to the variable $snapshot_tags
if $instance_name_tag_create; then
snapshot_tags="$snapshot_tags Key=InstanceName,Value=$ec2_volume_instance_name"
fi
#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"
Expand Down Expand Up @@ -113,7 +117,7 @@ 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')

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)
Expand Down Expand Up @@ -149,9 +153,11 @@ hostname_tag_create=false
user_tags=false
#sets the Purge Snapshot feature to false - if purge_snapshots=true then snapshots will be purged
purge_snapshots=false
#sets the "InstanceName" tag set for a snapshot to be false
instance_name_tag_create=false
#handles options processing

while getopts :s:c:r:v:t:k:pnhu opt; do
while getopts :s:c:r:v:t:k:pnhui opt; do
case $opt in
s) selection_method="$OPTARG" ;;
c) cron_primer="$OPTARG" ;;
Expand All @@ -163,6 +169,7 @@ while getopts :s:c:r:v:t:k:pnhu opt; do
h) hostname_tag_create=true ;;
p) purge_snapshots=true ;;
u) user_tags=true ;;
i) instance_name_tag_create=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
Expand Down Expand Up @@ -204,11 +211,19 @@ 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
if $instance_name_tag_create; then
#get the id of the instance the volume is attached to
ec2_volume_instance_id=$(aws ec2 describe-volumes --region $region --output text --volume-id $ebs_selected --query 'Volumes[*].[Attachments[*].InstanceId]')
#get the name of the instance for the instance id
ec2_volume_instance_name=$(aws ec2 describe-instances --region $region --output text --instance-id $ec2_volume_instance_id --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value')
#replace spaces with underscores (issue with tagging)
ec2_volume_instance_name=$(echo "$ec2_volume_instance_name"|sed -e 's/ /_/g')
fi
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)
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