Last active
November 11, 2015 03:43
-
-
Save flashvoid/71a6444df2ec380a9848 to your computer and use it in GitHub Desktop.
AWS ebs snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# creating snapshot of jenkins instance | |
MOUNTPOINT=/u1 | |
DEVICE="/dev/xvdf" # TODO discover | |
AWS_DEVICE="/dev/sdf" | |
LINEAGE="ci" | |
KEEP=10 # how many shapshots to keep | |
VOLUME_ID=$(aws ec2 describe-instances --instance-id `ec2metadata --instance-id` --query "Reservations[].Instances[].BlockDeviceMappings[]" --output json | jq -r '.[] | if .DeviceName == "/dev/sdf" then .Ebs.VolumeId else empty end ') | |
xfs_freeze -f $MOUNTPOINT | |
SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id $VOLUME_ID | awk '{ print $3 }') | |
xfs_freeze -u $MOUNTPOINT | |
aws ec2 create-tags --resources $SNAPSHOT_ID --tags Key=Lineage,Value=$LINEAGE | |
# Purge | |
toEpoch () { while read A; do D=${A/snap-?????????}; ID=${A%%????????????????????????Z}; echo "$ID $(date -d $D +%s)"; done; } | |
OLD_SNAPSHOTS=$(aws ec2 describe-snapshots --filter Name=tag:Lineage,Values=ci --query "Snapshots[].[SnapshotId,StartTime]" | xargs -n2 | toEpoch | sort -r -n -k 2 | tail -n +${KEEP} | awk '{ print $1 }') | |
echo "Purging old snapshots $OLD_SNAPSHOTS" | |
for s in $OLD_SNAPSHOTS; do | |
aws ec2 delete-snapshot --snapshot-id $s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment