Saturday, 22 October 2016

Script to take Snapshots of Disk Google Cloud

How to take Regular snapshot of VM Disks on Google Cloud

Bash Script to take Backup of Disks on Google cloud

Google Cloud VM disks backup can be taken via its command line tool "gcloud". A script running some gcloud command and the script itself running with the cron gives you the power to automate backup of disks.

Following is the script to take backup:

#!/bin/bash
#
# create snapshot of disk
#
 
echo "Backup is running on `date +%Y%m%d-%H%M`"
 
 
# list of disks
declare -a arr=("example-disk-1" "example-disk-2" "example-disk-n")
 
# current timestamp to stick with snapshot
TIMESTAMP=`date +%Y%m%d-%H%M`
 
for i in "${arr[@]}"
do
    echo "sn-$TIMESTAMP-$i"
    gcloud compute disks snapshot $i --snapshot-names "sn-$TIMESTAMP-$i" --zone us-east1-c
    if [ $? -eq 0 ]; then
        echo "snapshot for $i created sucessfully...\n"
    else
        echo "Problem in creating snapshot ... "
        exit
    fi
done

Just replace the name of the disk "example-disk-1" with yours and set this script to run via cron.

PS: Also make sure you have gcloud installed, by default all google VM have gcloud already installed.

After setting this up, you might want to know "How to purge disk snapshot older than 7 days".

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel