Skip to content

Instantly share code, notes, and snippets.

@vaibhav9392
Last active December 29, 2016 06:59
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 vaibhav9392/0dd7da29982ef31a3acf8e28967c3a6f to your computer and use it in GitHub Desktop.
Save vaibhav9392/0dd7da29982ef31a3acf8e28967c3a6f to your computer and use it in GitHub Desktop.
AWS EC2 automatic volume daily/weekly/monthly snapshots.
#!/usr/bin/php -q
<?php
date_default_timezone_set('UCT');
$dryrun = FALSE;
$volumes = array('');
$api_key = '';
$api_secret = '';
$ec2_region = 'us-east-1';
require 'aws/aws-autoloader.php';
use Aws\Ec2\Ec2Client;
$client = new Ec2Client(
array(
'credentials' => array(
'key' => $api_key,
'secret' => $api_secret
),
'version' => 'latest',
'region' => $ec2_region
)
);
/* * * * * * * * * * DAILY BACKUP START * * * * * * * * * */
$interval = '24 hours';
$keep_for = '3 Days';
$snap_descr = "Daily backup";
$db = json_decode(file_get_contents(__DIR__ . '/db_daily.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' => $volume.' - '.$snap_descr,
)
);
$db[] = array(
'volume' => $volume,
'time' => strtotime($result['StartTime']),
'id' => $result['SnapshotId']
);
}
file_put_contents(__DIR__ . '/db_daily.json', json_encode($db));
/* * * * * * * * * * DAILY BACKUP END * * * * * * * * * */
/* * * * * * * * * * WEEKLY BACKUP START * * * * * * * * * */
$interval = '7 Days';
$keep_for = '21 Days';
$snap_descr = "Weekly backup";
$db = json_decode(file_get_contents(__DIR__ . '/db_weekly.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' => $volume.' - '.$snap_descr,
)
);
$db[] = array(
'volume' => $volume,
'time' => strtotime($result['StartTime']),
'id' => $result['SnapshotId']
);
}
file_put_contents(__DIR__ . '/db_weekly.json', json_encode($db));
/* * * * * * * * * * WEEKLY BACKUP END * * * * * * * * * */
/* * * * * * * * * * MONTHLY BACKUP START * * * * * * * * * */
$interval = '1 Month';
$keep_for = '3 Months';
$snap_descr = "Monthly backup";
$db = json_decode(file_get_contents(__DIR__ . '/db_monthly.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' => $volume.' - '.$snap_descr,
)
);
$db[] = array(
'volume' => $volume,
'time' => strtotime($result['StartTime']),
'id' => $result['SnapshotId']
);
}
file_put_contents(__DIR__ . '/db_monthly.json', json_encode($db));
/* * * * * * * * * * MONTHLY BACKUP END * * * * * * * * * */
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment