Last active
April 15, 2016 12:08
-
-
Save britweb-tim/7f249f419ea9a1ef97083381f074901c to your computer and use it in GitHub Desktop.
Snapshot all disks in current project with Google Cloud Console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Inspired by http://gcloud-script-disk-snapshot.blogspot.co.uk/2015/04/gcloud-snapshot-script.html | |
# | |
#Delete the snapshots older than 7 days | |
retention_preiod=7 | |
removal_date=`date --date="$retention_preiod days ago" +%Y%m%d%H%M%S` | |
dt=`date +%Y%m%d%H%M%S` | |
echo $dt | |
#Delimiter to exclude is "\|" | |
exclude_list="NAME" | |
all_disks=`gcloud compute disks list |awk '{print $1"="$2}'|grep -v $exclude_list` | |
for line in `echo -e $all_disks` ;do | |
zone=`echo $line |awk -F"=" '{print $2}'` | |
disks=`echo $line |awk -F"=" '{print $1}'` | |
gcloud compute disks snapshot $disks --zone $zone --snapshot-name "snapshot-$disks-$dt" | |
done | |
#Code to delete the older snapshots | |
#Mention snapshots which you do not want to delete | |
exclude_snap="" | |
snapshot_list=`gcloud compute snapshots list|grep snapshot|awk '{print $1}'|grep -v "$exclude_snap"` | |
for list in `echo $snapshot_list`;do | |
backup_date=`echo $list |awk -F "-" '{print $2}'` | |
if [ $backup_date -le $removal_date ];then | |
echo "Delete the backup $list" | |
gcloud compute snapshots delete $list -q | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment