Skip to content

Instantly share code, notes, and snippets.

@jayadevn
Forked from Serhioromano/backup.php
Created March 23, 2016 10:22
Show Gist options
  • Save jayadevn/588f8b44f017ae7d50d6 to your computer and use it in GitHub Desktop.
Save jayadevn/588f8b44f017ae7d50d6 to your computer and use it in GitHub Desktop.
AWS EC2 automatic volume daily snapshot.

How to create daily HDD/EBS snapshot of you AWS EC2 instance

It looks like a very required and trivial task. But there is not outof the box solution in AWS. But fortunately it is easy to setup with few steps.

How to setup

  1. Opent SSH connection to your server.

  2. Navigate to folder

     $ cd /usr/local/
    
  3. Clon this gist

     $ 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

     $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
    

    Test is snapshot was created.

  10. If everything is ok just add cronjob.

    * 23 * * * /usr/local/ec2/backup.php
    
#!/usr/bin/php -q
<?php
date_default_timezone_set('UCT');
$dryrun = FALSE;
$interval = '24 hours';
$keep_for = '10 Days';
$volumes = array('vol-********');
$api_key = 'AKIAIXXXXXXXXXXXXXXX';
$api_secret = 'IzMni.........................emQKct';
$ec2_region = 'us-east-1';
$snap_descr = "Daily backup";
require 'aws/aws-autoloader.php';
use Aws\Ec2\Ec2Client;
$client = Ec2Client::factory(
array(
'key' => $api_key,
'secret' => $api_secret,
'region' => $ec2_region
)
);
$db = json_decode(file_get_contents(__DIR__.'/db.json'), TRUE);
$snapshots = array();
foreach($db AS $key => $snapshot)
{
if(!empty($snapshots[$snapshot['volume']]))
{
if($snapshot['time'] > $snapshots[$snapshot['volume']]['time'])
{
$snapshots[$snapshot['volume']] = $snapshot;
}
}
else
{
$snapshots[$snapshot['volume']] = $snapshot;
}
if($snapshot['time'] < strtotime('- ' . $keep_for))
{
$client->deleteSnapshot(
array(
'DryRun' => $dryrun,
'SnapshotId' => $snapshot['id'],
)
);
unset($db[$key]);
}
}
foreach($volumes As $volume)
{
if((!empty($snapshots[$volume])) && ($snapshots[$volume]['time'] > strtotime('-' . $interval)))
{
continue;
}
$result = $client->createSnapshot(
array(
'DryRun' => $dryrun,
'VolumeId' => $volume,
'Description' => $snap_descr,
)
);
$db[] = array(
'volume' => $volume,
'time' => strtotime($result['StartTime']),
'id' => $result['SnapshotId']
);
}
file_put_contents(__DIR__.'/db.json', json_encode($db));
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment