how to purge backup files

S

sanjeev.sagar

Hello All,

I have structure like below

dira/backup-050105
dira/backup-050805
dirb/backup-050105
dirb/backup-050805
..
..
..
dirf/backup-050105
dirf/backup-050805

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.
 
J

Josef Moellers

Hello All,

I have structure like below

dira/backup-050105
dira/backup-050805
dirb/backup-050105
dirb/backup-050805
.
.
.
dirf/backup-050105
dirf/backup-050805

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.

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

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

Josef
 
T

Tad McClellan

I have structure like below

dira/backup-050105
dira/backup-050805
dirb/backup-050105
dirb/backup-050805

When the third backup arrive in the respective directories, i need to
delete the first old backup dir i.e
I am kind a lost in finalizing the approach to acheive this.


How would you do this manually?

Would you look at the filenames or the timestamps?

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.


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

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.


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

John W. Krahn

I have structure like below

dira/backup-050105
dira/backup-050805
dirb/backup-050105
dirb/backup-050805
.
.
.
dirf/backup-050105
dirf/backup-050805

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.

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" ]
}
<*/backup-*>;

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{$dir}[0]"
or warn "Cannot delete $dir/$by_dir{$dir}[0]: $!";
}



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



John
 
J

Joe Smith

John said:
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" ]
}
<*/backup-*>;

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

linux% mkdir -p dir{a,b,f}/backup-2005-05-{01,08,15}
linux% perl
print "Oldest for $_ is ",oldest_name($_),"\n" for glob "dir*";
sub oldest_name { (glob "$_[0]/backup-*")[0]; }
^D
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?

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top