A script to patch oracle binary

This is a simple script to patch oracle binary in perl.

#!/usr/bin/perl
#
#Unzip patch file, and run opatch to patch oracle binary

use strict;
use Cwd            qw( abs_path );
use File::Basename qw( dirname );

my $ORACLE_BASE = "/export/home/oracle/products";
my $HOME = "/export/home/oracle";

sub do_help {
  #--------------------------------------------------------------------------
  # Give help screen and exit
  #--------------------------------------------------------------------------
  print "Usage  : $0 <patch_file> \n";
  print "Example: $0 /patch/p12615660_112030_SOLARIS64.zip\n";
  exit 1;
}

#check input
my $patch_file = shift;
if(!(-r $patch_file)) {
  print "The file $patch_file doesn't exist\n";
  &do_help;
}
my $pf_name = `basename $patch_file`;
   chop($pf_name);
my $pf_dir = dirname(abs_path($patch_file));
my $ora_ver;
my $patch_num;
if( $pf_name =~ /p(\d+)_(\d+)_SOLARIS64.zip/ ) {
  $patch_num = $1;
  $ora_ver = $2;
  $ora_ver = substr($ora_ver, 0, 5);
} else {
  print "Cannot resolve patch file $pf_name, should be like p14592214_112033_SOLARIS64.zip\n";
  &do_help;
}

#set the environment
my $ORACLE_HOME       = "$ORACLE_BASE/$ora_ver";
$ENV{ORACLE_HOME}     = $ORACLE_HOME;
$ENV{LD_LIBRARY_PATH} = "$ORACLE_HOME/lib";
$ENV{PATH}            = "$ORACLE_HOME/OPatch:$ORACLE_HOME/bin:$HOME/bin:/usr/local/bin:/usr/ccs/bin:/usr/ucb:/usr/bin:/usr/sbin:/usr/openwin/bin";

&print_titile("Environment");
print "patch file name: $pf_name\n";
print "patch file dir : $pf_dir\n";
print "patch number   : $patch_num\n";
print "oracle version : $ora_ver\n";
print "oracle home    : $ORACLE_HOME\n";

#unzip the patch file
&print_titile("Unzip the patch file");
my $re;
if( -d "$pf_dir/$patch_num" ) {
  print "$pf_dir/$patch_num already exists, skip unzip...\n";
} else {
  $re=system("cd $pf_dir; unzip $pf_name");
  if($re != 0) {
     print "Unzip $pf_dir/$pf_name failed. exit...\n";
     exit;
  }
}

#patch the binary
&print_titile("Run opatch apply");
$re = system("cd $pf_dir/$patch_num; opatch apply");
if($re == 0) {
  print "It is done. Run opatch lsinventory\n";
  system("opatch lsinventory");
  my $lsinventory=`ls -t $ORACLE_HOME/cfgtoollogs/opatch/lsinv/lsinventory* | head -1`;
  print "\nLatest lsinventory file : $lsinventory\n";
} else {
  print "Looks like somehting is wrong, please fix it before run again. \n";
}
print "Please read $pf_dir/$patch_num/README.txt , and do necessary as needed\n";

sub print_titile {
  my ($line) = @_;
  print "\n==$line==\n\n";
}
Advertisements

About Alex Zeng
I would be very happy if this blog can help you. I appreciate every honest comments. Please forgive me if I'm too busy to reply your comments in time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: