Pages

Thursday, January 26, 2012

Perl Script for Automatically Backing Up Log Files

This Perl script was written for backing up system log files in the /tmp/ directory and tomcat6 log files in the /usr/share/tomcat6/logs/ directory.

Modify the code to suite your personal needs. Hopefully this will provide a helpful platform to build of off!


**********Begin Perl Script for Log Backup***********

#!/usr/bin/perl -w

use strict;

########################################################################
#
# Created by Andrew Stout <stout.andrew@ymail.com>
# Origination Date: Jan 24, 2011
#
# Current Version: 1.1.2
# Release Date: Mar 13, 2012
#
# This Perl application archives Linux system and Apache Tomcat log files for
# system and network administrators. Each file is compressed and renamed with a
# timestamp, that identifies when the compression occured, appended as a prefix
# to the original filename and .tar.gz as the suffix, representing the compression
# methods used. These files are then relocated to the /home/logArchive/ directory
# for local-system storage
#
# To automate this archive script, as root user:
# 1. cp or mv this script (archiveLogs_1.1.2.pl) to /etc/bin/
# 2. Change the user and group ownership of this script to root, with chown, and
# set the permissions to allow read, write, and execute by the root user, with
# chmod.
# 3. Open /etc/crontab and add '1 0 * * *  root archiveLogs.pl', then save and close  # the file.
# 4. In 'yast runlevel', enable the cron service, so that it will automatically start in
# the event of a reboot. This will ensure the archival script remains automated.
#
########################################################################

# Designates and makes, if not already in existance, the target directory for archiving
# compressed log files
my $destination="/home/logArchive";
system "mkdir $destination";

#### .log Evaluation
# This opens the specified system directory and reads the files into the DIR filehandle,
# then finds the files ending in ".log" and places them into an array (a list). The contents
# of this array are then evaluated for size to determine whether or not they are large enough
# to archive
chdir "/tmp"
    or die "cannot chdir to ../tmp/: $!";
opendir DIR, "/tmp/";
my @logEntry = grep {/.+\.log$/} (readdir DIR);
foreach my $filename (@logEntry) {
        my $size = (stat($filename))[7];

    # Makes a copy of the orginal filename and then removes the extension,
    # while preserving the original. The truncated filename will be inserted
    # into the new archival name, once the file has been compressed
    my $filenameTrunc = $filename;
    $filenameTrunc =~ s/^(.+)\.log$/$1/;

        # Ensures that existing log files (ones created automatically by the system) have
        # the correct owner:group and permissions settings to grant the configured system
        # users writable access to the logs. This is generally only necessary before the log
        # file has grown large enough to be archived for the first time. After that, this
        # script will automatically replace the log files and configure the proper ownership
        # and permissions. The permissions will allow the ownwer to read and write to the file,
        # members of the same group to read and write to the file, and other users to only
        # read the file, but not write. This is based on a three-bit binary activation for
        # "rwx", with read = 4, write = 2 (r+w=6), and execute = 1.
        if ($size < 1_048_576) {
                system "chown ido.emf-fiscal.net:mercury $filename && chmod 664 $filename";
        }

    # EValuates the file size, then compresses and archives it if it is larger than 1MB.
    # If the file is smaller than 1MB, it leaves it alone.
    if ($size >= 1_048_576) {
        # Extracts the year, month, day, hour, minute, and second elements from the
        # localtime function, in that order, and formats the year to be four digits,
        # with every other element being two digits, with a preceding 0, if necessary.
        # This time stamp is then placed at the beginning of the new filename, when
        # the log file is compressed
        (my $Y, my $m, my $d, my $H, my $M, my $S) = (localtime)[5,4,3,2,1,0];
        my $timeStamp = sprintf
                "%4d%02d%02d%02d%02d%02d",
                ($Y+1900, $m+1, $d, $H, $M, $S);
           
                # Shorten the code by assigning a temporary filename for the original file to
        # $tmpFile. The original file will be moved to this temporary name, if it does
        # not exist, and the original file will be emptied for new entries. Compression
        # will then be performed on the new temp file
        my $tmpFile = "$timeStamp.$filenameTrunc.tmp";

        # Checks whether or not the temporary filename already exists. If not, it moves
        # the original file to the new filename, then empties the original log file.
        # The compression and renaming, for the log archive is performed on the new
        # temporary file, then the temporary file is deleted automatically. This prevents
        # the potential loss of data entries, which could occur if the compression were
        # performed on the original file at the same time as new data was being written.
        if (-e $tmpFile) {
            warn "Can't rename $filename to $tmpFile: $tmpFile already exists\n";
        } elsif (rename $filename, $tmpFile) {
            system "touch $filename && chown ido.emf-fiscal.net:mercury $filename && chmod 664 $filename";
            system "tar -czf $timeStamp.$filenameTrunc.tar.gz $tmpFile && echo $filename && rm $tmpFile";
            system "mv $timeStamp.$filenameTrunc.tar.gz $destination";
        } else {
            warn "rename $filename to $tmpFile failed: $!\n";
        }
        }
}

#### .out Evaluation
# This opens the specified system file directory and reads the files into the DIR filehandle,
# then finds the files ending in ".out" and places them into an array (a list). The contents
# of this array are then evaluated for size to determine whether or not they are large enough
# to archive
chdir "/usr/share/tomcat6/logs"
    or die "cannot chdir to ..tomcat6/logs/: $!";
opendir DIR, "/usr/share/tomcat6/logs/";
my @outFiles = grep {/.+\.out$/} (readdir DIR);
foreach my $filename (@outFiles) {
    my $size = (stat($filename))[7];
   
    # Makes a copy of the orginal filename and then removes the extension,
    # while preserving the original. The truncated filename will be inserted
    # into the new archival name, once the file has been compressed
    my $filenameTrunc = $filename;
    $filenameTrunc =~ s/^(.+)\.out$/$1/;

        # Ensures that existing log files (ones created automatically by the system) have
        # the correct owner:group and permissions settings to grant the configured system
        # users writable access to the logs. This is generally only necessary before the log
        # file has grown large enough to be archived for the first time. After that, this
        # script will automatically replace the log files and configure the proper ownership
        # and permissions. The permissions will allow the ownwer to read and write to the file,
        # members of the same group to read and write to the file, and other users to only
        # read the file, but not write. This is based on a three-bit binary activation for
        # "rwx", with read = 4, write = 2 (r+w=6), and execute = 1.
        if ($size < 1_048_576) {
                system "chown tomcat:tomcat $filename && chmod 664 $filename";
        }

    # Evaluates the file size, then compresses and archives it if it is larger than 1MB.
    # If the file is smaller than 1MB, it leaves it alone.
    if ($size >= 1_048_576) {
                # Extracts the year, month, day, hour, minute, and second elements from the
        # localtime function, in that order, and formats the year to be four digits,
        # with every other element being two digits, with a preceding 0, if necessary.
        # This time stamp is then placed at the beginning of the new filename, when
        # the log file is compressed
        (my $Y, my $m, my $d, my $H, my $M, my $S) = (localtime)[5,4,3,2,1,0];
        my $timeStamp = sprintf
                "%4d%02d%02d%02d%02d%02d",
                ($Y+1900, $m+1, $d, $H, $M, $S);
           
        # Shorten the code by assigning a temporary filename for the original file to
        # $tmpFile. The original file will be moved to this temporary name, if it does
        # not exist, and the original file will be emptied for new entries. Compression
        # will then be performed on the new temp file
        my $tmpFile = "$timeStamp.$filenameTrunc.tmp";

        # Checks whether or not the temporary filename already exists. If not, it moves
        # the original file to the new filename, then empties the original log file.
        # The compression and renaming, for the log archive is performed on the new
        # temporary file, then the temporary file is deleted automatically. This prevents
        # the potential loss of data entries, which could occur if the compression were
        # performed on the original file at the same time as new data was being written.
        if (-e $tmpFile) {
            warn "Can't rename $filename to $tmpFile: $tmpFile already exists\n";
        } elsif (rename $filename, $tmpFile) {
            system "touch $filename && chown tomcat:tomcat $filename && chmod 664 $filename";
            system "tar -czf $timeStamp.$filenameTrunc.tar.gz $tmpFile && echo $filename && rm $tmpFile";
            system "mv $timeStamp.$filenameTrunc.tar.gz $destination";
        } else {
            warn "rename $filename to $tmpFile failed: $!\n";
        }
    }
}

# Evaluate the age of files in the log archive and move those that are a year old to a new location
chdir "/home/logArchive"
        or die "cannot chdir to ..logArchive/: $!";
opendir DIR, "/home/logArchive/";
my @archiveFiles = grep {/.+\.out$/} (readdir DIR);
foreach my $filename (@archiveFiles) {
        my $size = (stat($filename))[7];
}

system "/etc/init.d/apache2 restart";
system "/etc/init.d/tomcat6 restart";


**************End Perl Script for Log Backup*************

No comments:

Post a Comment