Skip to content

Instantly share code, notes, and snippets.

@Aricg
Last active December 16, 2015 19:00
Show Gist options
  • Select an option

  • Save Aricg/5482068 to your computer and use it in GitHub Desktop.

Select an option

Save Aricg/5482068 to your computer and use it in GitHub Desktop.
uses ec2-create-snapshot ec2-describe-instances and some awk to make a snapshot for each attached volume in any number of ec2 environments and across all availability zones . Assumes keys are in $KEYDIR and are named foo.key and foo.pub
#!/bin/bash
#########################
# How to use
# Naviage to the aws/securityCredentials page and generate a x.509 certificate
# take both the public and the private certificate file and place them in $KEYDIR
# rename the public and private certificate foo.pub and foo.key respectivly
# you may provide this script with any number of certificate pairs
#
# What it does
# This script takes a SNAPSHOT of all ATTACHED volumes across all AVALIABILITY zones.
# Bascially it covers your ass.
#
# Aric Gardner 2013
#
# Copyleft Information
# Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument.
# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
##########################
version="1.0"
LOG="snapshots.log"
LOGDIR="/var/log/aws/"
KEYDIR="/home/ubuntu/KEYS/"
whoareyou () {
if [[ $(whoami) != "root" ]]
then
echo "This script must be run as user root"
exit 1
fi
}
#pretty logs
log() {
if [ ! -d "$LOGDIR" ]; then
mkdir -p "$LOGDIR" > /dev/null 2>&1
fi
if [ ! -f "$LOGDIR""$LOG" ]; then
touch "$LOGDIR""$LOG"
log "Creating Log File"
fi
echo "$(date "+%Y/%m/%d %H:%M:%S"): $@" | tee -a "$LOGDIR""$LOG"
}
#Keys must be in the format projectname.key and projectname.pem
get_clients()
{
for x in $(find "$KEYDIR"* -type f | grep ".key");
do
describe_instances "$@"
done
}
getvol() {
getvol=()
while read -d $'\n'; do
getvol+=("$REPLY")
done < <(cat tmp_info)
}
inventory () {
for description in volumes snapshots instances
do
log "Logging "$(basename ${x%.*})"'s "$description" in $zone avaliablity zone (this can take a while)"
ec2-describe-"$description" --headers $key > "$LOGDIR"instances-"$zone"-"$(basename ${x%.*})"
done
}
#Get a list of avaliable avaliablility zones to ensure we snapshot ATTACHED volmes in all zones
describe_instances() {
if [[ ! -e tmp_zones ]]; then
ec2-describe-regions -C ${x%.*}.pub -K ${x%.*}.key | awk '{ print $2 }' > tmp_zones
fi
for zone in $(cat tmp_zones)
do
key="--region "$zone" -C ${x%.*}.pub -K ${x%.*}.key"
if [[ $inventory == true ]]; then
inventory "$@"
fi
#I'm certain there is a better way to deal with tmp files.
if [ -e tmp_info ]; then
rm tmp_info
fi
#this prepares the information to be parsed.
if [[ $snapshot == true ]] || [[ $test == true ]];
then
log "running ec2-describe-instances to find "$(basename ${x%.*})"'s volumes in $zone avaliablity zone (this can take a while)"
if [[ $test == true ]]; then log " this is only a test"; fi
ec2-describe-instances $key |grep -v RESERVATION | grep -v TAG | awk '{print $2 " " $3 }' | sed 's,ami.*,,g' | sed -E '/^i-/ i\\n' | awk 'BEGIN { FS="\n"; RS="";} { for (i=2; i<=NF; i+=1){print $1 " " $i}}' > tmp_info
getvol "$@"
makesnap "$@"
fi
done
}
makesnap () {
for vol in "${getvol[@]}";
do
instance=$(echo $vol | awk '{print $1}')
device=$(echo $vol | awk '{print $2}')
volume=$(echo $vol | awk '{print $3}')
#I need to call ec2tag with the ouput if the snapshot command so I made the output a variable, probably not the best thing to do. but. meh.
#test
if [[ $test == true ]]; then
log "TEST COMMAND OUTPUT : ec2-create-snapshot $key --description ""$volume" of "$device" of "$instance"" "$volume""
else
#Actual
if snap="$(ec2-create-snapshot $key --description ""$volume" of "$device" of "$instance"" "$volume" | awk '{print $2}')"; then
log "Snapshot "$snap" succeeded for client "${x%.*}" "
ec2tag $key "$snap" --tag Name="Backup of "$volume" of "$device" of "$instance""
else
status=$?
log "This command failed: ec2-create-snapshot $key --description ""$volume" of "$device" of "$instance"" "$volume""
log "With this status $status"
fi
fi
done
}
usage() {
cat << EOF
"$0": ensures a snapshot is made for all attached volumes in all zones for all clients
version: $version
usage: $0 [OPTIONS]
-h Show this message
-t Run in test mode
-s Run in snapshot mode
-i Run in inventory mode
-d Run in delete old snapshots mode
-l Choose log dir
-k Choose key dir
Example $0 -t -l $LOGDIR -k $KEYDIR
Note: keys must be in the format projectname.key and projectname.pub
detected keys:
EOF
for x in $(find "$KEYDIR"* -type f | grep ".key"); do
basename "${x%.*}"
done
echo ""
exit 1
}
whoareyou
if [[ -z "$@" ]]; then usage
fi
while getopts ":tlkhis" OPTION
do
case $OPTION in
t ) test=true ;;
l ) LOGDIR="$OPTARG" ;;
k ) KEYDIR="$OPTARG" ;;
i ) inventory=true ;;
s ) snapshot=true ;;
h ) usage; exit;;
\? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
# : ) echo "Missing option argument for -$OPTARG" >&2; exit 1;;
* ) echo "Unimplimented option: -$OPTARG" >&2; exit 1;;
esac
done
get_clients "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment