Monthly Archives: March 2015

Amazon AWS EC2 Automatic Snapshot Daily Cron Job

There seems to be very poor documentation about setting up an automatic backup, daily snapshot with Amazon AWS EC2. I am currently running Ubuntu 14.04 with ISPConfig. Here is how to setup a daily cron job for backup.

First, you must get your Access Key ID and Secret Access Key from Amazon. There is plenty of documentation about this. You may want to setup an IAM user so this access key has limited permissions, as Amazon recommends. I’ve linked to the Amazon support page if you are not familiar with this. This is so that Amazon knows you are authorized to make this request.

Second, log in to your AWS console and click on ‘Volumes’ under the EBS (elastic block storage) section. Make note of your Volume-ID, which you will need so that Amazon knows which volume to backup.

Next, you must install the Amazon command line tools. All of the sources should already be setup for you, if you used an Amazon image. Just execute the following code from the command shell and install.

sudo apt-get install ec2-api-tools

Here is the command used for your cron job:

/usr/bin/ec2-create-snapshot -O aws-secret-key -W aws-access-key-id  volume-id -d "Daily Backup $(date +'%m/%d/%y')"

For a daily backup, use the hour and minutes that you want the job to run. Use a * for days-of-month, months, and days-of-week. This means it will run at a specific time of day, but every day.

Breakdown:

  • -O AWS Secret Access Key
  • -W AWS Access Key ID
  • -d Snapshot Name (in our case, it will be Daily Backup mm/dd/yy). Of course, you can change this as needed.

ISPConfig Configuration:

First, check to make sure your permissions are setup for Reseller and Client to allow for you to create cron jobs. Also, you will want to setup the maximum number of cron jobs (-1 is unlimited) and the type of cron job to ‘Full Cron’. To setup a cron job with ISPConfig, you must link to a .sh file. Below, replace ‘clientXX’ and ‘webXX’ with your actual client number.

Create a directory in your client folder called ‘cron’. This should be located at: /var/www/clients/clientXX/cron/

cd /var/www/clients/clientXX
mkdir cron

Inside, create a file called daily-snapshot.sh.

vi cron/daily-snapshot.sh

This file should include the following:

#!/bin/bash

# content of your script

/usr/bin/ec2-create-snapshot -O aws-secret-key -W aws-access-key-id  volume-id -d "Daily Backup $(date +'%m/%d/%y')"

Also, you much execute the following commands to give the correct permissions to this file:

chown -R webXX:clientXX /var/www/clients/clientXX/cron/
chmod 755 /var/www/clients/clientXX/cron/daily-snapshot.sh

Finally, you are now ready to create your cron job in ISPConfig. Go to the ‘Cron Jobs’ section under the sites tab. Create a new cron job. Select your site. For a daily backup, use the hour and minutes that you want the job to run. Use a * for days-of-month, months, and days-of-week. This means it will run at a specific time of day, but every day. Enter the following for the command to run:

/var/www/clients/clientXX/cron/daily-snapshot.sh

Links: