Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Log file backup

by munk (Novice)
on May 23, 2002 at 14:39 UTC ( #168774=snippet: print w/replies, xml ) Need Help??
Description: 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# 5:49:40 hancock6@server4 /home/hancock6/logs# ls -R .: 200203/ 200204/ 200205/ access_log error_log* 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 :)
# 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:
        push(@logfiles, $file);
        print $file,"\n";

for my $logfile (@logfiles){
    # foreach logfile in @logfiles,
    # build the dirname to copy it to:
    my $dirname=substr($1,0,-2);

    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:
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://168774]
[Corion]: 1nickt: Finding autobox in production would give me pause, yes
[LanX]: efficient survey
[MidLifeXis]: And under MINGW64_NT-6.1 MYHOST 2.6.0(0.304/5/3) 2016-09-09 09:46 x86_64 Msys there seem to be issues with escapes in external build tool calls.
[Corion]: I mean, it's a technical feat it achieves, but... why? ;)
[MidLifeXis]: And it also has the 0.14 version of the tarball in its manifest.
[LanX]: avoiding unreadable brackets
[MidLifeXis]: Although the previous one could be a b0rken PATH, I would need to dig for that.
[thezip]: I've got to go to meetings now. If anyone has further comments regarding Spreadsheet::XLSX deployment to Strawberry Perl 5.24.1, please /msg me -- thanks!
LanX has to go ... plans to crash with a car into a group of pythonistas while screaming "LARRY IS THE GREATEST"
LanX ... darn ... where is my car?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-03-23 17:28 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (291 votes). Check out past polls.