-
Notifications
You must be signed in to change notification settings - Fork 4
/
ec2-delete-old-snapshots.php
199 lines (150 loc) · 5.96 KB
/
ec2-delete-old-snapshots.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Modified from code by:
* Oren Solomianik’s
* http://orensol.com/2009/02/12/how-to-delete-those-old-ec2-ebs-snapshots/
* New region. ENV vars and no-op code from
* @author Erik Dasque
* @version 0.7
* @copyright Erik Dasque, 3 March, 2010
* @package default
**/
/**
* Define DocBlock
**/
// Your EC2 credentials
$AWS_ACCESS_KEY_ID = '';
$AWS_SECRET_ACCESS_KEY='';
define ('SYNTAX','Usage: php ec2-delete-old-snapshots.php -v vol-id [-v vol-id ...] -o days [--region region] [--noop]\n\n');
// uncomment the next line and comment the one after so the application is always in NO-OP mode (no delete)
//define("NOOP", "1");
define("NOOP", "0");
date_default_timezone_set('UTC');
if ((empty($AWS_ACCESS_KEY_ID) ) && (!(strlen(getenv('AWS_ACCESS_KEY_ID'))==0)))
{ $AWS_ACCESS_KEY_ID=getenv('AWS_ACCESS_KEY_ID'); }
if ((empty($AWS_SECRET_ACCESS_KEY) ) && (!(strlen(getenv('AWS_SECRET_ACCESS_KEY'))==0)))
{ $AWS_SECRET_ACCESS_KEY=getenv('AWS_SECRET_ACCESS_KEY'); }
// parse options (vol-ids, older-than)
$opts = getopt("v:o:",array("region:","noop"));
if (($opts['v']) && !is_array($opts['v']))
$volumes = array($opts['v']);
else
$volumes = $opts['v'];
// to Debug getopt
// var_dump($opts);
if (isset($opts['noop'])) $noop=true; else $noop=false;
if ($opts['region'])
{
if (is_array($opts['region']))
die("\n\nRegion cannot have more than one value\n".SYNTAX);
else
$region = $opts['region'];
}
else { echo "No region specified, defaulting to us-east-1\n"; $region = "us-east-1"; }
$ServiceURL = "https://".$region.".ec2.amazonaws.com";
if (!is_array($opts['o']))
{
$now = time();
$days = $opts['o'];
$older_than = $now - $days * 24 * 60 * 60;
}
if ((!$volumes) || (!$older_than))
die("\n\nDid not provide vol-id or older-than-time.\n".SYNTAX);
echo "\n";
foreach ($volumes as $volume)
{
echo "Will try to bulk delete for " . $volume ." in region ".$region." older than " . date("Y/m/d H:i:s", $older_than) . "\n";
}
echo "\n";
// include required EC2 library elements
require_once("Amazon/EC2/Client.php");
require_once("Amazon/EC2/Model/DescribeSnapshotsRequest.php");
require_once("Amazon/EC2/Model/DescribeVolumesRequest.php");
require_once("Amazon/EC2/Model/DeleteSnapshotRequest.php");
//$ec2Config = array ('ServiceURL' => 'https://us-east-1.ec2.amazonaws.com');
$ec2Config = array ('ServiceURL' => $ServiceURL);
// instantiate EC2 client
// was:
//$service = new Amazon_EC2_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
// with support for a different zone than US we do the following:
$service = new Amazon_EC2_Client($AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY, $ec2Config);
// Modify for Europe
// $ec2Config = array ('ServiceURL' => 'https://eu-west-1.ec2.amazonaws.com');
// $ec2 = new Amazon_EC2_Client($accessKeyId, $secretAccessKey, $ec2Config);
// for US-East:
// $ec2Config = array ('ServiceURL' => 'https://us-east-1.ec2.amazonaws.com');
// get all volumes
$request_v = new Amazon_EC2_Model_DescribeVolumesRequest();
try {
$response_v = $service->describeVolumes($request);
} catch (Amazon_EC2_Exception $e) {
echo 'Error: ' .$e->getMessage()."\n";
die ("\nExiting application\n\n");
}
$result_v = $response_v->getDescribeVolumesResult();
$volumes_found = $result_v->getVolume();
if (!is_array($volumes_found) || empty($volumes_found)) die ("No volumes found in region ".$region."\n\n");
$a_match=false;
foreach ($volumes_found as $volume_in_region)
{
foreach ($volumes as $volume_in_request)
{
$a_match = ($volume_in_request==$volume_in_region->getVolumeId());
// echo $volume_in_request." may match (".$a_match.") ".$volume_in_region->getVolumeId()."\n";
}
if ($a_match) break;
}
if (!$a_match) die ("None of these volumes were found in region ".$region."\n\n");
// get all snapshots
$request = new Amazon_EC2_Model_DescribeSnapshotsRequest();
$response = $service->describeSnapshots($request);
$result = $response->getDescribeSnapshotsResult();
$snaps = $result->getSnapshot();
if (is_array($snaps))
{
// first check we have at least 1 newer snapshot for every vol-id we got
// we don't want to delete all snapshots of a vol and be left with no snapshots,
// this guarantees it. so we build a "go_ahead_volumes" array.
foreach ($volumes as $volume)
{
foreach ($snaps as $snap)
{
$snapTimestamp = strtotime($snap->getStartTime());
$snapStatus = $snap->getStatus();
if (($snapTimestamp >= $older_than) && ($snapStatus=="completed"))
{
if ($snap->getVolumeId() == $volume)
{
$go_ahead_volumes[] = $volume;
echo "Ready for deletion of snapshots older than ".date("Y/m/d H:i:s e", $older_than). " for volume[".$volume."] in region ".$region;
echo ",\nfound newer snapshot [" . $snap->getSnapshotId() . "] taken on " . date('m/d/y \a\t H:i:s e',strtotime($snap->getStartTime())) . "\n";
break;
}
}
}
}
if (empty($go_ahead_volumes)) die ("No snapshots found for these volumes in region ".$region."\n\n");
echo "\n";
// now go over all snaps, if encounter a snap for a go_ahead_volume which
// is older than, well, older_than, delete it.
$dodelete = true;
if (NOOP) { $dodelete = false; echo "WARNING: the global NOOP is set in the source code for debbugging purpose so we won't be actually performing deletions\n\n"; }
else if ($noop) { $dodelete = false; echo "WARNING: not actually deleting, you specified --noop\n\n"; }
foreach ($snaps as $snap)
{
$snapTimestamp = strtotime($snap->getStartTime());
if ( (in_array($snap->getVolumeId(), $go_ahead_volumes)) && ($snapTimestamp < $older_than) )
{
echo "Deleting volume " . $snap->getVolumeId() . " snapshot " . $snap->getSnapshotId() . " created on: " . date('m/d/y \a\t H:i:s e',strtotime($snap->getStartTime())) ."\n";
// and now really delete using EC2 library
$request = new Amazon_EC2_Model_DeleteSnapshotRequest();
$request->setSnapshotId($snap->getSnapshotId());
if ($dodelete) $response = $service->deleteSnapshot($request);
}
}
echo "\n\n";
}
else
{
die ("\n\nNo snapshots found, quitting.\n\n");
}