VoicePulse Blog

Automate backing up your AWS instances (i.e. volumes) using this utility

#!/bin/bash

# Copyright (c) 2017 Ketan Patel, VoicePulse Inc
# REQUIREMENTS:
# - Intended to run on an Amazon Linux AMI
# - Tested with Amazon Linux AMI 2017.03.1.20170623 x86_64 HVM GP2
# - Requires AWS CLI to be installed and configured
# - Requires mailx client: sudo yum install mailx
#
# RELEASE NOTES:
# 2017-08-04 Version 1.0
# - Discover all volumes in a region
# - Find associated instance information
# - Create a snapshot and tag with a unique run ID
# - Send a summary email of snapshot progress & log file attachment
# - Allow for a dry run
#
# Thanks to the N2WS guide at https://n2ws.com/how-to-guides/automate-amazon-ec2-instance-backup.html for a starting point to creating this utility.
#
# DISCLAIMER OF WARRANTY
# THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

# Check for permissions
if [ `id -u` -ne 0 ]; then
  echo "Please run as root: sudo ./ec2_volume_backup.sh"
  exit 1
fi

# Check for arguments
if [ $# -eq 0 ]; then
  echo "Syntax: sudo ./ec2_volume_backup.sh "
  echo "Example: sudo ./ec2_volume_backup.sh us-east-1"
  exit 1
fi

# Configuration
DRY_RUN=1 # Change to 0 to actually create snapshots
SCRIPT_NAME="EC2 Volume Snapper"
VOLUMES_LIST=/var/log/volumes_info
SNAP_CREATION=/var/log/snap_creation
SNAP_SUMMARY=/var/log/snap_summary

# Initialize variables
DATE=`date +%Y%m%d%H%M%S`
REGION=$1
EMAIL_SUBJECT="${SCRIPT_NAME} ${REGION} ${DATE} DRY_RUN=${DRY_RUN}"
EMAIL_LIST=user@user.net

echo "Snapshots Creation Status" > $SNAP_CREATION
echo "Snapshots Creation Status" > $SNAP_SUMMARY

# Extract list of volumes to a list
aws ec2 describe-volumes --region $REGION --query Volumes[*].[Attachments[0].InstanceId,VolumeId] --output text &> $VOLUMES_LIST

if [ -f $VOLUMES_LIST ]; then

# Creating Snapshot for each volume using for loop
while read VOL_INFO; do
  # Getting the Volume InstanceId and VolumeId into the Separate Variables.
  VOL_INSTANCE=`echo $VOL_INFO | awk '{print $1}'`
  VOL_ID=`echo $VOL_INFO | awk '{print $2}'`

  # Creating the Snapshot of the Volumes with Proper Description.
  DESCRIPTION="${VOL_INSTANCE}_${VOL_ID}_${DATE} Automated backup by ${SCRIPT_NAME}"
  echo "aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION --query 'SnapshotId' --output text &>> $SNAP_CREATION" &>> $SNAP_CREATION
  if [ $DRY_RUN -eq 0 ]; then
    aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION --query 'SnapshotId' --output text &>> $SNAP_CREATION
    sleep 3
    SNAP_ID=`aws ec2 describe-snapshots --region $REGION --query Snapshots[*].[SnapshotId] --filters "Name=description,Values=$DESCRIPTION" --output text`
    echo "aws ec2 create-tags --resources $SNAP_ID --tags Key=EC2VolumeSnapperRunID,Value=$DATE --region $REGION &>> $SNAP_CREATION" &>> $SNAP_CREATION
    aws ec2 create-tags --resources $SNAP_ID --tags Key=EC2VolumeSnapperRunID,Value=$DATE --region $REGION &>> $SNAP_CREATION
  else
    echo "** DRY RUN ** NO SNAPSHOT CREATED **"
    echo "** DRY RUN ** NO SNAPSHOT CREATED **" &>> $SNAP_SUMMARY
  fi
  echo $DESCRIPTION
done < $VOLUMES_LIST

aws ec2 describe-snapshots --region $REGION --query Snapshots[*].[SnapshotId,State,Progress,Description] --filters "Name=tag-value,Values=$DATE" --output table &>> $SNAP_SUMMARY

# If no volumes were found
else
echo "No volumes found. Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
exit 1
fi

echo >> $SNAP_CREATION
echo >> $SNAP_CREATION

# Send email
mailx -s "$EMAIL_SUBJECT" -a /var/log/snap_creation $EMAIL_LIST < /var/log/snap_summary

Topics: aws