Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Delete all but the most recent backup file

by jagexCoder (Novice)
on Jan 27, 2013 at 14:54 UTC ( #1015577=note: print w/ replies, xml ) Need Help??


in reply to Re: Delete all but the most recent backup file
in thread Delete all but the most recent backup file

Thanks it certainly is useful and does perform deletion. However I have two queries: (1) Komodo Edit reports:

Name "main::DATA" used only once: possible typo at bk_remove.pl line 7 +3. readline() on unopened filehandle DATA at bk_remove.pl line 73.
This is referring to <DATA> in the code. (2) It appears the code does the deletion based on the date listed in the filename, while this is ideal I've done the previous coding based on the day modified only (using -M) and not the date on the filename so I would like to keep it this way. Any ideas on how I could modify this to the way I did it? Much appreciated for all answers, apologies I am not very good at perl however I do get the odd scripts here and there done when needed. Thanks again!


Comment on Re^2: Delete all but the most recent backup file
Download Code
Re^3: Delete all but the most recent backup file
by soonix (Curate) on Jan 27, 2013 at 20:05 UTC
    I guess you'd have to replace the line
    map { my ( $d, $m, $y ) = /(\d+)/g; [ $_, "$y$m$d" ] }
    by something like
    map {[$_, -M $_]}
    However, currently I'm not often using these functions, so of course you should test it first, not that you end up keeping the oldest instead of the newest backup...
Re^3: Delete all but the most recent backup file
by Kenosis (Priest) on Jan 27, 2013 at 23:12 UTC

    You're most welcome!

    Yes, Komodo appears to just be alerting you about <DATA>, but should certainly know better, since there's a __DATA__ section.

    Try the following:

    use strict; use warnings; use File::stat; chomp( my @fileNames = <*.bak> ); my @sortedFileNames = map $_->[0], sort { $b->[1] <=> $a->[1] } map { my $stat = stat $_; [ $_, $stat->mtime ] } grep /^backup_\d\d_\d\d_\d{4}.bak$/, @fileNames; shift @sortedFileNames; if (@sortedFileNames) { print "$_\n" for @sortedFileNames; #unlink @sortedFileNames; }

    This stats each file for the modification time, using it in the sort. Also, note that a file glob's used to read directory files...

      Hi thanks, that works great! I modified the regular expression to the file naming format that we use at work. I just noticed that the system of sorting and preserving the latest backup and deleting the rest is the most efficient - my supervisor and dad (he's a programmer) said the same thing. I don't know why I stuck with the idea of using flags, I guess it's the little mistakes the not-that-experienced programmers make. This is a good learning experience! I'll figure out the syntax related to sorting that has been implemented and understand it fully. Thanks again to both of you - take care!

        Here is an alternative idea

        Archive the backups for a month before deleting them (like zip/7-zip)

        Since the files are nearly identical, it should compress really well, so in the same space you have of 24hrs of backups you can have ~20 days of backups (one zip per day), and if you combine those zip/day backups into a single archive, you'll have space for more days :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-12-20 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls