| |
#!/usr/bin/perl
#=========
# WARNING: This program will delete files from your s3 bucket.
# Furthermore, it's designed to delete backups! If you are
# careful enough to make backups, and you care enough to place
# them into long term replicated storage, then be extra careful
# when deleting them. Make sure you know what this script is
# doing before using it. Don't just copy, paste, and run.
# You take full responsibility. Use at your own risk. Caveat emptor ...
#
# LICENSE: What license?
#
use POSIX;
$DRYRUN = 1;
# Be careful about turning dryrun off. It **WILL** delete your files.
$BUCKET = "your-bucket-name-here";
$DAYSTOKEEP = 7;
$WEEKSTOKEEP = 5;
$MONTHSTOKEEP = 0;
# 0 = keep monthly backups for all time
$KEEPWDAY = 0;
# Which day of week to keep around (0 = Sunday, 4 = Thursday)
@NOW = localtime(time());
$THINDAILY = mktime(-1,0,0,$NOW[3] - $DAYSTOKEEP,$NOW[4],$NOW[5]);
$THINWEEKLY = mktime(-1,0,0,$NOW[3] - 7*$WEEKSTOKEEP,$NOW[4],$NOW[5]);
$THINMONTHLY = mktime(-1,0,0,$NOW[3],$NOW[4] - $MONTHSTOKEEP,$NOW[5]);
open FILELIST, "s3cmd ls s3://$BUCKET |";
while (<FILELIST>) {
$_ = (split(/\s+/))[3]; # Filename is in 4th field
next if ! m/backup_(\d{4})(\d{2})(\d{2})_(\d+).tgz$/;
# Filename format must match "backup_00001122_334455.tgz"
# Other filenames are skipped. To keep an arbitrary backup
# forever, rename it to something that won't match:
# i.e. "backup_20100516_KEEPME.tgz"
$filetime = mktime(0,0,0,$3,$2-1,$1-1900);
next if ($filetime > $THINDAILY);
@T = localtime($filetime);
next if ($filetime > $THINWEEKLY and $T[6] == $KEEPWDAY);
$lastday = (localtime(mktime(0,0,0,0,$2,$1-1900)))[3]; # Day 0 of next month is last day of this month
next if ((!$MONTHSTOKEEP or $filetime > $THINMONTHLY) and $T[3] == $lastday);
$command = "s3cmd del $_";
if ($DRYRUN) {
print "$command\n";
} else {
system $command;
}
} |
|