GCP - Create disk snapshot

Create disk snapshot on Google Cloud Platform.

To create disk snapshot I'm using Cloud Tools installed on all VMs created on GCP.

gcloud compute disks snapshot DISKNAME --zone ZONE --snapshot-names "OPTIONAL"

More information: https://cloud.google.com/sdk/gcloud/reference/compute/disks/snapshot

This is a simple script which will loop through a list of disks and create a snapshot and delete snapshots created 30 days ago

#!/bin/bash

# To get list of all disk in project
# DISK_LIST=$(gcloud compute disks list | awk '{print $1 " --zone " $2}' | sed 1d)

DISKLIST=('DISK-1 --zone ZONE' 'DISK-2 --zone europe-west1-d')

echo "== Generating disk snapshots"

DATE=$(date "+%Y%m%d")
PASTDATE=$(date -d "`date` - 30 days" +%Y%m%d)

echo "== Today is: $DATE"

for d in "${DISKLIST[@]}"
do
        echo "$d"
        DISKNAME=$(echo $d | awk '{print $1}')
        gcloud compute disks snapshot $d --snapshot-names "$DISKNAME-$DATE"

        echo "==DELETE SNAPSHOT $DISKNAME-$PASTDATE"

        gcloud compute snapshots delete "$DISKNAME-$PASTDATE" -q

done

echo "== Done"