forked from alestic/ec2-expire-snapshots
/
ec2-expire-snapshots
executable file
·1215 lines (902 loc) · 39.4 KB
/
ec2-expire-snapshots
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# Copyright (C) 2009-2012 Eric Hammond <ehammond@thinksome.com>
#
use strict;
use warnings;
(our $Prog) = ($0 =~ m%([^/]+)$%);
use Getopt::Long;
use Pod::Usage;
use Date::Manip;
use DateTime;
use DateTime::Format::ISO8601;
use File::Slurp;
use Net::Amazon::EC2;
#---- OPTIONS ----
my $Help = 0;
my $Debug = 0;
my $EC2Debug = 0;
my $Quiet = 0;
my $Noaction = 0;
my $aws_access_key_id = $ENV{AWS_ACCESS_KEY_ID};
my $aws_secret_access_key = $ENV{AWS_SECRET_ACCESS_KEY};
my $aws_access_key_id_file = $ENV{AWS_ACCESS_KEY_ID};
my $aws_secret_access_key_file = $ENV{AWS_SECRET_ACCESS_KEY};
my $aws_credentials_file = $ENV{AWS_CREDENTIALS};
my $use_iam_role = 0;
my $region = undef;
my $ec2_endpoint = undef;
my $volume_id_in_tag = undef;
my $delete_delay = undef;
my $force_delete_all = undef;
my $keep_first_yearly = undef;
my $keep_first_quarterly = undef;
my $keep_first_monthly = undef;
my $keep_first_weekly = undef;
my $week_starts = 'Sun';
my $keep_first_daily = undef;
my $keep_first_hourly = undef;
my $keep_most_recent = undef;
my $keep_all_since = undef;
my $expiration_tag_name = undef;
my $expiration_tag_optional = undef;
Getopt::Long::config('no_ignore_case');
GetOptions(
'h|help|?' => \$Help,
'd|debug' => \$Debug,
'q|quiet' => \$Quiet,
'n|noaction' => \$Noaction,
'ec2debug' => \$EC2Debug,
'aws-access-key-id=s' => \$aws_access_key_id,
'aws-secret-access-key=s' => \$aws_secret_access_key,
'aws-access-key-id-file=s' => \$aws_access_key_id_file,
'aws-secret-access-key-file=s' => \$aws_secret_access_key_file,
'aws-credentials-file=s' => \$aws_credentials_file,
'use-iam-role' => \$use_iam_role,
'region=s' => \$region,
'volume-id-in-tag=s' => \$volume_id_in_tag,
'delete-delay=i' => \$delete_delay,
'force-delete-all' => \$force_delete_all,
'keep-first-yearly=s' => \$keep_first_yearly,
'keep-first-monthly=s' => \$keep_first_monthly,
'keep-first-weekly=s' => \$keep_first_weekly,
'keep-first-daily=s' => \$keep_first_daily,
'keep-first-hourly=s' => \$keep_first_hourly,
'keep-most-recent=i' => \$keep_most_recent,
'keep-all-since=s' => \$keep_all_since,
'expiration-tag-name=s' => \$expiration_tag_name,
'expiration-tag-optional' => \$expiration_tag_optional,
) or pod2usage(2);
my $filesystem_frozen = 0;
pod2usage(1) if $Help;
my @volume_ids = @ARGV;
pod2usage(2) unless scalar @volume_ids;
$ec2_endpoint ||= "https://ec2.$region.amazonaws.com" if $region;
my $constraints = {
force_delete_all => $force_delete_all,
keep_first_yearly => $keep_first_yearly,
keep_first_quarterly => $keep_first_quarterly,
keep_first_monthly => $keep_first_monthly,
keep_first_weekly => $keep_first_weekly,
week_starts => $week_starts,
keep_first_daily => $keep_first_daily,
keep_first_hourly => $keep_first_hourly,
keep_most_recent => $keep_most_recent,
keep_all_since => $keep_all_since,
expiration_tag_name => $expiration_tag_name,
expiration_tag_optional => $expiration_tag_optional,
};
#---- MAIN ----
($aws_access_key_id, $aws_secret_access_key) = determine_access_keys(
$aws_access_key_id, $aws_secret_access_key,
$aws_access_key_id_file, $aws_secret_access_key_file,
$aws_credentials_file,
);
die "$Prog: ERROR: Can't find AWS access key or secret access key"
unless $use_iam_role or ($aws_access_key_id and $aws_secret_access_key);
$Debug and warn "$Prog: Using AWS access key: $aws_access_key_id\n";
my $ec2 = Net::Amazon::EC2->new(
((! $use_iam_role) ? (AWSAccessKeyId => $aws_access_key_id,) : () ),
((! $use_iam_role) ? (SecretAccessKey => $aws_secret_access_key) : () ),
($ec2_endpoint ? (base_url => $ec2_endpoint) : ()),
# ($Debug ? (debug => 1) : ()),
);
my $snapshots_for_volumes = snapshots_for_volumes($ec2,$volume_id_in_tag,@volume_ids);
for my $volume_id ( @volume_ids ) {
expire_snapshots_for_volume($ec2, $volume_id,
$snapshots_for_volumes->{$volume_id},
$constraints);
}
exit 0;
#---- METHODS ----
# Figure out which AWS credentials to use
sub determine_access_keys {
my ($aws_access_key_id, $aws_secret_access_key,
$aws_access_key_id_file, $aws_secret_access_key_file,
$aws_credentials_file,
) = @_;
# 1. --aws-access-key-id and --aws-secret-access-key
return ($aws_access_key_id, $aws_secret_access_key)
if $aws_access_key_id;
# 2. --aws-access-key-id-file and --aws-secret-access-key-file
if ( $aws_access_key_id_file ) {
die "$Prog: Please provide both --aws-access-key-id-file and --aws-secret-access-key-file"
unless $aws_secret_access_key_file;
$aws_access_key_id = File::Slurp::read_file($aws_access_key_id_file);
$aws_secret_access_key= File::Slurp::read_file($aws_secret_access_key_file);
chomp($aws_access_key_id);
chomp($aws_secret_access_key);
return ($aws_access_key_id, $aws_secret_access_key);
}
# 3. $AWS_CREDENTIALS or $HOME/.awssecret
return read_awssecret($aws_credentials_file);
}
# Look for the access keys in $AWS_CREDENTIALS or ~/.awssecret
sub read_awssecret {
my ($aws_credentials_file) = @_;
$aws_credentials_file ||= "$ENV{HOME}/.awssecret";
my ($aws_access_key_id, $aws_secret_access_key);
eval {
($aws_access_key_id, $aws_secret_access_key) =
File::Slurp::read_file($aws_credentials_file);
chomp $aws_access_key_id;
chomp $aws_secret_access_key;
};
return ($aws_access_key_id, $aws_secret_access_key);
}
# Return hash of { volume_id => [ snapshot, snapshot, ... ], ... }
sub snapshots_for_volumes {
my ($ec2,$volume_id_in_tag,@volume_ids) = @_;
my @filters;
$volume_id_in_tag and push @filters, ['tag-key', $volume_id_in_tag];
@volume_ids and push @filters, ['volume-id', @volume_ids];
$Debug and warn "$Prog: Retrieving snapshot list\n";
my $snapshots;
eval {
$snapshots = $ec2->describe_snapshots((@filters ? (Filter => \@filters) : ()));
};
if ( $@ ){
die "$Prog: ERROR: describe_snapshots: ", ec2_error_message($@);
}
my %snapshots_for_volumes;
foreach my $snapshot (@$snapshots) {
push @{$snapshots_for_volumes{$snapshot->volume_id}}, $snapshot;
}
return \%snapshots_for_volumes;
}
# Do the expiration for a single volume
sub expire_snapshots_for_volume {
my ($ec2, $volume_id, $snapshots, $constraints) = @_;
if ( not defined $snapshots ) {
warn "$Prog: WARNING: No snapshots found for volume: $volume_id\n";
return undef;
}
$Debug and warn "$Prog: Processing volume $volume_id\n";
my $good_snapshots = filter_snapshots($volume_id, $snapshots);
my $candidate_list = build_candidate_list($good_snapshots);
my $snapshots_to_delete =
expired_for_constraints($volume_id, $candidate_list, $constraints);
my $count_to_delete = scalar(@$snapshots_to_delete);
my $count_to_keep = scalar(@$snapshots) - $count_to_delete;
$Debug and warn "$Prog: $volume_id: Keeping $count_to_keep snapshot",
($count_to_keep == 1 ? "" : "s"),".",
" Deleting $count_to_delete snapshot",
($count_to_delete == 1 ? "" : "s"),"\n";
my $index = 0;
for my $snapshot ( @$snapshots_to_delete ) {
if ( $index > 0 && $delete_delay ) {
$Quiet or warn "$Prog: Sleeping $delete_delay second/s between deletes\n";
sleep $delete_delay;
}
my $snapshot_id = $snapshot->{snapshot_id};
$Quiet or warn "$Prog: $volume_id: Deleting snapshot: $snapshot_id (",
$snapshot->start_time, ")\n";
if ( !$Noaction ) {
eval {
$ec2->delete_snapshot(SnapshotId => $snapshot_id)
or die "$Prog: ERROR deleting snapshot: $snapshot_id\n";
};
if ( $@ ){
die "$Prog: ERROR: delete_snapshot $snapshot_id: ",
ec2_error_message($@);
}
}
$index++;
}
}
# Filter out uncompleted snapshots
sub filter_snapshots {
my ($volume_id, $snapshots) = @_;
my $completed_snapshots = [];
for my $snapshot ( @$snapshots ) {
if ( $snapshot->status eq 'completed' ) {
push @$completed_snapshots, $snapshot;
} else {
$Quiet or
warn "$Prog: $volume_id: Status '", $snapshot->status,
"': Keeping ", $snapshot->snapshot_id, "\n";
}
}
return $completed_snapshots;
}
# Convert snapshot list into expected candidate list format
sub build_candidate_list {
my ($snapshots) = @_;
my $candidate_list = [];
for my $snapshot ( @$snapshots ) {
push @$candidate_list, {
timestamp => $snapshot->start_time,
id => $snapshot->snapshot_id,
tags => 'TBD',
object => $snapshot,
};
}
return $candidate_list;
}
#
# Determine which items have expired given a list of keep constraints
#
# Input candidate list should be array of hashes:
#
# [ { timestamp => $timestamp, id => $id, object => $object }, ... ]
#
# where:
#
# timestamp - can be decoded with DateTime
# id - some meaningful string for debugging
# object - opaque object meaningful to caller
#
# This method returns an array of the resulting objects that should
# be expired.
#
sub expired_for_constraints {
my ($group_name, $unsorted_candidate_list, $constraints) = @_;
my $candidate_list_datetime = add_datetime($group_name,
$unsorted_candidate_list);
my $candidate_list = [sort { $a->{datetime} <=> $b->{datetime} }
@$candidate_list_datetime];
#TBD: Die with error unless some keep/tag/delete option was specified.
return force_delete_all($group_name, $candidate_list, $constraints)
if $constraints->{force_delete_all};
my @keep_list = ();
push @keep_list, keep_all_since($group_name, $candidate_list,
$constraints->{keep_all_since});
push @keep_list, keep_first_yearly($group_name, $candidate_list,
$constraints->{keep_first_yearly});
push @keep_list, keep_first_quarterly($group_name, $candidate_list,
$constraints->{keep_first_quarterly});
push @keep_list, keep_first_monthly($group_name, $candidate_list,
$constraints->{keep_first_monthly});
push @keep_list, keep_first_weekly($group_name, $candidate_list,
$constraints->{keep_first_weekly},
$constraints->{week_start});
push @keep_list, keep_first_daily($group_name, $candidate_list,
$constraints->{keep_first_daily});
push @keep_list, keep_first_hourly($group_name, $candidate_list,
$constraints->{keep_first_hourly});
push @keep_list, keep_most_recent($group_name, $candidate_list,
$constraints->{keep_most_recent});
#TBD: Implement expiration_tag_name
die "$Prog: ERROR: Option 'expiration_tag_name' is not yet implemented"
if $constraints->{expiration_tag_name};
#TBD: Implement expiration_tag_optional
die "$Prog: ERROR: Option 'expiration_tag_optional' is not yet implemented"
if $constraints->{expiration_tag_optional};
my %keep_ids = map { $_->{id} => 1 } @keep_list;
my $expire_list = [];
for my $item ( @$candidate_list ) {
push @$expire_list, $item->{object}
unless exists $keep_ids{$item->{id}};
}
return $expire_list;
}
sub force_delete_all {
my ($group_name, $candidate_list, $constraints) = @_;
for my $key ( keys %$constraints ) {
die "$Prog: ERROR: Option 'force-delete-all' not compatible with".
" any 'keep' options"
if $key =~ m%^(keep|expiration_tag)% && $constraints->{$key};
}
$Quiet or warn "$Prog: $group_name: INFO: Expiring ALL by request";
my $expired_list = [];
for my $item ( @$candidate_list ) {
push @$expired_list, $item->{object};
}
return $expired_list;
}
sub keep_all_since {
my ($group_name, $candidate_list, $timestamp) = @_;
return () unless defined $timestamp;
#TBD: Implement keep_all_since
die "$Prog: ERROR: Option 'keep_all_since' is not yet implemented";
}
sub keep_first_yearly {
my ($group_name, $candidate_list, $count_to_keep) = @_;
return keep_first_period($group_name, $candidate_list, $count_to_keep,
'year', 'years', "%Y");
}
sub keep_first_quarterly {
my ($group_name, $candidate_list, $count_to_keep) = @_;
return () unless defined $count_to_keep;
#TBD: Implement keep_first_quarterly
die "$Prog: ERROR: Option 'keep_first_quarterly' is not yet implemented";
}
sub keep_first_monthly {
my ($group_name, $candidate_list, $count_to_keep) = @_;
return keep_first_period($group_name, $candidate_list, $count_to_keep,
'month', 'months', "%Y-%m");
}
sub keep_first_weekly {
my ($group_name, $candidate_list, $count_to_keep, $week_starts) = @_;
return keep_first_period($group_name, $candidate_list, $count_to_keep,
'week', 'weeks', "%Y-%m-%d");
}
sub keep_first_daily {
my ($group_name, $candidate_list, $count_to_keep) = @_;
return keep_first_period($group_name, $candidate_list, $count_to_keep,
'day', 'days', "%Y-%m-%d");
}
sub keep_first_hourly {
my ($group_name, $candidate_list, $count_to_keep) = @_;
return keep_first_period($group_name, $candidate_list, $count_to_keep,
'hour', 'hours', "%Y-%m-%d %H:00");
}
# Abstract out hour/day/week/month/quarter/year
sub keep_first_period {
my ($group_name, $candidate_list, $count_to_keep,
$period, $periods, $human_format) = @_;
return () unless $count_to_keep;
my $keep_all = ($count_to_keep =~ m%^all$%i) ? 1 : 0;
die "$Prog: ERROR: $periods value must be numeric or 'all'"
unless $keep_all || $count_to_keep =~ m%^\d+$%;
my $start_time = DateTime->now(time_zone => 'UTC')->truncate(to => $period);
die "$Prog: ERROR: Unable to calculate current $period"
unless $start_time;
my $end_time = undef;
my @keep_list = ();
PERIOD:
while ( $keep_all || $count_to_keep-- > 0 ) {
last PERIOD
if $keep_all && $end_time &&
$end_time < $candidate_list->[0]->{datetime};
my $human_start_time = $start_time->strftime($human_format);
for my $item ( @$candidate_list ) {
my $lead = "$Prog: $group_name: \u$period $human_start_time";
if ( defined($end_time) && $item->{datetime} >= $end_time ) {
$Quiet or warn "$lead: No candidates to keep\n";
next PERIOD;
}
if ( $item->{datetime} >= $start_time ) {
$Debug and
warn "$lead: Keeping ", $item->{id}, "\n";
push @keep_list, $item;
next PERIOD;
}
}
} continue {
$end_time = $start_time->clone;
$start_time->subtract($periods => 1);
}
return @keep_list;
}
sub keep_most_recent {
my ($group_name, $candidate_list, $count_to_keep) = @_;
$count_to_keep = 1 unless defined $count_to_keep;
my $from_index = $#$candidate_list - $count_to_keep + 1;
$from_index = 0 if $from_index < 0;
my $to_index = $#$candidate_list;
return () if $from_index > $to_index;
my @keep_list = ();
for (my $count = 1; $count <= $count_to_keep; ++$count) {
my $lead = "$Prog: $group_name: Most recent $count";
if ( $count > scalar @$candidate_list ) {
$Quiet or warn "$lead: No candidates to keep\n";
} else {
my $item = $candidate_list->[scalar(@$candidate_list) - $count];
$Debug and warn "$lead: Keeping ", $item->{id}, "\n";
push @keep_list, $item;
}
}
return @keep_list;
}
# Support error formats from different versions of Net::Amazon::EC2
sub ec2_error_message {
my ($error) = @_;
if ( ref $error && ref $error->errors eq 'ARRAY' ) {
$error = join("\n", map {$_->code.': '.$_->message} @{$error->errors});
}
return $error;
}
# Add the DateTime to each entry for easy manipulation
sub add_datetime {
my ($group_name, $candidate_list) = @_;
my $list_with_datetime = [];
for my $item ( @$candidate_list ) {
my $datetime = datetime_for_timestamp($item->{timestamp});
die "$Prog: $group_name: ERROR: Unable to parse timestamp for ",
$item->{id}, ": ", $item->{timestamp}
unless $datetime;
push @$list_with_datetime, {
%$item,
datetime => $datetime,
};
}
return $list_with_datetime;
}
# Convert ISO8601 format into a DateTime object.
sub datetime_for_timestamp {
my ($timestamp) = @_;
my $dt = DateTime::Format::ISO8601->parse_datetime($timestamp);
$dt->set_time_zone('UTC');
return $dt;
}
=head1 NAME
ec2-expire-snapshots - Delete expired EBS snapshots in Amazon EC2
=head1 SYNOPSIS
ec2-expire-snapshots [opts] VOLUMEID...
=head1 OPTIONS
=over
=item C<-h> C<--help>
Print help and exit.
=item C<-d> C<--debug>
Debug mode.
=item C<-q> C<--quiet>
Quiet mode.
=item C<-n> C<--noaction>
Don't do it. Just say what you would have done.
=item C<--aws-access-key-id KEY>
=item C<--aws-secret-access-key SECRET>
Amazon AWS access key and secret access key. Defaults to
environment variables or .awssecret file contents described below.
=item C<--aws-access-key-id-file KEYFILE>
=item C<--aws-secret-access-key-file SECRETFILE>
Files containing Amazon AWS access key and secret access key.
Defaults to environment variables or .awssecret file contents
described below.
=item C<--aws-credentials-file CREDENTIALSFILE>
File containing both the Amazon AWS access key and secret access
key on separate lines and in that order. Defaults to contents of
$AWS_CREDENTIALS environment variable or the value $HOME/.awssecret
=item --use-iam-role
The instance is part of an IAM role that that has permission to create
snapshots so there is no need to specify access key or secret.
=item C<--region REGION>
Specify a different EC2 region like "eu-west-1". Defaults to
"us-east-1".
=item C<--volume-id-in-tag TAGNAME>
Specifies the name of a tag to look for on each EBS snapshot
indicating what volume-id to associate with this snapshot,
replacing the volume-id that is returned by the API. The common
use is for when snapshots are copied across regions. This is so
a consistent expiration schedule can be kept, without regards to
the new volume-id generated each time a copy is made.
=item C<--delete-delay DELAY_IN_SECONDS>
Specifies the number of seconds to wait between deleting
snapshots. Allows ec2-expire-snapshots to run without hitting
the AWS rate limiter.
=item C<--keep-most-recent COUNT>
=item C<--keep-all-since DATETIME> [NOT YET IMPLEMENTED]
=item C<--keep-first-yearly YEARCOUNT>
=item C<--keep-first-quarterly QUARTERCOUNT> [NOT YET IMPLEMENTED]
=item C<--keep-first-monthly MONTHCOUNT>
=item C<--keep-first-weekly WEEKCOUNT>
=item C<--week-starts DAYOFWEEK> [NOT YET IMPLEMENTED]
=item C<--keep-first-daily DAYCOUNT>
=item C<--keep-first-hourly HOURCOUNT>
=item C<--expiration-tag-name TAGNAME> [NOT YET IMPLEMENTED]
=item C<--expiration-tag-optional> [NOT YET IMPLEMENTED]
These options identify which EBS snapshots should be preserved. See
the "PRESERVATION OPTIONS" section for more details on what they mean
and how to use them.
=item C<--force-delete-all>
This dangerous option overrides all other default option values, EBS
snapshot tag values, and normal safety measures. All EBS snapshots
for the specified EBS volumes are attemped to be deleted. Even the
most recent EBS snapshot is deleted. The EBS snapshots are deleted
even if the EBS volume does not exist. After this option is used, you
will have no EBS snapshots for the specified EBS volume, unless there
was some error condition that prevented an EBS snapshot from being
deleted.
This is not a normal EBS snapshot expiration strategy, but is a
convenience option for use when throwing away all data associated with
an EBS volume that is no longer useful for any purpose.
=back
=head1 ARGUMENTS
=over
=item VOLUMEID
EBS volume ids for which EBS snapshots are to be expired (deleted).
The EBS volume does not have to exist for its EBS snapshots to be
found and deleted.
=back
=head1 INSTALLATION
On Ubuntu, the B<ec2-expire-snapshots> package can be installed
directly from the Alestic.com PPA using the following commands:
sudo add-apt-repository -y ppa:alestic
sudo apt-get update
sudo apt-get install -y ec2-expire-snapshots
=head1 DESCRIPTION
This program deletes expired EBS snapshots of the specified EBS
volumes on Amazon EC2. Different expiration strategies and
specifications are supported.
When deciding what options to use, it's easier to think of which EBS
snapshots should be *preserved* instead of which should be expired and
deleted.
As a general rule, all EBS snapshots that you have not requested to be
preserved will be deleted.
Unless overridden, the software always preserves the most recent EBS
snapshot, along with any EBS snapshots where it isn't clear what was
intended (e.g., unrecognized expiration tag values).
Please read the descriptions of the preservation options carefully.
They might not mean what they look like on first glance. For example,
these mean two very different things:
=over
=item C<--keep-all-since '3 days ago'> [NOT YET IMPLEMENTED]
This option asks the program to preserve *every* EBS snapshot that was
created since the current time 3 days ago, i.e., the most recent 72
hours.
=item C<--keep-first-daily 3>
This option asks the program to preserve the first completed EBS
snapshot that was created on each of the last 3 calendar days
including "today", "yesterday", and "2 days ago", calculated in UTC.
All other EBS snapshots of the EBS volume may be deleted unless there
are other conditions specified to prevent that.
=back
=head2 PRESERVATION OPTIONS
The following options control which EBS snapshots are preserved. If
an EBS snapshot is not flagged as one to be preserved, then it is
considered expired and will be deleted.
There are three basic expiration methodologies supported by this
program.
=over
=item 1. Keep Recent
This is the simplest approach where you tell the program to preserve a
specifc number of the the most recent EBS snapshots created (e.g.,
"10") and/or to preserve any EBS snapshots created since a particular
date/time in the past (e.g., "7 days ago").
The specific options related to this approach include:
=over
=item C<--keep-most-recent COUNT>
The most recent COUNT completed EBS snapshots will be preserved. By
default, the most recent EBS snapshot is always preserved, but this
can be overridden against our recommendation by explicitly specifying
"0" in this option.
=item C<--keep-all-since DATETIME> [NOT YET IMPLEMENTED]
The EBS snapshots that were created at or after DATETIME will be
preserved. The value may be an absolute date/time or it may be
relative to now.
See "DATE/TIME FORMATS" below for recommended value formats.
=back
=item 2. Keep One Per Calendar/Clock Cycle
This approach lets you keep many very recent copies of EBS snapshots,
somewhat fewer as you go into the near-term past, and more sparse
backups as you get to distant history. The time frames involved are
hourly, daily, weekly, monthly, quarterly, and yearly. You can
specify as many or as few of these time periods as you wish, and save
as many cycles of each as you wish.
The specific options related to this approach include:
=over
=item C<--keep-first-hourly HOURCOUNT>
=item C<--keep-first-daily DAYCOUNT>
=item C<--keep-first-weekly WEEKCOUNT>
=item C<--keep-first-monthly MONTHCOUNT>
=item C<--keep-first-quarterly QUARTERCOUNT> [NOT YET IMPLEMENTED]
=item C<--keep-first-yearly YEARCOUNT>
Preserve the first completed EBS snapshot created in each of the most
recent COUNT hours/days/weeks/months/quarters/years, respectively,
calculated in UTC. The value "all" can be used instead of a number if
you wish to keep the first snapshot available in any available period.
The current hour/day/week/month/quarter/year is considered "1". A
COUNT of "all", case insensitive, is equivalent to an infinitely high
COUNT.
A single EBS snasphot may match multiple options. For example, the
first EBS snapshot in a month is also going to be the first EBS
snapshot for a particular day, but perhaps not the first for any
particular week. There may be no completed EBS snapshots in some time
periods.
The first completed EBS snapshot in a month may have been taken on a
date later than the first day of the month and the dates don't need to
be the same across different months. For example, the first snapshot
of the month may have been created on the 3rd. This software simply
preserves the oldest completed EBS snapshot in each relevant time
period.
If hourly EBS snapshots would be too many for you, you can simply not
create EBS snapshots that often. For example, you could create EBS
snapshots every 6 hours and then specify C<--keep-first-hourly 30> to
preserve about 4, depending on what exactly the time stamps are on the
EBS snapshots.
=item C<--week-starts DAYOFWEEK> [NOT YET IMPLEMENTED]
Specifies the first day of each week as you think of it. This is used
in conjunction with C<--keep-first-weekly> to know which EBS snapshot you
prefer to keep. Supported values include "Sunday", "Sun", "Monday",
"Mon", case insensitively. The default first day of the week is Monday.
=back
=item 3. Ask The Snapshot
This approach transfers the decision making about how long each EBS
snapshot should be preserved onto some other process, perhaps the one
that creates the EBS snapshots in the first place. That external
process must create or add a tag to each EBS snapshot indicating how
long it should be preserved or when it should expire.
This gives maximum flexibility for any system that goes beyond the
simple rules understood by this program. It also allows for
exceptions to be made to these rules by humans who make decisions
about how long a specific EBS snapshot should be preserved.
The specific options related to this approach include:
=over
=item C<--expiration-tag-name TAGNAME> [NOT YET IMPLEMENTED]
Specifies the name of a tag to look for on each EBS snapshot
indicating when that EBS snapshot is allowed to expire. The tag value
can either be an absolute date/time, or a date/time offset expression
that is to be calculated relative to the timestamp of the EBS
snapshot's creation. For example, Expiration: +8 days
If the calculated expiration time is in the future or is unrecognized,
then the EBS volume is preserved. A tag value of "never" or "forever"
will prevent an EBS snapshot from ever being expired.
If this option is specified multiple times, then each tag name is
checked on each EBS snapshot, and any one of them can trigger the
preservation of an EBS snapshot (even if another tag indicates that
the snapshot has expired).
See "DATE/TIME FORMATS" below for recommended tag value formats.
=item C<--expiration-tag-optional> [NOT YET IMPLEMENTED]
By default, if you specify one or more C<--expiration-tag-name> options
and an EBS snapshot does not have any of those tag names, then that
EBS snapshot will be preserved.
Including the C<--expiration-tag-optional> option tells the program that
EBS snapshots without the expiration tags are allowed to be expired.
=back
=back
It is acceptable and encouraged to include options from across
multiple of these strategies. This program will preserve all EBS
snapshots that match any of the conditions.
For example, you may want to keep 24 hourly, 7 daily, 4 weekly, and 12
monthly EBS snapshots as a base strategy.
Then, you could add the last 3 hours of all EBS snapshots just in case
you end up creating some EBS snapshots manually during a sensitive
file modification procedure and you don't want your EBS snapshots
inadvertently deleted in the event you need to undo some recent work.
Then, you could add in some optional expiration tags that can be set
to override normal expiration and preserve for a longer time period
any special EBS snapshots that you care to flag from time to time.
=head2 BACKGROUND
The creation of EBS snapshots on EC2 is a risk reduction and safety
improvement measure in a few ways:
=over
=item 1
An EBS snapshot is a form of backup and disaster recovery
preparedness, allowing you to restore data that may have been deleted,
corrupted, or in any other way lost on the EBS volume due to hardware,
system, environmental, or human error at a time after the EBS snapshot
was created.
=item 2
EBS snapshots are available from multiple availability zones in a
given EC2 region even if the availability zone for the source EBS
volume is inaccessible.
=item 3
An EBS snapshot automatically and transparently reduces the rate of
failure of the underlying EBS volume, due to the way that Amazon has
designed and implemented the EBS system.
The EBS system can automatically recover parts of a failing EBS volume
from an EBS snapshot, if the blocks that are failing have not been
modified since the EBS snapshot was taken. The more frequently an EBS
volume is snapshotted, the lower its potential rate of failure.
=back
With backup strategies using physical media, we normally introduce a
backup rotation strategy because we have a limited amount of disk or
tape to store the backups. With EC2, the available EBS snapshot space
is perceived as limitless at the level a single organizion could use
it.
So, why do we want to expire and delete EBS snapshots?
=over
=item 1
EBS snapshots cost money to store on EC2. Even though multiple EBS
snapshots share the same copies of unchanged blocks, and even though
the block contents are stored in a compressed format, these charges
can add up over time when you have a lot of EBS snapshots.
=item 2
There is a limit on the number of EBS snapshots a single EC2 account
can have at any point in time. This limit can easily be increased by
submitting a request to Amazon with an explanation of why you need
more, but eventually you're probably going to want to trim back on how
many EBS snapshots you retain.
=item 3
Depending on the tools you are using to manage your AWS resources, it
can get unwieldy to manage large numbers of EBS snapshots.
=back
This program tries to help manage your EBS snapshot storage costs by
deleting EBS snapshots that you believe are the least helpful to your
backup needs, while preserving a select set of EBS snapshots that you
believe are likely to be the most important in the near and distant
future.
Just remember: When you (or this software) delete an EBS snapshot, it
is gone forever and is completely irrecoverable!
=head2 SAFETY
This software attempts to be somewhat conservative and to protect you
in a few ways:
=over
=item 1
This program will exit with error unless you specify at least one of
the preservation options to give this program an indication of what
your expiration strategy is and what EBS snapshots should be
preserved.
=item 2
If you use the C<--expiration-tag-name> option and this program cannot
make sense of the value for that tag on one of the EBS snapshots, then
that EBS snapshot is not deleted.
=item 3
If you specify C<--expiration-tag-name> and C<--expiration-tag-optional>
without any of the C<--keep> options, and the tag name(s) are not found
on any EBS snapshots, then the program exits with an error instead of
deleting all your EBS snapshots.
=item 4
The most recent EBS snapshot for an EBS volume is always preserved
unless you explicitly request it to be deleted with:
--keep-most-recent 0
Deleting the most recent EBS snapshot reduces the reliability of the
EBS volume and increases the time, IO, and cost required to create a
new EBS snapshot in the future.
=item 5
If any of the options indicate to this program that an EBS snapshot
should be preserved, then it will not be deleted. For example, even
if there is an expiration tag on an EBS snapshot that indicates it should
expire yesterday, it will not be deleted if it is also the first
EBS snapshot of this month and you specified C<--keep-first-monthly> greater
than zero.
=item 6
EBS snapshots in the "pending" or other non-"completed" states are
completely ignored by this program. For example, only "completed"
snapshots are counted when deciding what is the first snapshot of a
calendar period to preserve.
We have no guarantee that a "pending" snapshot will ever complete
successfully, but if it does complete before the next time you run
this program, then it will be taken into account in the calculations
and may become the new "first" EBS snapshot to be preserved for a time
period.
=back