Wednesday, July 9, 2008

Log rotate final solution


#!/usr/bin/perl -w
use strict;

my $numDays = 7; #Number of days to retain Output Directories
my $numLongDays = 30; #Number of days to reatain archived tars

#get the current date time and break up
my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);

#create date stamp
$year = $year + 1900;
my $dateStamp = "$mon-$mday-$year";

#find out who is running this process
my $who = `whoami`;
chomp $who;

#Get the list of directories
my @baseDirs = `find /usr/peoplesoft/$who/appserv/prcs/* \\( ! -name dir -prune \\) -type d -print`;

#for each dir arcvhive and delete folder from log_ouput
foreach my $baseDir (@baseDirs) {
chomp $baseDir;

#set the path to the directory that has the logs and the archive directory
my $logDir = "$baseDir/log_output";
my $logArchiveDir ="$baseDir/log_output_archive";

unless(-d $logArchiveDir){
mkdir $logArchiveDir or die "Couldn't create dir: [$logArchiveDir] ($!)";;
}

#archive then delete out put folders
system("/usr/bin/tar cvf - `/usr/bin/find $logDir/* -mtime +$numDays` | gzip > $logArchiveDir/log_output_$dateStamp.tar.gz");
system("/usr/bin/find $logDir/* -mtime +$numDays -exec rm -rf {} \\;");
system("/usr/bin/find $logArchiveDir/* -mtime +$numLongDays -exec rm -rf {} \\;");
}

No comments: