how to purge backup files

Discussion in 'Perl Misc' started by sanjeev.sagar@gmail.com, May 10, 2005.

  1. Guest

    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.
     
    , May 10, 2005
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, May 10, 2005
    #2
    1. Advertising

  3. <> wrote:

    > 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";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 10, 2005
    #3
  4. wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 10, 2005
    #4
  5. Joe Smith Guest

    John W. Krahn wrote:

    > 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
     
    Joe Smith, May 11, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQ=?=

    Diable Explorer Button or Purge History List

    =?Utf-8?B?RGF2aWQ=?=, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    388
    =?Utf-8?B?RGF2aWQ=?=
    Feb 19, 2004
  2. gavino
    Replies:
    13
    Views:
    1,067
  3. purge like utility in c

    , Oct 10, 2006, in forum: C Programming
    Replies:
    118
    Views:
    1,703
    Dave Thompson
    Oct 23, 2006
  4. Bill M

    purge old build files

    Bill M, Feb 3, 2012, in forum: Perl Misc
    Replies:
    0
    Views:
    446
    Bill M
    Feb 3, 2012
  5. Replies:
    0
    Views:
    662
Loading...

Share This Page