Last active
October 19, 2016 09:23
-
-
Save avtehnik/09a30b7ae7a1441c94d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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