I'm trying to find or create a Perl script that at runtime will simply take a specified logfile or logfiles, and append todays date onto the end of it's name. If possible, I would also like the option of zipping the new file up. I can then run this as a Cron job every month, and keep a nice labelled archive of logs.

I've found quite a few on the net already, but they either require compiling and some form of installation of files into areas I can't access on my host, or they're just too complex and try to do too much - my requirements are very very simple, it's really just a renaming script.

Does anyone know of something that does just this already, or do you have any code that could be adapted easily? As a last resort, could someone give me a framework or pointers on how easy it would be for an absolute beginner in Perl - I've done some basic C and VB before.

Thanks!

I need more information, exactly on what you want... like... what you have access to all. Let me put it this way, if you want to give me the paths to the log files, and the paths to where you want them saved, I'll build it for you. I'm not 100% sure about the zipping.... I know I could tar them, but tarring individual files? I am sure I could bzip it or something..... let me know, my e-mail is available on this site by clicking my name, and following the menus.

Looks simple. If I understand correctly, you just have to rename the file to todays date and also compress it.

Yes. Truth is, it's already done, and he's already using it. I coded it, and commented it line by line, so he could learn from and modify it for future needs.

Someone E-mailed Me, Requesting The Source For This, So, Here is the actual script (with modified paths for privacy):

#!/usr/bin/perl

# The Line Above Might Need To Be Changed To Wherever Perl is Located

#################################
# Custom Defined User Variables #
#################################

$logpath = "/home/sites/path2your/logs";
$orgfile1 = "youraccess_log";
$orgfile2 = "cabmirror.youraccess_log";  # Assuming  You Have A Mirror Log

$use_rename = "true";                    # Rename The Original Logfile After The Date is Added?
$use_zip = "true";                       # Use Zip Compression On The Renamed Logfiles?
$remove_original_after_zip = "true";     # Remove The Orignal Logfile AFTER it has been zipped?                              # Or Have a logfile, and a zipped logfile?

###################################
# End of Custom Defined Variables #
###################################

# /* Get The Formated Date For Today */
$today = &get_todays_date;

# /* If We Are Renaming The Original Log File */
if ($use_rename eq "true") {

       # /* Call Sub-Routine To Rename Log File */
       &rename_logfile;

} else {

       # /* Call Sub-Routine To Xerox Log File With New Name */
       &xerox_logfile;
}

# /* If We Are Going To Zip The Log Files */
if ($use_zip eq "true") {
       # /* Call The Sub-Routine To Zip Logfiles of "Today" */
       &zip_logfiles;
}

# /* Stop This Program From Running */
exit;

sub get_todays_date
{
       # /* Get The Current Date (Open As A new Thread) */
       open(DT, "date +\%D |");

               # /* Store The Date In Variable $rawdate, and remove any new line characters */
               chomp($rawdate = <DT>);

       # /* Close The "Date" Process */
       close(DT);

       # /* Rip Apart The Raw Date Into Respective Parts */
       ($month, $day, $year) = split(/\//, $rawdate);

       # /* Format The Parts Of The Date Into Our Desired Format */
       $good_date = "20$year-$month-$day";

       # /* Return The Formated Date Back To Our Main Program From This Sub */
       return $good_date;
}

sub rename_logfile
{

       # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
       if (-e "$logpath/$orgfile1") {

               # /* Rename The Original File To The New File With The Appended Date */
               `mv $logpath/$orgfile1 $logpath/$orgfile1.$today`;
       }

       # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
       if (-e "$logpath/$orgfile2") {

               # /* Rename The Original File To The New File With The Appended Date */
               `mv $logpath/$orgfile2 $logpath/$orgfile2.$today`;
       }
}

sub xerox_logfile
{

       # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
       if (-e "$logpath/$orgfile1") {

               # /* Copy The Original File To The New File With The Appended Date */
               `cp $logpath/$orgfile1 $logpath/$orgfile1.$today`;
       }

       # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
       if (-e "$logpath/$orgfile2") {

               # /* Copy The Original File To The New File With The Appended Date */
               `cp $logpath/$orgfile2 $logpath/$orgfile2.$today`;
       }

}

sub zip_logfiles
{

       # /* If The Logfile With Appended Date Exists */
       if (-e "$logpath/$orgfile1.$today") {

               # /* Add The Logfile To It's  Own Zip Acrchives */
               `zip $logpath/$orgfile1.$today.zip $logpath/$orgfile1.$today`;
       }

       # /* If The Logfile (With Appended Date) Exists */
       if (-e "$logpath/$orgfile2.$today") {

               # /* Add The Logfile To It's Own Zip Archive */
               `zip $logpath/$orgfile2.$today.zip $logpath/$orgfile2.$today`;
       }

       # /* If We Are Going To Remove The Original Logfile (Not The Zip) After it's been Zipped */
       if ($remove_original_after_zip eq "true") {

               # /* If The First Logfile (Uncompressed) Exists */
               if (-e "$logpath/$orgfile1.$today") {

                       # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
                       unlink "$logpath/$orgfile1.$today";

               }

               # /* If The Second Logfile (Uncompressed) Exists */
               if (-e "$logpath/$orgfile2.$today") {

                       # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
                       unlink "$logpath/$orgfile2.$today";

               }
       }
}

Thank You for posting.

greetings,

i know this is a perl forum, but we we rotate logs at my job using a ksh script on a cron every night at 11:59 pm:

#!/bin/ksh
DATE=`date +%Y%m%d`
FILES=`find /home/shared/logs/ \( -name \*.start -o -name \*.log \)`
for logfile in $FILES
do
        newlogfile=$logfile.$DATE
        CMD="cp $logfile $newlogfile"
        echo $CMD
        $CMD
        cat /dev/null > $logfile
        rotated_logs="$rotated_logs $newlogfile"
done
for newlogfile in $rotated_logs
do
        nice -20 /usr/bin/gzip -9 $newlogfile
done

it appends the date to the end of the file before zipping. we also run another script afterwards to remove logs older than 2 weeks:

find /home/shared/logs/ \( -name \*.start.\* -o -name \*.log.????????\* -o -name \*start-\* -o -name \*.log.\* -o -name \*.log-\* -o -name \*.log????? -o -name \*.log.????????.gz \)  -mtime +14 -exec rm -f {} \;

good luck!

Yes, It Is A Perl Forum.... But Nice Script.

Yes, It Is A Perl Forum.... But Nice Script.

thanks... sorry about posting non-perl code :o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.