Skip to content

Instantly share code, notes, and snippets.

@tuxpiper
Created April 18, 2014 10:05
Show Gist options
  • Save tuxpiper/11035567 to your computer and use it in GitHub Desktop.
Save tuxpiper/11035567 to your computer and use it in GitHub Desktop.
EBS snapshot based on DeviceName
#!/bin/bash
# Requirements:
# - awscli ( https://pypi.python.org/pypi/awscli )
# - jq ( http://stedolan.github.io/jq )
# - curl
# - configuring crendentials properly (see below)
# Invoke with devicename matching the EBS to be backed up (i.e. "/dev/sdm")
[ -z "$1" ] && { echo "Call me with the devicename of the EBS you want to back up!"; exit 1; }
#
device_name=$1
# Configure these properly
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_DEFAULT_REGION=...
get_instance_id() {
curl http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null
}
get_vol_id() {
local instance_id=$1
local device_name=$2
aws ec2 describe-instances --instance-ids $1 | \
jq '.Reservations[0].Instances[0].BlockDeviceMappings |
map(select(.DeviceName == "'$2'")) | .[0] | .Ebs.VolumeId' | \
tr -d \"
}
aws ec2 create-snapshot --volume-id $(get_vol_id `get_instance_id` $device_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment