How-make-automatic-snapshots-using-google-compute-engine
Referance Articles : http://www.smiss.com.ua/blog/how-make-automatic-snapshots-using-google-compute-engine and https://cloud.google.com/compute/docs/gcutil/
Objective : how-make-automatic-snapshots-using-google-compute-engine
Snapshots are used for Backup purpose . This document talks about how to create automatic backups i.e Snapshot Backup
Process
login to the google cloud compute engine and look for "gcutil" via locate command
If Not available then Install gcutil as below
Install gcutil
To install gcutil:
Download gcutil-1.16.1.tar.gz file.
wget https://dl.google.com/dl/cloudsdk/release/artifacts/gcutil-1.16.1.tar.gz
Extract the files.
Run the following command to extract the tar file. This unpacks a directory named gcutil-1.16.1 in /usr/local/share.
sudo tar xzvpf gcutil-1.16.1.tar.gz -C /usr/local/share
Create a symbolic link to the gcutil binary.
sudo ln -sf /usr/local/share/gcutil-1.16.1/gcutil /usr/local/bin/gcutil
Start using gcutil.
If you modified your system path, restart your shell before continuing so the changes can take effect. To see a list of available gcutil commands, run:
$ gcutil help
For additional information on authenticating with the Cloud SDK, such as if you are working in a terminal session on another machine, see the following section.
Authenticate gcutil
To authenticate gcutil, follow the instructions for gcloud compute.
Then, test out the tool by running:
$ gcutil listinstances
Once gcutil has been installed please proceed with the script now
Automated Backup Script
Create a script as /root/scripts/gsnapshots.sh ( make sure you do make it executable ) the Script is below . The parameter to be looked upon are
Mailboxes - email address to which alert mail should go
GCUTILPATH - the path of gcutils
project - this is google project name , it has to be go from Mr Banerjee
list - List of disk which need to be backup , run the command
gcutil --project="aimil-zimbra" listdisks --format=csv name,zone,status,size-gb disk-1,asia-east1-a,READY,500 instance-1,asia-east1-a,READY,20
so there are 2 disk , disk-1 and instance-1
days - No of days for rotation of snapshot backups
The script /root/scripts/gsnapshots.sh as follows
#!/bin/bash
# Google cloud snapshots script.
#list of mailboxes
mailboxes="biswajit@tetrain.com"
mailtext=/tmp/mailtext$$
# GCUTILPATH
GCUTILPATH=/usr/local/bin/
# Project name
project=aimil-zimbra
# What need to backup, list - disks from list, all - all disks.
backup="all"
#List of persistent disks
list="disk-1 instance-1"
# Rotation, how many days to keep
days=30
##############################################################################
log()
{
echo `date +%y/%m/%d_%H:%M:%S`:: $* >> $mailtext
echo `date +%y/%m/%d_%H:%M:%S`:: $*
}
case $backup in
"all" )
disklist=`${GCUTILPATH}gcutil --project="$project" listdisks --format=csv | tail -n +2 | cut -d ',' -f1`
;;
"list")
disklist=$list
;;
*)
log "Nothing to backup"
exit 0
;;
esac
function test {
"$@"
local status=$?
if [ $status -ne 0 ]; then
echo "error with $1"
mail -s "GOOGLE snapshot backup, ERROR. " $mailboxes < $mailtext
exit $status
fi
return $status
}
for i in $disklist
do
log "Starting snapshot for $i"
test ${GCUTILPATH}gcutil --service_version="v1" --project="$project" addsnapshot "$i""-`date +%y%m%d`" --source_disk="$i"
log "Snapshot for $i finished"
done
#Rotation
for i in $disklist
do
snapshot="$i""-`date +%y%m%d -d "$days day ago"`"
exist=`${GCUTILPATH}gcutil --project="$project" listsnapshots --format=csv | grep $snapshot`
if [ -z $exist ];then
log "Warning, snapshot $snapshot was not found"
else
log "Starting rotation for $i"
test ${GCUTILPATH}gcutil --project="$project" deletesnapshot -f $snapshot
log "Rotation for $i finished"
fi
done
log "All done."
test rm $mailtext