-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.sh
71 lines (53 loc) · 1.56 KB
/
aws.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
#********* Fill in your email address and instance name ************
#Set email address
email="YOUR_EMAIL_ADDRESS"
#Set Day to do backps, Sunday is 0
bud=0
#Determine year
year=$(date +%Y)
#Determine last year
lyear=`expr $year - 1`
#echo $lyear
# How many weeks of snapshots to keep
delete=2
# Name the new weekly snapshot the week number
week=$(date +%V)
#echo $week
#Name the snapshot to delete
twoweeks=`expr $week - $delete`
#echo $twoweeks
#Weeks in the year- will be corrected below for years with 53 weeks
numweeks=52
#Set week number for testing
#week=1
#Adjust delete name for first 2 weeks of the year
if [ $week -eq 1 ]
then
lastsunday=$(cal 12 $lyear | awk '/^ *[0-9]/ { d=$1 } END { print d }')
#echo $lastsunday
numweeks=$(date -d "$lyear-12-$lastsunday" +%V)
#echo $numweeks
twoweeks=`expr $numweeks - 1`
#echo $twoweeks
fi
if [ $week -eq 2 ]
then
lastsunday=$(cal 12 $lyear | awk '/^ *[0-9]/ { d=$1 } END { print d }')
#echo $lastsunday
numweeks=$(date -d "$lyear-12-$lastsunday" +%V)
#echo $numweeks
twoweeks=$numweeks
#echo $twoweeks
fi
#Adjust for adding a 0 in front of weeks 1 - 9
if [ $twoweeks -lt 10 ]
then
printf -v newtwoweeks "%02d" $twoweeks
twoweeks=$newtwoweeks
fi
#echo $twoweeks
#Create new snapshot
/usr/local/bin/aws lightsail create-instance-snapshot --instance-name YOUR_INSTANCE_NAME --instance-snapshot-name $week 2>&1 | mail -s "Create Snapshot for AWS" $email
#Delete older snapshot
/usr/local/bin/aws lightsail delete-instance-snapshot --instance-snapshot-name $twoweeks 2>&1 | mail -s "Delete Snapshot for AWS" $email