Created
March 5, 2014 18:36
-
-
Save njadhav1/9373654 to your computer and use it in GitHub Desktop.
Create a snapshot of AWS EBS volume
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 | |
######## | |
# Purpose :- To Create Automatic snapshot of AWS EBS volume | |
# Requirement :- Make Sure snapshot.config file is present in /data/backup/ | |
# format for snapshot.config is VOLMEID00000000 | |
# For example | |
# vol-a994fff3 | |
# vol-a994fTT4 | |
# Provide your Access and Secrrt Key also check your Region | |
# Bug Report to :- jadhav.nitin941@gmail.com | |
######## | |
export JAVA_HOME=/usr/java/jdk1.7.0_04/ | |
export EC2_HOME=/usr/bin/ec2/ | |
export AWS_ACCESS_KEY=****************************** | |
export AWS_SECRET_KEY=****************************** | |
export EC2_URL=https://ec2.us-east-1.amazonaws.com | |
export PATH=/usr/bin/ec2/bin/:$EC2_HOME:$JAVA_HOME:$PATH | |
DATE=$(date +%F) | |
CONFIG_FILE="/data/backup/snapshot.config" | |
DAYS_COUNT=40 | |
BACKUP_DIR="/data/backup" | |
SNAPSHOT_DEL_DATE=$(date --date " ${DATE} ${DAYS_COUNT} days ago " +%F) | |
PROGNAME=$(basename $0) | |
BOX=$(uname -n) | |
LOGDIR="${BACKUP_DIR}/log/" | |
LOGFILE="SNAPSHOT_${DATE}.log" | |
LOCKFILE="/tmp/${PROGNAME}.lock" | |
#MAILTO="shajith.maniyath@ugamsolutions.com" | |
# Define custom variables | |
LOCK_FILE () | |
{ | |
if [ "$1" = "create" ]; then | |
if [ -f $LOCKFILE ]; then | |
SEND_EXIT_MAIL "ERROR ::" "Unable to create lock may lock file not removed" | |
else | |
touch $LOCKFILE | |
fi | |
fi | |
if [ "$1" = "remove" ]; then | |
rm -fr $LOCKFILE | |
exit 0 | |
fi | |
} | |
DEFINE_VARIABLE () | |
{ | |
#INSTANCE=$(echo $SNAPSHOT | cut -d"|" -f1) | |
VOLUME=$(echo $SNAPSHOT | cut -d"|" -f1) | |
SNAPSHOT_CREATION_DATE=$(date +%Y_%m_%d_%H) | |
} | |
# validation of custom variables | |
CHECK_VARIABLE () | |
{ | |
if [ -z "${VOLUME}" ];then | |
SEND_EXIT_MAIL "ERROR ::" "check your configration in snapshot.config Vaiables is blank" | |
exit | |
fi | |
if [ -z "${DISCRIPTION}" ];then | |
DISCRIPTION=$(echo "snap-${VOLUME}") | |
fi | |
} | |
SEND_EXIT_MAIL () | |
{ | |
mail -s "${BOX} :: ${PROGNAME} : $1 $2" -t $MAILTO < $LOGDIR/$LOGFILE | |
LOCK_FILE "remove" | |
exit 1 | |
} | |
SEND_MAIL () | |
{ | |
mail -s "${BOX} :: ${PROGNAME} : $1 $2" -t $MAILTO < $LOGDIR/$LOGFILE | |
} | |
LOCK_FILE "create" | |
touch $LOGDIR/$LOGFILE | |
echo "Script started at :- $(date)" 1>> $LOGDIR/$LOGFILE 2>> $LOGDIR/$LOGFILE | |
for dir in "${BACKUP_DIR}" "${LOGDIR}" | |
do | |
if [ ! -d "${dir}" ]; then | |
mkdir -p ${dir} 1>/dev/null 2>/dev/null | |
if [ $? -ne 0 ]; then | |
SEND_EXIT_MAIL "ERROR::" "Unable to Create ${dir} Directory :" | |
fi | |
fi | |
done | |
if [ ! -f "${CONFIG_FILE}" ]; then | |
SEND_EXIT_MAIL "ERROR::" " SNAPSHOT Config file is not Present :" | |
else | |
if [ ! -s "${CONFIG_FILE}" ]; then | |
SEND_EXIT_MAIL "ERROR ::" " SNAPSHOT Config file is ZERO byte" | |
fi | |
echo "SNAPSHOT CREATION STARTED\n" 1>> $LOGDIR/$LOGFILE 2>> $LOGDIR/$LOGFILE | |
# Snapshot Creation Logic starts | |
for SNAPSHOT in $(cat ${CONFIG_FILE}) | |
do | |
DEFINE_VARIABLE | |
CHECK_VARIABLE | |
ec2-create-snapshot ${VOLUME} --description ${DISCRIPTION}_${DATE} | awk '{print $2 "/\t" $3 "/\t" $4 "/\t" "/\t" $5}' | tr -d '/' 1>> $LOGDIR/$LOGFILE 2>> $LOGDIR/$LOGFILE | |
if [ $? -ne 0 ];then | |
SEND_MAIL "ERROR ::" "While Taking Snap of Volume ${VOLUME}" | |
fi | |
done | |
echo "SNAPSHOT CREATION ENDED" 1>> $LOGDIR/$LOGFILE 2>> $LOGDIR/$LOGFILE | |
echo "Script ended at :- $(date)" 1>> $LOGDIR/$LOGFILE 2>> $LOGDIR/$LOGFILE | |
SEND_MAIL "" "Snapshot script details " | |
LOCK_FILE "remove" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment