Skip to content

Instantly share code, notes, and snippets.

@avtehnik
Last active October 19, 2016 09:23
Show Gist options
  • Save avtehnik/09a30b7ae7a1441c94d4 to your computer and use it in GitHub Desktop.
Save avtehnik/09a30b7ae7a1441c94d4 to your computer and use it in GitHub Desktop.
<?php
$volumeId = 'vol-id';
$daysLimit = 3;
$ownerId = '0123456789101';
$tag = false; // tag name or false
$date = date('Y-m-d H:i:s');
$resultJson = `aws ec2 create-snapshot --volume-id $volumeId --description "site.appshed.com $date"`;
if($tag){
$snapshotInfo = json_decode($resultJson);
$snapshotId = $snapshotInfo->SnapshotId;
`/usr/local/bin/aws ec2 create-tags --resources $snapshotId --tags Key=Name,Value="$tag"`;
}
$snapshotsList = `aws ec2 describe-snapshots --owner-ids $ownerId`;
$snapshots = json_decode($snapshotsList);
foreach ($snapshots->Snapshots as $snapshot) {
if ($snapshot->VolumeId == $volumeId) {
$startDate = new DateTime($snapshot->StartTime);
$interval = $startDate->diff(new DateTime());
echo $interval->format('%R%a %h:%i:%S') . PHP_EOL;
if ($interval->format('%a') >= $daysLimit) {
echo "Delete {$snapshot->SnapshotId}" . PHP_EOL;
$deleteRequest = `aws ec2 delete-snapshot --snapshot-id {$snapshot->SnapshotId}`;
echo $deleteRequest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment