#!/usr/bin/perl -w ################################################################################ # # logrotate - Danny Sauer (sauer@cloudmaster.com) # logrotate backs up the system logs when they get older than a # specific age or go over a specified size limit. It's designed # to be run from # # Version 0.1 - 7/13/2000 # Initial version written for stu, questionable features... # Version 0.2 - 7/25/2000 # Added in date and time checks, overwrote old version # Version 0.3 - 8/08/2000 # Added support for "no compression" example... # # Todo: # Add actual date / size check code # add support for command-line options to rotate a specific log without # modifying the code (maybe for a system user to rotate their personal # logs or something like that... # ################################################################################ use File::stat; # insert full paths to log files to be checked here. # make sure to update the %maxage and %maxsize hash too # if you don't want the defaults to be used... @logs = ( # full path to log files to back up "/var/log/messages", "/var/log/warn", "/var/log/mail" ); %maxage = ( # maximum age in days $logs[0] => 30, # I use $logs[] to avoid mispelingz $logs[1] => 60, $logs[2] => 14 ); $def_maxage = 30; # default $maxage if logfile not in hash %maxsize = ( # maximum size in KB $logs[0] => 2048, $logs[1] => 2048, $logs[2] => 2048 ); $def_maxsize = 2048; # default $maxsize # Normally, the rotated logs are left in the same directory. # uncomment this to move rotated logs to a specific archive directory # rotated logs will still have the datestamp appended #$OldLogDir = "/var/log/old"; # compress($infile, $outfile) # compression subroutine - customize this however you want # it wil be called with the logfile to compress as the first arg, and # the output file as the second. Here are examples: # # bzip2 $bzipP = "/usr/bin/bzip2"; sub compress{ $i=shift; $o=shift; system("$bzipP -zc $i > $o.bz2");}; # ## gzip #$gzipP = "/usr/bin/gzip"; #sub compress{ $i=shift; $o=shift; system("gzip -c $i > $o.gz");}; # ## compress #$compressP = "/usr/bin/compress"; #sub compress{ $i=shift; $o=shift; system("$compressP -c -f $i > $o.Z");}; # ## no compression #sub compress{ ($i,$o)=@_; open(A,$i); open(B,$o); while(){print B;}; }; ################################################################################ # end user-modifiable code (mostly) ################################################################################ foreach $log (@logs){ # get file's info unless($stat = stat($log)){ print "Problem stat()'ing $log: $!"; next; } # check date first ### # Unfortunately, syslog seems to scerw with ctime, so logfile age # checks don't work worth a darn. If anyone knows how to fix that, # feel free to lemme know... ### $maxtime = $maxage{$log} || $def_maxage; $maxtime *= 24 * 60 * 60; # 24hr/day, 60min/hr, 60sec/min print "MaxTime is $maxtime (", 1+(localtime($maxtime))[7], " days)\n"; $age = time() - $stat->ctime; if($age > $maxtime){ $why = "ctime $age is greater than $maxtime"; rotate($log, $why); } # check size $size = $stat->size; # size of file, in bytes print "$log age: $age, size $size\n"; $max_size = $maxsize{$log} || $def_maxsize; $max_size *= 1024; # maxsize is given in KB, stat gives Bytes. rotate($log, "bigger than $max_size bytes") if($size > $max_size); } exit(0); sub rotate{ $file = shift; $reason = shift; $reason = defined($reason) ? "because $reason" : ""; print "rotating $file $reason\n"; }