-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot
35 lines (31 loc) · 1.39 KB
/
snapshot
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
#!/bin/bash
# This script takes snapshots of the files/data directory on google cloud instances
# To setup as a cron job:
# 0 9 * * * /apps/spidamin/spida/disk-snapshot.sh $DISK_NAME $EMAIL $CLIENT_NAME $ENVIRONMENT $PRODUCT $ZONE
# create.sh script uses disk-snapshot.sh as the file name (instead of snapshot.sh) when storing the script on new instances
DT=`date +%Y%m%d`
DT2=`date -d'-1 week' +%Y%m%d`
DISK_NAME="$1"
EMAIL="$2"
CLIENT_NAME="$3"
ENVIRONMENT="$4"
PRODUCT="$5"
ZONE="$6"
if [[ "$ZONE" = "" ]]; then
ZONE="us-central1-a"
fi
sudo sync
sudo fsfreeze -f /apps/spidamin
std_err=$(gcloud compute disks snapshot $DISK_NAME --snapshot-names $DISK_NAME-v$DT --zone $ZONE --labels=client=$CLIENT_NAME,env=$ENVIRONMENT,product=$PRODUCT 2>&1 > /dev/null)
if [[ $? -ne 0 ]]; then
echo "error creating disk snapshot on $(hostname): $std_err" | mailx -s "$(hostname) gcloud disk snapshot error" "$EMAIL"
fi
sudo fsfreeze -u /apps/spidamin
std_err=$(gcloud compute snapshots list --filter="name=('$DISK_NAME-v$DT2')" 2>&1 > /dev/null)
# If the list command doesn't return a snapshot, then stderr is "Listed 0 items." and the exit code is 0
if [[ "$std_err" == "" ]]; then
std_err=$(gcloud compute snapshots delete $DISK_NAME-v$DT2 -q 2>&1 > /dev/null)
if [[ $? -ne 0 ]]; then
echo "error deleting disk snapshot on $(hostname): $std_err" | mailx -s "$(hostname) gcloud disk snapshot error" "$EMAIL"
fi
fi