Find archive log file by given sequence with perl

Links: http://www.dbatools.net/experience/find_log_by_sequence_in_perl.html

    I am writing some DBA scripts with perl, one of them is the archive log file related. I have to implement this feature for more works. The first version is implemented by UNIX find command.

#
# Get the full archived log name by sequence
#
sub findLogBySequence
{
    my ($logseq) = @_;
    my $logfile = "";

    if (length($logfile) == 0)
    {
        # Foloowing logic is for Oracle 9i (sid_%t_%s.arc)
        $logfile = `find /data*/arch -follow -name *_${logseq}.* | head -1`;
        chomp($logfile);
        # Foloowing logic is for Oracle 10g (sid_%t_%s_%r.arc)
        if (length($logfile) == 0)
        {
          $logfile = `find /data*/arch -follow -name *_${logseq}_* | head -1`;
          chomp($logfile);
        }
    }

    scalar $logfile;
}

    Later I feel that the find command will run as many times as archive log count, it's not so effective enough, then I rewrite it with the following, it elimated lots of executions of find command.

my $logfmt = "???";
#
# Get the full archived log name by sequence
#
sub findLogBySequence
{
    my ($logseq) = @_;
    my $logfile = "";

    #
    # if archive log pattern is not null
    # get the archive log file from pattern
    #
    if ($logfmt ne "???")
    {
        $logfile = $logfmt;
        $logfile =~ s/XXXXXX/${logseq}/g;
        if (! -e $logfile)
        {
          $logfile = "";
          $logfmt = "???";
        }
    }

    if (length($logfile) == 0)
    {
        # Foloowing logic is for Oracle 9i (sid_%t_%s.arc)
        $logfile = `find /data*/arch -follow -name *_${logseq}.* | head -1`;
        chomp($logfile);
        # Foloowing logic is for Oracle 10g (sid_%t_%s_%r.arc)
        if (length($logfile) == 0)
        {
          $logfile = `find /data*/arch -follow -name *_${logseq}_* | head -1`;
          chomp($logfile);
        }
    }

    # Store the archive log pattern
    if (length($logfile) and -e $logfile and $logfmt eq "???")
    {
        $logfmt = $logfile;
        $logfmt =~ s/_${logseq}\./_XXXXXX\./;
        $logfmt =~ s/_${logseq}_/_XXXXXX_/;
    }
    scalar $logfile;
}

    I will write more perl DBA scripts with it, for example, keep given gigabytes of archive logs.

Post a comment

« Previous | Main | Next »

Powered by
Movable Type 3.36