-
Notifications
You must be signed in to change notification settings - Fork 570
/
ec2-ebs-create-snapshots.sh
executable file
·58 lines (45 loc) · 2.07 KB
/
ec2-ebs-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
# This script takes a snapshot of each EC2 volume that is tagged with Backup=1
# TODO: Add error handling and loop breaks
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
echo "AWS config not found or CLI not installed. Please run \"aws configure\"."
exit 1
fi
fi
DESCRIBEVOLUMES=$(aws ec2 describe-volumes --filter Name=tag:Backup,Values="1")
TOTALBACKUPVOLUMES=$(echo "$DESCRIBEVOLUMES" | grep Name | cut -f 3 | nl | wc -l)
echo " "
echo "====================================================="
echo "Creating EC2 Snapshots for Volumes with tag Backup=1"
echo "Snapshots to be created:"$TOTALBACKUPVOLUMES
echo "====================================================="
echo " "
START=1
for (( COUNT=$START; COUNT<=$TOTALBACKUPVOLUMES; COUNT++ ))
do
echo "====================================================="
echo \#$COUNT
VOLUME=$(echo "$DESCRIBEVOLUMES" | cut -f 9 | nl | grep -w $COUNT | cut -f 2)
echo "Volume ID: "$VOLUME
NAME=$(echo "$DESCRIBEVOLUMES" | grep Name | cut -f 3 | nl | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
echo "Volume Name: "$NAME
CLIENT=$(echo "$DESCRIBEVOLUMES" | grep Client | cut -f 3 | nl | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
echo "Client: "$CLIENT
DESCRIPTION=$NAME-$(date +%m-%d-%Y)
echo "Snapshot Description: "$DESCRIPTION
SNAPSHOTRESULT=$(aws ec2 create-snapshot --volume-id $VOLUME --description $DESCRIPTION)
# echo "Snapshot result is: "$SNAPSHOTRESULT
SNAPSHOTID=$(echo $SNAPSHOTRESULT | cut -d ' ' -f5)
echo "Snapshot ID: "$SNAPSHOTID
# echo $SNAPSHOTID | grep -E "snap-........"
# sleep 3
TAGRESULT=$(aws ec2 create-tags --resources $SNAPSHOTID --tags Key=Name,Value=$NAME Key=Client,Value=$CLIENT Key=SnapshotCreation,Value=Automatic Key=SnapshotDate,Value=$(date +%m-%d-%Y))
# echo "Tag Result is: "$TAGRESULT
done
echo "====================================================="
echo " "
echo "Completed!"
echo " "