EBS Volume Snapshots and Delete obsolete Backups

Posted By : Tarun Singhal | 28-Jan-2018

EBS Volume Snapshots and Delete obsolete Backups.

N|Solid

Here is the bash script which can help us to automate the regular snapshot of our AWS ebs volumes on a periodic order and delete the obsolete one also in an automated manner.

This script has some dependencies:

Installation Steps:

apt-get install jq
pip install awscli
You need to install dateUtils (http://www.fresse.org/dateutils/)

Now, Configure Aws command line utility

N|Solid

  • Enter aws Access key and Hit enter
  • Now Enter Aws Secret Access Key and Hit Enter
  • Enter Region name and hit enter
  • For output format, remain blank and Hit Enter because by default it uses JSON

Bash Function for taking ebs snapshots

function backup_ebs () {
    for volume in $(aws ec2 describe-volumes | jq .Volumes[].VolumeId | sed 's/\"//g' )
    do 
        echo Creating snapshot for $volume $(aws ec2 create-snapshot --volume-id $volume --description "backup-script" )
    done
}

Bash Function for deleting snapshots

function delete_snapshot () {

    if  [ -z $1 ] || ! [[ "$1" =~ ^[0-9]+$ ]];
    then
        echo "Please enter the age of backups you want to delete and it must be integer"
        exit 1
    else
        for snapshot in $(aws ec2 describe-snapshots --filters Name=description,Values=backup-script | jq .Snapshots[].SnapshotId | sed 's/\"//g')
        do
            SNAPSHOT_DATE=$(aws ec2 describe-snapshots --filters Name=snapshot-id,Values=$snapshot | jq .Snapshots[].StartTime | cut -d T -f1 | sed 's/\"//g' )
            START_DATE=$(date +"%Y-%m-%d")
            INTERVAL=$(datediff $SNAPSHOT_DATE $START_DATE)
            if (($INTERVAL > $ROTATION_PERIOD));
                then
                    echo "deleting snapshot $snapshot "
                    aws ec2 delete-snapshot --snapshot-id $snapshot
            fi
        done
    fi
}

How to use.

  • To backup ebs volume.

./ec2_backup_script.sh backup

This will snapshot all the ebs volumes present in a particular region.

  • To Delete obsolete Snapshots.

./ec2_backup_script.sh delete 5

This command helps us to delete the snapshots.
Here to delete, we need two arguments first is delete argument to tell script we need to delete snapshots and second is x (int value) to delete snapshots older than x days
Here we are deleting all snapshots older than 5 days.

Setup Cron

You can also setup this script as a cron for daily backup and delete obsolete backups according to your need.

I will add more features to this script to make it more helpful.
Hope this will help you in any way.

About Author

Author Image
Tarun Singhal

Tarun is a RedHat Certified System Administrator. He is very keen to learn new technologies. He has good command over tools like Ansible, Gitlab-CI etc.

Request for Proposal

Name is required

Comment is required

Sending message..