Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Log file backup

by munk (Novice)
on May 23, 2002 at 14:39 UTC ( [id://168774]=CUFP: print w/replies, xml ) Need Help??

Moves all log files in a directory into new directories based on the name(s) of the original files.

Example: (output from Unix 'script command')

5:49:24 hancock6@server4 /home/hancock6# cd logs 5:49:28 hancock6@server4 /home/hancock6/logs# ls 20020305.tar.gz 20020324.tar.gz 20020412.tar.gz 20020501.tar.gz 200203 +06.tar.gz 20020325.tar.gz 20020413.tar.gz 20020502.tar.gz 20020307.ta +r.gz 20020326.tar.gz 20020414.tar.gz 20020503.tar.gz 20020308.tar.gz +20020327.tar.gz 20020415.tar.gz 20020504.tar.gz 20020309.tar.gz 20020 +328.tar.gz 20020416.tar.gz 20020505.tar.gz 20020310.tar.gz 20020329.t +ar.gz 20020417.tar.gz 20020506.tar.gz 20020311.tar.gz 20020330.tar.gz + 20020418.tar.gz 20020507.tar.gz 20020312.tar.gz 20020331.tar.gz 2002 +0419.tar.gz 20020508.tar.gz 20020313.tar.gz 20020401.tar.gz 20020420. +tar.gz 20020509.tar.gz 20020314.tar.gz 20020402.tar.gz 20020421.tar.g +z 20020510.tar.gz 20020315.tar.gz 20020403.tar.gz 20020422.tar.gz 200 +20511.tar.gz 20020316.tar.gz 20020404.tar.gz 20020423.tar.gz 20020512 +.tar.gz 20020317.tar.gz 20020405.tar.gz 20020424.tar.gz 20020513.tar. +gz 20020318.tar.gz ... etc 5:49:29 hancock6@server4 /home/hancock6/logs# mvlog.pl 5:49:40 hancock6@server4 /home/hancock6/logs# ls -R .: 200203/ 200204/ 200205/ access_log error_log mvlog.pl* 200203: 20020305.tar.gz 20020312.tar.gz 20020319.tar.gz 20020326.tar.g +z 20020306.tar.gz 20020313.tar.gz 20020320.tar.gz 20020327.tar.gz 200 +20307.tar.gz 20020314.tar.gz 20020321.tar.gz 20020328.tar.gz 20020308 +.tar.gz 20020315.tar.gz 20020322.tar.gz 20020329.tar.gz 20020309.tar. +gz 20020316.tar.gz 20020323.tar.gz 20020330.tar.gz 20020310.tar.gz 20 +020317.tar.gz 20020324.tar.gz 20020331.tar.gz 20020311.tar.gz 2002031 +8.tar.gz 20020325.tar.gz 200204: 20020401.tar.gz 20020409.tar.gz 20020417.tar.gz 20020425.tar.g +z 20020402.tar.gz 20020410.tar.gz 20020418.tar.gz 20020426.tar.gz 200 +20403.tar.gz 20020411.tar.gz 20020419.tar.gz 20020427.tar.gz 20020404 +.tar.gz 20020412.tar.gz 20020420.tar.gz 20020428.tar.gz 20020405.tar. +gz 20020413.tar.gz 20020421.tar.gz 20020429.tar.gz ... etc


Hope this gives an idea of what it does! It's pretty much hardwired to what I needed it to do as well (as in name formats for the files/dirs to create), but maybe someone might find it of use :)
#!/usr/bin/perl # # move files from a log dir # into a newly created directory # based on the name of the logfiles # 20020305.tar.gz use strict; my (@logfiles); # file cntaining all the logfiles: my $logdir="/home/hancock6/logs"; #open the dir ready for reading: opendir(LOGDIR, $logdir); #read in files from dir: my @logdir=readdir(LOGDIR); for my $file (@logdir){ # foreach file in the dir, if it's a tarball, # copy it into @logfiles: if($file=~/(.*)\.tar\.gz$/){ push(@logfiles, $file); print $file,"\n"; } } for my $logfile (@logfiles){ # foreach logfile in @logfiles, # build the dirname to copy it to: $logfile=~/(.*)\.tar\.gz$/; my $dirname=substr($1,0,-2); $dirname=$logdir."/".$dirname; print $dirname,"\n"; # check if the dir exists, if not, create it: if(!-e $dirname){ mkdir($dirname, 0755); } # copy the file into the dir: link ($logdir."/".$logfile, $dirname."/".$logfile); # remover the old file: unlink($logdir."/".$logfile); }

Replies are listed 'Best First'.
•Re: Log file backup
by merlyn (Sage) on May 23, 2002 at 16:04 UTC
    $logfile=~/(.*)\.tar\.gz$/; my $dirname=substr($1,0,-2);
    If the match fails, that $1 will be wrong. Never use $1 unless it's in the conditional of the match test. I'd change those two lines to:
    $logfile =~ /(.*)..\.tar.gz$/ or die "Bad non-match on $logfile"; my $dirname = $1;

    Also, your "link/unlink" pair can be replaced by a simple "rename". They're equivalent under the hood, with a lot less duplication.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-16 18:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found