Skip to content

Instantly share code, notes, and snippets.

@pserrano
Forked from Serhioromano/backup.php
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pserrano/bd4e889af66e6d31c13c to your computer and use it in GitHub Desktop.
Save pserrano/bd4e889af66e6d31c13c to your computer and use it in GitHub Desktop.
Backup EC2 EBS automatic snapshot for all volumes if you want backup for all.

This is based of https://gist.github.com/Serhioromano/9738785 gist but my fork gist give more information of snapshot description of all machines than you want make snapshot it.

You need php-cli and php-curl installed on your system for execute all.

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/bd4e889af66e6d31c13c.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.7.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;
}
$volinfo = $client->describeVolumes(
array(
'VolumeIds' => array ($volume),
)
);
$description = $snap_descr . ': ' . $volume;
if (isset($volinfo['Volumes'][0]['Tags'][0]['Value'])){
$description .= ' | Machine: ' . $volinfo['Volumes'][0]['Tags'][0]['Value'];
}
$result = $client->createSnapshot(
array(
'DryRun' => $dryrun,
'VolumeId' => $volume,
'Description' => $description,
)
);
$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