how to purge backup files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hello All,

I have structure like below


When the third backup arrive in the respective directories, i need to
delete the first old backup dir i.e
if dira/backup-051505 arrives, need to delete dira/backup-050105. same
requirement for all others direcotries i.e. dirb, dirc...dirf too.

I am kind a lost in finalizing the approach to acheive this. I tried to
use linux find command or perl File::Find to extract the first old
directory by using -mtime for 21 days but did not work because files
underneath those directory are with original timestamp and they are
over 21 days old.

Any help will be highly appreciable.

I just need to know that what is the best way to find out the first old
directory name, so that i can remove it.

Appreciate it.

Re: how to purge backup files wrote:
Quoted text here. Click to load it

I'd do it like this (untested!):

my $dir = 'dira';
my @backups = ();
for (<$dir/backup-??????>) {
     next unless m|.*/backup-(\d)|;
     push @backups, $1;
unless (@backups < 3) {
     @backups = sort { $a <=> $b } @backups;
     while (@backups >= 3) {
    unlink "$dir/backup-" . shift @backups;

Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
                        -- T.  Pratchett

Re: how to purge backup files

Quoted text here. Click to load it

How would you do this manually?

Would you look at the filenames or the timestamps?

Quoted text here. Click to load it

If the timestamps are not accurate, then perhaps you should be looking
for filenames instead...

Quoted text here. Click to load it

   my $dir = 'dira';  # untested
   my $oldest = (glob "$dir/backup-*")[0];
   print "oldest file is '$oldest'\n";

    Tad McClellan                          SGML consulting                   Perl programming
    Fort Worth, Texas

Re: how to purge backup files wrote:
Quoted text here. Click to load it

I will assume that your file names use the form backup-MMDDYY

my @files =
     map $_->[ 0 ],
     sort { $a->[ 1 ] cmp $b->[ 1 ] }
     map {
         my ( $path, $month, $day, $year ) = /^(.+backup-)(\d\d)(\d\d)(\d\d)$/;
         [ $_, "$path$year$month$day" ]

my %by_dir;
for ( @files ) {
     my ( $dir, $file ) = m!(.+)/(.+)!;
     push @{ $by_dir{ $dir } }, $file;

for my $dir ( keys %by_dir ) {
     next if @{ $by_dir{ $dir } } < 3;
     unlink "$dir/$by_dir[0]"
         or warn "Cannot delete $dir/$by_dir[0]: $!";

Of course if your file names already used the form backup-YYMMDD it would be
much simpler.

use Perl;

Re: how to purge backup files

John W. Krahn wrote:

Quoted text here. Click to load it

If sanjeev.sagar was using a more rational naming convention,
the code could be made much more simple.

linux% mkdir -p dir/backup-2005-05-
linux% perl
print "Oldest for $_ is ",oldest_name($_),"\n" for glob "dir*";
sub oldest_name { (glob "$_[0]/backup-*")[0]; }
Oldest for dira is dira/backup-2005-05-01
Oldest for dirb is dirb/backup-2005-05-01
Oldest for dirf is dirf/backup-2005-05-01

When using yyyy in front, mm in the middle, and dd at the end,
alphabetical order == chronological order.  Can you change
the process that creates the backups, Sanjeev?


Site Timeline