Jump to content

How-make-automatic-snapshots-using-google-compute-engine: Difference between revisions

From TetraWiki
No edit summary
No edit summary
Line 2: Line 2:




Referance Articles : http://www.smiss.com.ua/blog/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  
and https://cloud.google.com/compute/docs/gcutil/
https://cloud.google.com/compute/docs/gcutil/


Objective : how-make-automatic-snapshots-using-google-compute-engine
'''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  
Snapshots are used for Backup purpose . This document talks about how to create automatic backups i.e Snapshot Backup  


Process  
 
== Process ==


login to the google cloud compute engine and look for "gcutil"  via locate command  
login to the google cloud compute engine and look for "gcutil"  via locate command  
Line 16: Line 17:
If Not available then Install gcutil as below  
If Not available then Install gcutil as below  


Install gcutil
 
= Install gcutil =
 


To install gcutil:
To install gcutil:


Download gcutil-1.16.1.tar.gz file.
'''Download''' gcutil-1.16.1.tar.gz file.
 
  wget https://dl.google.com/dl/cloudsdk/release/artifacts/gcutil-1.16.1.tar.gz
  wget https://dl.google.com/dl/cloudsdk/release/artifacts/gcutil-1.16.1.tar.gz


Extract the files.
'''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.
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
  sudo tar xzvpf gcutil-1.16.1.tar.gz -C /usr/local/share


Create a symbolic link to the gcutil binary.
Create a symbolic link to the gcutil binary.
  sudo ln -sf /usr/local/share/gcutil-1.16.1/gcutil /usr/local/bin/gcutil
  sudo ln -sf /usr/local/share/gcutil-1.16.1/gcutil /usr/local/bin/gcutil


Start using 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:
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
  $ 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.
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
'''Authenticate gcutil'''


To authenticate gcutil, follow the instructions for gcloud compute.
To authenticate gcutil, follow the instructions for gcloud compute.
Line 42: Line 51:
Then, test out the tool by running:
Then, test out the tool by running:


$ gcutil listinstances
$ gcutil listinstances


Once gcutil has been installed please proceed with the script now  
Once gcutil has been installed please proceed with the script now  


Automated Backup Script
 
== 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  
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
'''Mailboxes''' - email address to which alert mail should go
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  
'''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
  gcutil --project="aimil-zimbra" listdisks --format=csv
  name,zone,status,size-gb
  name,zone,status,size-gb
Line 60: Line 76:
so there are 2 disk , disk-1 and instance-1  
so there are 2 disk , disk-1 and instance-1  


days - No of days for rotation of snapshot backups  
'''days''' - No of days for rotation of snapshot backups  


The script /root/scripts/gsnapshots.sh as follows  
The script /root/scripts/gsnapshots.sh as follows  

Revision as of 10:32, 7 November 2015


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