-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_snapshots.sh
executable file
·37 lines (32 loc) · 1.51 KB
/
create_snapshots.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash -ex
# create snapshots for every volume on every ec2 instance based on the instance name
#
# Requires the AWS CLI tools: http://aws.amazon.com/cli/
#
# e.g. dev-apache-*.ernie.com
namefilter="$1"
if [ ! "$1" ]
then
namefilter="*"
fi
tempfile=$(mktemp /tmp/create_snapshot.XXXXXX)
for instance_id in $(aws ec2 describe-instances --filter "Name=tag:Name,Values=$namefilter" --output text --query "Reservations[*].Instances[*].InstanceId")
do
#attachment.instance-id
instance_name=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$instance_id" "Name=key,Values=Name" | grep '"Value": "' | cut -f4 -d\")
for volume_id in $(aws ec2 describe-volumes --filter "Name=attachment.instance-id,Values=$instance_id" --query "Volumes[*].VolumeId" --output text)
do
script=$(basename $0)
user="$USER"
host=$(hostname)
description="Created with $script by $user on host $host for ec2 instance $instance_id"
aws ec2 create-snapshot --volume-id $volume_id --description "$description" | tee "$tempfile"
snapshot_id=$(grep '"SnapshotId": "snap-' "$tempfile" | cut -f4 -d\")
device_name=$(aws ec2 describe-volumes --volume-id $volume_id --query 'Volumes[*].Attachments[*].Device' --output text)
echo "snapshot_id: $snapshot_id"
echo "instance_name: $instance_name"
echo "device_name: $device_name"
aws ec2 create-tags --resources $snapshot_id --tags Key=Name,Value="$instance_name $device_name"
done
done
rm "$tempfile"