Once in a while I would go to http://aws.amazon.com/console and manually right click my EBS volume and create a snapshot.

This is kind of tedious and I would forget sometimes. Is it possible to automate this actions and say, make a daily or weekly snapshot?

I'm using a Windows Server instance.


Use AMI Tools:

  • install AMI Tools
  • create snapshot

As an alternative to command line tools you may use PHP script developed by me. It is easier to setup as you do not need write even a line of code and you do not need to setup environment.

Not only this script create snapshots automatically, it can also delete old snapshots after given period of time.

How to setup

  1. Open SSH connection to your server.
  2. Navigate to folder

    $ cd /usr/local/
    
  3. Clon this gist into ec2 folder

    $ git clone https://gist.github.com/9738785.git ec2
    
  4. Go to that folder

    $ cd ec2
    
  5. Make backup.php executable

    $ chmod +x backup.php
    
  6. Open releases of the AWS PHP SDK github project and copy URL of aws.zip button. Now download it into your server.

    $ wget https://github.com/aws/aws-sdk-php/releases/download/2.6.0/aws.zip
    
  7. Unzip this file into aws directory.

    $ unzip aws.zip -d aws 
    
  8. Edit backup.php php file and set all settings in line 5-12. Pay attention on your region.

    $dryrun     = FALSE;
    $interval   = '24 hours';
    $keep_for   = '10 Days';
    $volumes    = array('vol-********');
    $api_key    = '*********************';
    $api_secret = '****************************************';
    $ec2_region = 'us-east-1';
    $snap_descr = "Daily backup";
    
  9. Test it. Run this script

    $ ./backup.php
    

    Check if snapshot is created.

  10. If everything is ok just add cronjob. I run it everyday on 23:00. because i use interval 24 hours. If you setup shorter interval, consider to trigger cronjob more often. You can even set it every 5 minutes. It will create new snapshot only with given interval anyway.

    * 23 * * * /usr/local/ec2/backup.php