#!/usr/bin/perl # Copyright (c) 2004,2005 Scalable Informatics LLC # this is open source software covered under the GPL 2.0 license. use strict; use IO::Dir; use Getopt::Long; use Date::Manip; my ($dir,$rc,$age,$cwd,%d,$today,$olddate,$delta,$dt,$name,@delta_t); ### defaults $age = 30; # 30 days, approximately 1 month $dir = `pwd`; # defaults to current directory chomp($dir); $cwd = $dir; $rc = GetOptions ( "age=i" => \$age, "dir=s" => \$dir ); die "FATAL ERROR: must give a realistic value for age of backups to delete\n" if (($age <= 0) || ($age >= 10240)); die "FATAL ERROR: directory =\'".$dir."\' does not exist\n" if (!(-e $dir)); die "FATAL ERROR: \'".$dir."\' is not a directory\n" if (!(-d $dir)); die "FATAL ERROR: Unable to chdir to directory=\'".$dir."\'\n" if (!(chdir $dir)); $today = ParseDate("today"); print "\n"; tie %d, 'IO::Dir', "."; foreach (keys %d) { $name=$_; next if (($_ eq "." ) || ($_ eq "..")); if ($_ =~ /(\w+)\.(\d)\.(\d+)\..*/) { printf "name = %-40s\t",$name; #printf "path = %s, level = %i, date stamp = %s\t",$1,$2,$3; $olddate = ParseDate($3); $delta=DateCalc($olddate,$today); $dt=Delta_Format($delta,0,qw(%dt)); #$dt=join("_",@delta_t); printf "age = %s day(s)\n",$dt; if ($dt > $age) { printf "DELETION: backup file \'%s\' is %s day(s) old (maximum age=%s day(s)). Deleting \n", $name,$dt,$age; $rc = `/bin/rm -f $name`; if (-e $name) { warn "DANGER: remove of file \'".$name."\' failed!!!\n"; } } } }