-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightsail-backup-and-cleanup.sh
47 lines (35 loc) · 1.62 KB
/
lightsail-backup-and-cleanup.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
#!/bin/bash
######################################
## CREATE A NEW SNAPSHOT BACKUP ##
######################################
## This script takes in three arguments the name of your instance, the region, and the number of snapshots to keep.
NameOfYourInstance=$1
NameOfYourBackup=$NameOfYourInstance
Region=$2
aws lightsail create-instance-snapshot --instance-snapshot-name ${NameOfYourBackup}-$(date +%Y-%m-%d_%H.%M) --instance-name $NameOfYourInstance --region $Region
## Delay before initiating clean up of old snapshots
sleep 30
###############################################
## DELETE OLD SNAPSHOTS + RETAIN SNAPSHOTS ##
###############################################
# Set number of snapshots you'd like to keep in your account
snapshotsToKeep=$3
echo "Number of Instance Snapshots to keep: ${snapshotsToKeep}"
# get the total number of available Lightsail snapshots
numberOfSnapshots=$(aws lightsail get-instance-snapshots | jq '[.[] | select(.[].fromInstanceName == "'${NameOfYourInstance}'") ]| length')
echo "Number of instance snapshots: ${numberOfSnapshots}"
# get the names of all snapshots sorted from old to new
SnapshotNames=$(aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | map(select(.fromInstanceName == "'${NameOfYourInstance}'")) | .[].name')
# loop through all snapshots
while IFS= read -r line
do
let "i++"
# delete old snapshots condition
if (($i <= $numberOfSnapshots-$snapshotsToKeep))
then
snapshotToDelete=$(echo "$line" | tr -d '"')
# delete snapshot command
aws lightsail delete-instance-snapshot --instance-snapshot-name $snapshotToDelete
fi
done <<< "$SnapshotNames"
exit 1