Tip: Perl Script to Archive Log Files

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`;

}