Deletes all ROOT VOLUME snapshots except retain most recent

#!/bin/bash

### Deletes all ROOT VOLUME snapshots except $RETAIN most recent. Script is designed to be run from inside

### instance on which it is operating. Depends on EC2 API Tools being pre-installed and configured.

 

### How many snapshots do we want to retain?

RETAIN=3

 

### Obtain instance ID

INSTANCE=`ec2-metadata -i |sed -n 's/^[a-z-]*: \(i-[0-9a-f]*\).*$/\1/p'`

 

### Obtain region

REGION=`ec2-metadata -z |sed -n 's/^[a-z]*: \([^0-9]*-[0-9]\).*$/\1/p'`

 

### Obtain root volume ID

VOLUME=`ec2-describe-instances ${INSTANCE} --region ${REGION} \

        |sed -n 's/^.*\(vol-[0-9a-f]*\).*$/\1/p' |head -1`

 

### Create a one-dimensional array consisting of date-sorted snapshots

declare -a Snaps=(`ec2-describe-snapshots --region ${REGION} \

        |grep -v "CreateImage" \

        |grep -i ${VOLUME} \

        |awk '{print $2, $5}' \

        |sort -k 2 -r \

        |cut -d " " -f 1`);

 

array_len=${#Snaps[@]}

last_snap=$(($array_len -1))

 

for i in `seq 0 ${last_snap}`

do

        if [[ $i>$(($RETAIN -1)) ]]; then

                ec2-delete-snapshot --region ${REGION} ${Snaps[$i]};

        fi

done

Script to delete all snapshots for the all instances, across all regions - except the 3 most recent:

#!/bin/bash

# RCSID: $Id: cull_snapshots_allregions.sh,v 1.1 2012/07/14 10:58:46 wadu Exp $

 

### How many snapshots do we want to retain?

RETAIN=3

 

### get a list of regions

REGIONS=$(ec2-describe-regions | awk '{ print $2; }')

 

### Loop through the regions, get the instances, get the volumes, get the snapshots, delete any snapshots apart from the youngest three

for REGION in $REGIONS;

do

    echo ""

    echo "REGION: $REGION"

 

    ### Loop through instances

    INSTANCES=$(ec2-describe-instances --region $REGION | awk '{ print $2; }' | grep '^i-[0-9a-f]*')

    for INSTANCE in $INSTANCES;

         do

                  echo "--INSTANCE: $INSTANCE"

 

                  ### Get the volume info and work on deleting unneeded snapshots

                  VOLUME=`ec2-describe-instances ${INSTANCE} --region ${REGION} |sed -n 's/^.*\(vol-[0-9a-f]*\).*$/\1/p' |head -1`

                  echo "----VOLUME: $VOLUME"

                  ### Get a list of snapshots for that volume

                  SNAPSHOTS=$(ec2-describe-snapshots --region ${REGION} \

                                  |grep -v "CreateImage" \

                                  |grep -i ${VOLUME} |sort -k 5 -r |awk '{print $2}');

                        #ec2-describe-snapshots --region eu-west-1 |grep -v "CreateImage" |grep -i vol-cf90f3a7|sort -k 5 -r|awk '{print $2}'

 

                 ### Loop through the snapshots for the volume we are in

                 COUNTER=1

                 for SNAPSHOT in $SNAPSHOTS;

                        do

                                ###If the total number of snapshots for a volume is greater than 3, start deleting

                                if [ $COUNTER -gt $RETAIN ]; then

                                        ec2-delete-snapshot --region ${REGION} $SNAPSHOT > /dev/null 2>&1

                                         if [ $? -ne 0 ] ; then

                                                 echo "------!! FAILURE DELETING SNAPSHOT: $SNAPSHOT" ;

                                           else

                                                 echo "------DELETING SNAPSHOT: $SNAPSHOT" ;

                                         fi

                                fi

                                (( COUNTER++ ))

                        done

 

          done

 

done

Comments

Popular posts from this blog

Push command for NetWorker Client Upgrade

Factors Affecting Users’ Sense of Security and Trust in Integrating Electronic health record to the cloud

Basic Elastic Cloud Storage Troubleshooting Commands