•
•
•
•
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
![]() |
•
•
Join Date: Sep 2004
Location: Cheltenham, UK
Posts: 20
Reputation:
Rep Power: 5
Solved Threads: 0
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'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!
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
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.
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
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";
}
}
}•
•
Join Date: Jun 2005
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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!
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!
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 108
![]() |
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- About;Blank Please Help, Hijack Log File (Viruses, Spyware and other Nasties)
Other Threads in the Perl Forum
- Previous Thread: Perl Whitespaces
- Next Thread: Finding a number in a line



Linear Mode