#!/usr/bin/perl # Magnus little logrotator # (C) magnus(that at sign thing)abrante.net $default_directory="/var/log"; $max_old_save="4"; $input=$ARGV[0]; die "no arguments. Usage: $0 /path/to/file \n" if ($input eq ""); @array = split ('/', $input); # Last piece of argument in /path/to/file is the file, the rest is the # directory. Put the last thing from $logfile into $file and the rest into # $directory foreach $testing (@array) { $directory = "$directory/$tmp_string" if ($tmp_string ne ""); $tmp_string=$testing; } $logfile = $tmp_string; # in case we only got a filename: $directory=$default_directory if ($directory eq ""); die "no such file. Usage: $0 /path/to/logfile\n" if ( !-f $logfile ); chdir $directory; # Start from the $max_old_save value, move files from previous value $pointer = $max_old_save; until ($lastpointer eq "0") { # Is this the last file? if ($pointer eq $max_old_save ) { if ( -f "$logfile.$pointer.gz" ) { unlink "$logfile.$pointer.gz"; } $lastpointer = $pointer; $pointer--; next; } if ( -f "$logfile.$pointer.gz" ) { rename ("$logfile.$pointer.gz", "$logfile.$lastpointer.gz"); } $lastpointer = $pointer; $pointer--; } system "cp $logfile $logfile.$lastpointer"; system "cp /dev/null $logfile"; system "gzip $logfile.$lastpointer";