Home > Amazon-AWS, HowTo > aws backup script

aws backup script

August 11th, 2015 Leave a comment Go to comments

This is a robust script that backups all instances that have a TAG, Backup=TRUE
it generates a AMI images, and snapshots of each Volume, also a nice e-mail with a HTML table nice format.

It also prints a BackupExpiration TAG, so a purge script can latter remove AMI and snapshots older then BackupExpiration. This way we keep only the last 10 days.

Here is the code:


#!/bin/bash
#
# Script que realiza o backup dos servidores virtuais na Amazon
#
# 2015-05-14 Salgadinho - original version
# 2015-06-23 Felipe Ferreira - simplify code and better output

#Pre-Req:
#INSTANCES AND VOLUMES WITH TAGs: Project,Name,Backup(TRUE/FALSE)
#ec2addtag command
#sendEmail.pl https://github.com/mogaal/sendemail put it on /usr/bin/sendEmail
#aws cli 1.7.X (minimal)

#Attention on the log directory structure



export AWS_SECRET_KEY=
export AWS_ACCESS_KEY=
export PATH=$PATH:/home/amzadm/ec2-api-tools/bin:$JAVA_HOME/bin:~/bin:$PATH

#day=$(date +%Y%m%d_%H%M%S)
day=$(date +%Y%m%d)
dayWeek=$(date "+%A")
dayMonth=$(date "+%d")

LST="/home/amzadm/scripts/lst"
LOG="/var/log/aws/createImage_$day.log"
CSV="/var/log/aws/informe.csv"
BODY_HTML="/var/log/aws/informe.html"
COUNT_V=0
COUNT_I=0

FROM="backup_aws@mydomain.com"
EMAILS="felipe.ferreira@mydomain.com"
schedule="Backup Daily"
# if need more then 10 days retention change bellow
backupExpiration=$(date +%Y%m%d -d "10 day")
TAG="Backup"
DEBUG=1
#set -x #if need more Debug


############################################################################################
pt () {
	if [ -z $DEBUG ];then
	 echo -e "$1" >> $LOG
	else
	  echo -e "$1" |tee -a $LOG
	fi
}

function sendemail() {
# ENVIA EMAIL COM INFORME

 echo "" > $BODY_HTML
 STYLE="
  "

 echo $STYLE >> $BODY_HTML
  echo "$BODY_HTML"


# echo "Total de "
 echo "A tabela e referente ao backup de hoje 
" >> $BODY_HTML #Cria HTML e coloca no body do email (echo "" ; while read INPUT ; do echo "" ; done < $CSV ; echo "
${INPUT//;/}
") >> $BODY_HTML echo -e "$ERR_LIST" >> $BODY_HTML echo "
" >> $BODY_HTML
 echo "
" >> $BODY_HTML echo "

Atenciosamente,
Infra-Básica
" >> $BODY_HTML # ENVIA O EMAIL HTML COM TABELA DE RESULTADOS VINDA DO INFOTBC1 echo "$BODY_HTML" CMD="/usr/bin/sendEmail -f "$FROM" -t "$EMAILS" -o message-content-type=html -u '$SUBJECT' -o message-file=${BODY_HTML} -a $LOG" echo $CMD $CMD echo $? if [ $? -eq 0 ];then echo "OK - Email enviado com sucesso a $EMAIL" E=0 else echo "ERROR - email a $EMAIL nao foi enviado corretamente" E=2 fi } ############################################################################################### ############### MAIN ################### #apaga arquivos if [ -f $LST ]; then rm $LST fi if [ -f $LOG ]; then rm -f $LOG fi #Criando lista de instancias para criacao de imagens LIST=`aws ec2 describe-instances --filter Name=tag:$TAG,Values=TRUE --query 'Reservations[*].Instances[*].InstanceId'` if [ -z $LIST ]; then echo "CRITICAL - lista vazia $LIST , nenhum backup a ser feito" #exit 2 fi pt "\n $(date +%Y%m%d_%H-%M-%S) -- Listando instancias para criacao de imagens \n" echo -e "\t Instance \t; Volume \t; Project " > $CSV #Gerando script de images for instance in $(echo $LIST); #cria imagem, nomeia e gera lista do instanceName=$(aws ec2 describe-instances --instance-ids $instance --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value') #CREATING IMAGE pt "Cirando imagem $instanceName da instancia $instance" exec_imageid='aws ec2 create-image --instance "'"$instance"'" --name "'"img_${instanceName}_${day}"'" --no-reboot --description "'"$schedule -- $instanceName"'"' #pt "Commando: $exec_imageid" imageid=`eval $exec_imageid` if [ -z $imageid ]; then pt "ERROR - $Exec_imageid nao foi possivel criar imagem do $instanceName" #exit 2 else pt "OK - imagem do $instanceName criada com sucesso, imagemID $imageid " sleep 2 #Nomeando a Imagem amiName=$(aws ec2 describe-images --image-ids $imageid --query 'Images[*].[Name]') pt "amiName = $amiName" if [ -z $amiName ]; then pt "ERROR - Nao foi possivel encontrar a $imageid!" #exit 2 fi exec_tag='ec2addtag "'"$imageid"'" -t Name="'"$amiName"'" -t Schedule="'"$schedule"'" -t BackupExpiration="'"$backupExpiration"'"' tag=`eval $exec_tag` #pt "Commando: $exec_tag \n $tag" #Gerando Lista de SnapIds e salvando no arquivo exec_snaplst="aws ec2 describe-images --image-ids $imageid --region us-east-1 --output text --query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId' | sed -e "'"y/\t/\n/"'" >> $LST/createSnapid_${day}.lst" #pt "Comando: $exec_snaplst" snaplst=`eval $exec_snaplst` pt $snaplst COUNT_I=`expr $COUNT_I + 1` fi done if [ -f "$LST/createSnapid_${day}.lst" ]; then #Nomeando snapshots da imagem for snapid in $(cat $LST/createSnapid_$day.lst); do volume=$(aws ec2 describe-snapshots --snapshot-id $snapid --query 'Snapshots[*].VolumeId') partition=snap_$(aws ec2 describe-volumes --volume-ids $volume --query 'Volumes[].Tags[?Key==`Name`].Value') project=$(aws ec2 describe-volumes --volume-ids $volume --query 'Volumes[].Tags[?Key==`Project`].Value') pt "Tageando snapshot $snapid - Volume $volume - Name $partition - project $project" exec_tag='ec2addtag "'"$snapid"'" -t Name="'"$partition"'" -t Project="'"$project"'"' tag=`eval $exec_tag` #pt "Commando: $exec_tag \n $tag" P=`echo $partition|awk -F"_" '{ print $NF}'` N=`echo $partition|awk -F"_" '{ print $2}'` echo -e "\t $N \t; $P \t; $project " >> $CSV COUNT_V=`expr $COUNT_V + 1` done pt "\n---------------------------------------------------------------------------------------------------\n" else pt "ERROR - arquivo com lsita de imagens inexistente $LST/createSnapid_$day.lst , AMI criado mas snapshots nao" fi rm -f "$LST/createSnapid_${day}.lst" #CHECK LOG FOR ANY ERROR sed -i 's/ERROR/\ERROR/g' $LOG ERR_LIST=`grep ERROR $LOG` pt "\n $(date +%Y%m%d_%H-%M-%S) -- Backup realizado com sucesso de $COUNT_I imagens e $COUNT_V volumes \n" if [ $COUNT_I -eq 0 ] || [ $COUNT_V -eq 0 ]; then SUBJECT="Backup AWS FALHOU" sendemail exit 2 fi SUBJECT="Backup AWS $COUNT_I instancias e $COUNT_V Volumes" sendemail exit 0
  1. June 16th, 2016 at 06:10 | #1

    Thank you very much ! You have cleared out the difference between them.

  2. August 31st, 2017 at 11:59 | #2

    Nice article, Thanks for sharing about the AWS Back up Script.keep on posting those type of articles.

  1. No trackbacks yet.

VAMOVE *

*