I have an HPUX system that I runs a backup command via a perl maintenance script I wrote every week via cron. It creates a backup file in the /tmp directory. I want to only have 3 weeks worth of backups in the directory (all there is room for, all I need) so currently every week after it runs, I manually delete the oldest backup file. I want my maintenance script to do this for me. My problem is an easy way to determine the oldest file to unlink it.
Here is my code so far:
#!/opt/perl/bin/perl -w
use strict;
use File::stat;
use Compress::Zlib;
use vars qw($dirname @allfiles $file $day $month $year $created $stat $temp $time $junk $junk2);
$dirname = "/OV_backup"; # This is a directory where data can be found
opendir(DIR,"$dirname"
;
@allfiles = readdir DIR;
foreach $file (@allfiles) {
next unless ($file =~ /^OV_BACKUP_\d+/);
$stat = stat("$dirname/$file"
or die "Can't stat $file: $!";
$created = $stat->mtime;
$time = localtime($created);
($junk,$month,$day,$junk2,$year) = split(/\s+/,$time);
print "$month\n$day\n$year\n";
}
My problem is how do I do a "<" ">" and account for the times when the oldest file is at the end of the month and therefore the day is greater than the newer ones? I am thinking about somehow creating an array of hashes and referencing it that way but:
1) I am not that good at perl
2) I think there may be an easier way.
Thanks so much
Here is my code so far:
#!/opt/perl/bin/perl -w
use strict;
use File::stat;
use Compress::Zlib;
use vars qw($dirname @allfiles $file $day $month $year $created $stat $temp $time $junk $junk2);
$dirname = "/OV_backup"; # This is a directory where data can be found
opendir(DIR,"$dirname"
@allfiles = readdir DIR;
foreach $file (@allfiles) {
next unless ($file =~ /^OV_BACKUP_\d+/);
$stat = stat("$dirname/$file"
$created = $stat->mtime;
$time = localtime($created);
($junk,$month,$day,$junk2,$year) = split(/\s+/,$time);
print "$month\n$day\n$year\n";
}
My problem is how do I do a "<" ">" and account for the times when the oldest file is at the end of the month and therefore the day is greater than the newer ones? I am thinking about somehow creating an array of hashes and referencing it that way but:
1) I am not that good at perl
2) I think there may be an easier way.
Thanks so much