This is a simple Perl script I wrote to search a given directory for *.log files, tag with the current date and archive them.

#!/usr/bin/perl

$DIR=”/var/www/html/sites/logs”;
$DATE=`date +%F`;
chomp $DATE;

@log = `/usr/bin/find $DIR -type f -name “*.log” `;

#print “\nRotating $DATE\n”;

foreach $log (@log) {
chomp $log;
$new_fn=”$log”.”-”.”$DATE”;
#print “$log $new_fn\n”;
`mv “$log” “$new_fn”`;

}

@raw=`/usr/bin/find $DIR -type f -name “*$DATE*”`;

foreach $raw (@raw) {
chomp $raw;
#print “$raw\n”;
`gzip -9 $raw`;

}

EDIT:

Modified version:

#!/usr/bin/perl

$DIR=”/var/www/html/sites/logs”;
$DATE=`date +%F`;
chomp $DATE;

@log = `/usr/bin/find $DIR -type f -name “*.log” `;

#print “\nRotating $DATE\n”;

foreach $log (@log) {
chomp $log;
$new_fn=”$log”.”-”.”$DATE”;
#print “$log $new_fn\n”;
`mv “$log” “$new_fn”`;
`gzip -9 $new_fn`;

}

Popularity: 10% [?]

Share and Enjoy:
  • E-mail this story to a friend!
  • StumbleUpon
  • Digg
  • Technorati
  • del.icio.us
  • Reddit
  • Facebook
  • Google
  • Slashdot
  • Blogosphere News
  • TwitThis
  • NewsVine
  • Propeller
  • Furl
  • Simpy
  • Spurl

Related Posts