-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·31 lines (25 loc) · 880 Bytes
/
backup.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
#!/bin/bash
set -e
# set the amount of images to keep
KEEP_SNAPSHOTS=${KEEP:-2}
# get all server ids in project
SERVERSID=$(hcloud server list -o noheader -o columns=id)
function make_snapshot {
echo "Creating snapshot to server id $1"
hcloud server create-image --type snapshot --label id=$1 --description server-$1 --poll-interval 0 $1
}
function rotate_snapshots {
echo "Check if rotate is needed"
if [[ `hcloud image list --type snapshot --selector id=$1 -o noheader -o columns=id | wc -l | xargs` > $KEEP_SNAPSHOTS ]]; then
echo "Deleting oldest snapshot"
SNAPSHOTID=$(hcloud image list --type snapshot --selector id=$1 -o noheader -o columns=id | head -1)
hcloud image delete $SNAPSHOTID
else
echo "No need to delete snapshot"
fi
}
for ID in $SERVERSID; do
echo "working on server id $ID"
make_snapshot $ID
rotate_snapshots $ID
done