Log file rotation script

Reply

Join Date: Sep 2004
Posts: 20
Reputation: TimmyRaa is an unknown quantity at this point 
Solved Threads: 0
TimmyRaa's Avatar
TimmyRaa TimmyRaa is offline Offline
Newbie Poster

Log file rotation script

 
0
  #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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 212
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Log file rotation script

 
0
  #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 Quick reply to this message  
Join Date: Dec 2004
Posts: 42
Reputation: YUPAPA is an unknown quantity at this point 
Solved Threads: 0
YUPAPA's Avatar
YUPAPA YUPAPA is offline Offline
Light Poster

Re: Log file rotation script

 
0
  #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 Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 212
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Log file rotation script

 
0
  #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 Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 212
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Log file rotation script

 
0
  #5
May 12th, 2005
Someone E-mailed Me, Requesting The Source For This, So, Here is the actual script (with modified paths for privacy):

  1. #!/usr/bin/perl
  2.  
  3. # The Line Above Might Need To Be Changed To Wherever Perl is Located
  4.  
  5. #################################
  6. # Custom Defined User Variables #
  7. #################################
  8.  
  9. $logpath = "/home/sites/path2your/logs";
  10. $orgfile1 = "youraccess_log";
  11. $orgfile2 = "cabmirror.youraccess_log"; # Assuming You Have A Mirror Log
  12.  
  13. $use_rename = "true"; # Rename The Original Logfile After The Date is Added?
  14. $use_zip = "true"; # Use Zip Compression On The Renamed Logfiles?
  15. $remove_original_after_zip = "true"; # Remove The Orignal Logfile AFTER it has been zipped? # Or Have a logfile, and a zipped logfile?
  16.  
  17. ###################################
  18. # End of Custom Defined Variables #
  19. ###################################
  20.  
  21. # /* Get The Formated Date For Today */
  22. $today = &get_todays_date;
  23.  
  24. # /* If We Are Renaming The Original Log File */
  25. if ($use_rename eq "true") {
  26.  
  27. # /* Call Sub-Routine To Rename Log File */
  28. &rename_logfile;
  29.  
  30. } else {
  31.  
  32. # /* Call Sub-Routine To Xerox Log File With New Name */
  33. &xerox_logfile;
  34. }
  35.  
  36. # /* If We Are Going To Zip The Log Files */
  37. if ($use_zip eq "true") {
  38. # /* Call The Sub-Routine To Zip Logfiles of "Today" */
  39. &zip_logfiles;
  40. }
  41.  
  42. # /* Stop This Program From Running */
  43. exit;
  44.  
  45. sub get_todays_date
  46. {
  47. # /* Get The Current Date (Open As A new Thread) */
  48. open(DT, "date +\%D |");
  49.  
  50. # /* Store The Date In Variable $rawdate, and remove any new line characters */
  51. chomp($rawdate = <DT>);
  52.  
  53. # /* Close The "Date" Process */
  54. close(DT);
  55.  
  56. # /* Rip Apart The Raw Date Into Respective Parts */
  57. ($month, $day, $year) = split(/\//, $rawdate);
  58.  
  59. # /* Format The Parts Of The Date Into Our Desired Format */
  60. $good_date = "20$year-$month-$day";
  61.  
  62. # /* Return The Formated Date Back To Our Main Program From This Sub */
  63. return $good_date;
  64. }
  65.  
  66. sub rename_logfile
  67. {
  68.  
  69. # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
  70. if (-e "$logpath/$orgfile1") {
  71.  
  72. # /* Rename The Original File To The New File With The Appended Date */
  73. `mv $logpath/$orgfile1 $logpath/$orgfile1.$today`;
  74. }
  75.  
  76. # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
  77. if (-e "$logpath/$orgfile2") {
  78.  
  79. # /* Rename The Original File To The New File With The Appended Date */
  80. `mv $logpath/$orgfile2 $logpath/$orgfile2.$today`;
  81. }
  82. }
  83.  
  84. sub xerox_logfile
  85. {
  86.  
  87. # /* If The First Logfile Exists, Then Archive It, Otherswise Do Nothing */
  88. if (-e "$logpath/$orgfile1") {
  89.  
  90. # /* Copy The Original File To The New File With The Appended Date */
  91. `cp $logpath/$orgfile1 $logpath/$orgfile1.$today`;
  92. }
  93.  
  94. # /* If The Second Logfile Exists, Then Archive It, Otherwise Do Nothing */
  95. if (-e "$logpath/$orgfile2") {
  96.  
  97. # /* Copy The Original File To The New File With The Appended Date */
  98. `cp $logpath/$orgfile2 $logpath/$orgfile2.$today`;
  99. }
  100.  
  101. }
  102.  
  103. sub zip_logfiles
  104. {
  105.  
  106. # /* If The Logfile With Appended Date Exists */
  107. if (-e "$logpath/$orgfile1.$today") {
  108.  
  109. # /* Add The Logfile To It's Own Zip Acrchives */
  110. `zip $logpath/$orgfile1.$today.zip $logpath/$orgfile1.$today`;
  111. }
  112.  
  113. # /* If The Logfile (With Appended Date) Exists */
  114. if (-e "$logpath/$orgfile2.$today") {
  115.  
  116. # /* Add The Logfile To It's Own Zip Archive */
  117. `zip $logpath/$orgfile2.$today.zip $logpath/$orgfile2.$today`;
  118. }
  119.  
  120. # /* If We Are Going To Remove The Original Logfile (Not The Zip) After it's been Zipped */
  121. if ($remove_original_after_zip eq "true") {
  122.  
  123. # /* If The First Logfile (Uncompressed) Exists */
  124. if (-e "$logpath/$orgfile1.$today") {
  125.  
  126. # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
  127. unlink "$logpath/$orgfile1.$today";
  128.  
  129. }
  130.  
  131. # /* If The Second Logfile (Uncompressed) Exists */
  132. if (-e "$logpath/$orgfile2.$today") {
  133.  
  134. # /* Then Delete The File (The Uncompressed Logfile, Not The Zip) */
  135. unlink "$logpath/$orgfile2.$today";
  136.  
  137. }
  138. }
  139. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 1
Reputation: sretech is an unknown quantity at this point 
Solved Threads: 0
sretech sretech is offline Offline
Newbie Poster

Re: Log file rotation script

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

Re: Log file rotation script

 
0
  #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 Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 212
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Log file rotation script

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

Re: Log file rotation script

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum


Views: 15154 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC