Bash script for Automatic EBS Snapshots and Cleanup on Amazon Web Services

How it works: will:

  • Determine the instance ID of the EC2 server on which the script runs
  • Gather a list of all volume IDs attached to that instance
  • Take a snapshot of each attached volume
  • The script will then delete all associated snapshots taken by the script that are older than 7 days

Pull requests greatly welcomed!


IAM User: This script requires that new IAM user credentials be created, with the following IAM security policy attached:

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "Stmt1426256275000",
            "Effect": "Allow",
            "Action": [
            "Resource": [

After creating IAM user now you will make file with following content :

Install Script: Download the latest version of the snapshot script and make it executable:

cd ~
chmod +x
mkdir -p /opt/aws
sudo mv /opt/aws/

You should then setup a cron job in order to schedule a nightly backup. Example crontab jobs:

55 22 * * * root  AWS_CONFIG_FILE="/root/.aws/config" /opt/aws/

# Or written another way:
55 22 * * * root  /opt/aws/

To manually test the script:

sudo /opt/aws/

Note : You can make changes in script as per your requirement.

For eg.  If you want to set the back retention period to 3days then you have to change the value of



Blogger & Assc Cloud Architect

Site Footer

%d bloggers like this: