Ok.  Here's my situation.  I am modifying a Perl script that was created 
some time ago and am learning Perl on kind of a "trial by fire" basis 
(with a couple of good books).

The problem was first brought to my attention that we still had old log 
files greater than 60 old.  The gziplog Perl script you see below is 
supposed to do 3 things

        1.  Keep today's and yesterday's log file in uncompressed format
        2.  Anything older than 2 days up to 60 days, compress
        3.  Anything older than 60 days, delete

Originally, the problem was the last item.  The script would delete some 
but not all files older than 60 days.  Here's the difference:

Before (sub RemoveOldLogs)

          for ($day=$keeplogs+1;$day<=$keeplogs+60;$day++) {

After my change

          for ($day=$keeplogs+1;$day>$keeplogs;$day++) {


After making this change, old files from Mar/Apr 2003 were deleted 
successfully, but the script wouldn't end without a CTRL +C.  I'm thinking 
because of the increment "$day++" it just keeps looking.  Do I need to put 
in a "last" statement somewhere or using a While statement instead?

What are you thoughts, expert Perl mongers?


#!/usr/bin/perl -w
#
#
#    These logs remain for x days (as set by keeplogs) and then are 
deleted.
#    This script needs to be run at least once a week.
#    This script is best run at midnight each day.
#
#    Changes made 02Apr2003 by xxxxx to fix deletion of old logs
#    5/22/03 - Changed "ziplogs" to 2 so that logs get zipped more 
frequently
#
#
$release = "1.0";

$logs = "/var/spool/logs";              # location of logs
$ziplogs = 02;                  # days before zipping logs
$keeplogs = $ENV{KEEPMAILLOGS}; # days to keep daily logs, but >= 60
$keeplogs = 60; # unless ($keeplogs >= 60);

$host=`hostname`;chop($host);   # get local host name
chdir("/");
$clock = time();                # get start time
$oneday = 24*60*60;             # seconds in one day
@monthname = 
("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

GetLogName(1);                  # get log name and date format for 
yesterday
ZipOldLogs($ziplogs);           # zip up old logs to save space
RemoveOldLogs($keeplogs);       # remove logs too old to be usefull
exit(0);

#

# zip any logs which are older $ziplogs
#
sub ZipOldLogs {
  for ($day=$ziplogs;$day<=$keeplogs;$day++) {
    GetLogName($day);
    if (-r $logname) {
      unless (-r "$logname.gz") {
        print "Zipping log for $logdate ($logday)\n";
        system("/bin/gzip $logname");
      }
    }
  }
}

#
# remove any logs which are older than we want to keep
#
sub RemoveOldLogs {
#                                       ** RK 04Apr2003 was 7 before
  for ($day=$keeplogs+1;$day>$keeplogs;$day++) {
    GetLogName($day);
#                       * RK 04Apr2003 the "$" was missing
    @dated = ($logname,"$logname.gz");
    foreach $datedfile (@dated) {
      if (-r $datedfile) {
#                         * RK 04Apr2003 there was an extraneous "g" so it 
didn't print the file name
        print "Purging $datedfile\n";
        if (length($datedfile)<10) {die "Invalid log name $datedfile\n";}
        unlink ($datedfile);
      }
    }
  }
}
#
sub nn {
  local($xn) = $_[0];
  $xn = $xn+0;
  if ($xn<10) { return "0$xn"; }
  return "$xn";
};
#
#format date as 2000-12-31
#
sub fmtdate {
  local($fsec,$fmin,$fhour,$fday,$fmon,$fyear) = localtime($_[0]);
  $fyear+=1900;
  $fmon++;
  return $fyear.".".nn($fmon).".".nn($fday);
};
#
sub fmtday {
  local($fsec,$fmin,$fhour,$fday,$fmon,$fyear) = localtime($_[0]);
  $fmon = $monthname[$fmon];
  if ($fday<10) { return "$fmon $fday";}
  return "$fmon $fday";
};

#
# get log date and log name for a relative date
#
sub GetLogName {
  local($day) = $_[0]+0;
  if ($day < 1) {die "Invalid number of log days... $day \n";}
  $logtime = $clock-$day*$oneday;               # backup a number of days
  $logdate = fmtdate($logtime);                 # get date for log file
  $logday = fmtday($logtime);                   # get day for log file 
extract
  $logname = "$logs/log.$logdate";              # get daily log name
};

Reply via email to