EBS Volume Snapshots and Delete obsolete Backups
Posted By : Tarun Singhal | 28-Jan-2018
EBS Volume Snapshots and Delete obsolete Backups.
Here is the bash script which can help us to automate the regular snapshot of our AWS
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
- 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
-
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.