google-cloud-console


Creating automatic disk snapshot (backup) retention on Google Cloud Platform


I'd like to know if there is a way to create an automatic backup retention for an instance (disk more specifically). I'm not very experienced with the console so I prefer a GUI way, but if there is no I'll be glad to know about the console method.
I was looking for this but only found a solution for SQL.
Thanks!
I had the same problem, so I created a simple shell script to take a daily snapshot and delete all snapshots over 7 days: https://github.com/jacksegal/google-compute-snapshot
#!/usr/bin/env bash
export PATH=$PATH:/usr/local/bin/:/usr/bin
#
# CREATE DAILY SNAPSHOT
#
# get the device name for this vm
DEVICE_NAME="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/disks/0/device-name" -H "Metadata-Flavor: Google")"
# get the device id for this vm
DEVICE_ID="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
# get the zone that this vm is in
INSTANCE_ZONE="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google")"
# strip out the zone from the full URI that google returns
INSTANCE_ZONE="${INSTANCE_ZONE##*/}"
# create a datetime stamp for filename
DATE_TIME="$(date "+%s")"
# create the snapshot
echo "$(gcloud compute disks snapshot ${DEVICE_NAME} --snapshot-names gcs-${DEVICE_NAME}-${DEVICE_ID}-${DATE_TIME} --zone ${INSTANCE_ZONE})"
#
# DELETE OLD SNAPSHOTS (OLDER THAN 7 DAYS)
#
# get a list of existing snapshots, that were created by this process (gcs-), for this vm disk (DEVICE_ID)
SNAPSHOT_LIST="$(gcloud compute snapshots list --regexp "(.*gcs-.*)|(.*-${DEVICE_ID}-.*)" --uri)"
# loop through the snapshots
echo "${SNAPSHOT_LIST}" | while read line ; do
# get the snapshot name from full URL that google returns
SNAPSHOT_NAME="${line##*/}"
# get the date that the snapshot was created
SNAPSHOT_DATETIME="$(gcloud compute snapshots describe ${SNAPSHOT_NAME} | grep "creationTimestamp" | cut -d " " -f 2 | tr -d \')"
# format the date
SNAPSHOT_DATETIME="$(date -d ${SNAPSHOT_DATETIME} +%Y%m%d)"
# get the expiry date for snapshot deletion (currently 7 days)
SNAPSHOT_EXPIRY="$(date -d "-7 days" +"%Y%m%d")"
# check if the snapshot is older than expiry date
if [ $SNAPSHOT_EXPIRY -ge $SNAPSHOT_DATETIME ];
then
# delete the snapshot
echo "$(gcloud compute snapshots delete ${SNAPSHOT_NAME} --quiet)"
fi
done

Related Links

Creating automatic disk snapshot (backup) retention on Google Cloud Platform
GCE persistent disk data management
How do I see the errors shown in the Google Developers Console Project Page?

Categories

HOME
oracle12c
gridview
awk
android-studio-2.2
pyqt
sh
hugo
vsm
data-visualization
pkcs#11
computer-science
extjs4.2
g++
little-proxy
resultset
gkturnbasedmatch
onedrive-api
shared-libraries
event-sourcing
standards
asp.net-mvc-viewmodel
replication
sar
kendo-mobile
exploit
comparator
roundup
interbase
user-defined-functions
iis-express
paragraph
github3.py
sendbird
hivemq
alphabetical
xdocreport
modernizr
android-augmented-reality
verifiable-c
signals-slots
visa
attributeerror
dbnull
logback-groovy
chronicle-map
smoothstate.js
file-sharing
berkeley-db-je
x++
sonarlint-eclipse
reportservice2010
tinymce-3
magic-draw
static-cast
appdomain
google-photos
structuremap4
filenet
javafx-css
cvxpy
gets
cronexpression
stax
flex-monkey
ng-grid
hclust
delta
spreadjs
contentcontrol
user-management
concept-insights
maybe
themoviedb-api
video-player
doctype
finalbuilder
ocra
liquid-layout
skspritenode
diff3
rrule
broadband
public-html
exceldatareader
apache-spark-1.3
iso-prolog
jjaql
paste
extjs2
artemis
ie8-compatibility-mode
gpars
mvc-editor-templates
scmmanager
insert-id
boost-msm
xcode3.2
floating-point-exceptions
workflow-services
landscape-portrait
google-ajax-api
resharper-6.0
ie-compatibility-mode
updatesourcetrigger
silent
dot-emacs
addchild
zen
pos-for-.net
purepdf
feasibility
ifilter
pnrp

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App