-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshot.sh
42 lines (36 loc) · 1.27 KB
/
snapshot.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
#!/bin/bash
api=""
now=$(date +"%Y-%m-%d")
snapshot_name=""
snapshot_tags=""
volume_name=""
## check value of predefined variable
if [ "$api" == "" ] || [ "$snapshot_name" == "" ] || [ "$snapshot_tags" == "" ] || [ "$volume_name" == "" ];
then
echo "Please check your variable value"
exit 1
fi
## get volume id
volume_id=$(curl -X GET \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$api \
"https://api.digitalocean.com/v2/volumes?name=$volume_name" | \
jq -r '.volumes[].id')
# get snaphost id that would deleted
snapshot_id=$(curl -X GET \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$api \
"https://api.digitalocean.com/v2/volumes/$volume_id/snapshots?page=1&per_page=1" | \
jq '.snapshots | map(select((.created_at < "'$now'" and .name == "'$snapshot_name'")))' | \
jq -r '.[].id')
## delete snapshot
curl -X DELETE \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$api \
"https://api.digitalocean.com/v2/snapshots/$snapshot_id"
## create new snapshot today
curl -X POST \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer '$api \
-d '{"name":"'$snapshot_name'","tags":["'$snapshot_tags'"]}' \
"https://api.digitalocean.com/v2/volumes/$volume_id/snapshots"