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

How it works: automatic-snapshot-script.sh 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!

REQUIREMENTS

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": [
                "ec2:CreateSnapshot",
                "ec2:CreateTags",
                "ec2:DeleteSnapshot",
                "ec2:DescribeSnapshots",
                "ec2:DescribeVolumes"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

After creating IAM user now you will make ebs-snapshot.sh file with following content :

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

cd ~
wget https://github.com/bhargavamin/aws-automatic-ebs-snapshots/blob/master/automatic-snapshot-script.sh
chmod +x automatic-snapshot-script.sh
mkdir -p /opt/aws
sudo mv automatic-snapshot-script.sh /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/automatic-snapshot-script.sh

# Or written another way:
AWS_CONFIG_FILE="/root/.aws/config" 
55 22 * * * root  /opt/aws/automatic-snapshot-script.sh

To manually test the script:

sudo /opt/aws/automatic-snapshot-script.sh

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

retention_days="3"

 

Blogger & Assc Cloud Architect

Site Footer