Nov 14

AWS automated ebs snapshots

It is hard to take ebs backup manually each day as system administrator should log in to account daily. This bash script gives the power to automate that process. This automated ebs snapshots generating script reduce administrative task such it can defined how long backup should be kept on AWS account by configuring backup retention period, so Script automatically remove old backups for you. Also you add “cost center values” and “tag”  to the snapshot, this could be useful to identify it and calculate snapshot storage cost later.

Pre Requirements

1) Script is runs on Linux environment

you can keep small EC2 for this, if you don’t have one.

2) Install AWS CLI

please refer this http://docs.aws.amazon.com/cli/latest/userguide/installing.html

3) Configure IAM credentials

Please refer above doc. It will guide you how to do it properly.
** IAM should have permission to create and delete snapshots at least.

Below is full script with sample cron job. I have explained code in detail at the end.



Bash script explanation.

retention_days –> How long back should be kept on. Remember AWS will bill you for this. So keep retention days as short as possible.
logfile –> script output, kind of debug
volume_info –> EC2 volume ID are kept here for instance named “prod3”
snapshot_info –> Sanpshot ID are kept here for instance named “prod3”

instance_id –> unique ID of the EC2 instance.
os_vol,data_vol –> instance has 2 volume you can define any amount of volumes. example instance was named as “prod3”. so changed that as well.

os_cs_output –> text base (you can see command is append with “–output text” ) output result after creating snapshot. This is for the volume which contains OS. you have to create other volumes if there any. for example I create separate variable for data volume (data_cs_output). cs stands for “create snaphost” :)
os_snapshot_id –> grab the snapshot ID.
os_ct_output –> tag the snapshot with cost center value, we already have snapshot ID. This is extra step. you can skip If you wish. ct stands for “create tag”

Now time for delete old snapshots

Empty the volume_info file first. and then insert all volume ID to it.

Empty the snapshot_info file and insert all snapshot created for volumes mentioned on volume_info file.


I feels, I explain a lot in here. Do comment here if you have any doubt.


Leave Your Thought Here