User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 426,907 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,307 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 12265 | Replies: 8
Reply
Join Date: Sep 2004
Location: Cheltenham, UK
Posts: 20
Reputation: TimmyRaa is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
TimmyRaa's Avatar
TimmyRaa TimmyRaa is offline Offline
Newbie Poster

Log file rotation script

  #1  
Jan 14th, 2005
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Log file rotation script

  #2  
Jan 14th, 2005
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.
Reply With Quote  
Join Date: Dec 2004
Posts: 42
Reputation: YUPAPA is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
YUPAPA's Avatar
YUPAPA YUPAPA is offline Offline
Light Poster

Solution Re: Log file rotation script

  #3  
Jan 18th, 2005
Looks simple. If I understand correctly, you just have to rename the file to todays date and also compress it.
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Log file rotation script

  #4  
Jan 18th, 2005
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.
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Log file rotation script

  #5  
May 12th, 2005
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";

               }
       }
}
Reply With Quote  
Join Date: May 2005
Posts: 1
Reputation: sretech is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sretech sretech is offline Offline
Newbie Poster

Re: Log file rotation script

  #6  
May 12th, 2005
Thank You for posting.
Reply With Quote  
Join Date: Jun 2005
Posts: 6
Reputation: optomystique is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
optomystique optomystique is offline Offline
Newbie Poster

Re: Log file rotation script

  #7  
Jun 8th, 2005
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:

[PHP]#!/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[/PHP]

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:

[PHP]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 {} \;[/PHP]

good luck!
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 108
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Log file rotation script

  #8  
Jun 8th, 2005
Yes, It Is A Perl Forum.... But Nice Script.
Reply With Quote  
Join Date: Jun 2005
Posts: 6
Reputation: optomystique is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
optomystique optomystique is offline Offline
Newbie Poster

Re: Log file rotation script

  #9  
Jun 8th, 2005
Originally Posted by Comatose
Yes, It Is A Perl Forum.... But Nice Script.

thanks... sorry about posting non-perl code :o
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 11:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC