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`;
}
Related Posts
1 Comment to “Tip: Perl Script to Archive Log Files”
Post comment
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- baby
on My October Earnings from Adbrite - ramakanth
on My October Earnings from Adbrite - juan kara-an
on Cebu Pacific Airlines is Evil! - Eugene Engelbrecht
on Cebu Pacific Airlines is Evil! - David Beecher
on Howto: Install yum On RHEL 4









[...] is little perl script created by rhai of pinoytux.com on how to rotate apache log files. [...]