Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> perl script for export
I'd like to offer my perl script for doing exports. Its been in production for a month and works quite well for me. Hope some find it usefull.
# --------------------------------------------------------------------
# this perl script will do a full export of an Oracle database
# to a specified directory, naming the export file export_SID_DATE.dat
# --------------------------------------------------------------------
use Time::localtime; use File::stat; use File::copy;
# Configure system parameters here
# --------------------------------
my $success_line = 'Export terminated successfully without warnings.'; my $userid = "sys"; my $userpassword = "smr2pass"; my $servicename = "mp2"; my $export_dir = "e:\\backup\\exports"; #no trailing backslash my $keepdays = 4; my $test = 'FALSE';
# first, we'll build the filename for our export.
# this logic will work properly after Y2k.
# -----------------------------------------------
my $filename = sprintf("%s\\full_%s_%4.4d_%2.2d_%2.2d_%2.2d_%2.2d", $export_dir, $servicename, localtime->year() + 1900, localtime->mon() + 1, localtime->mday(),
printf("\n\nDumping database %s to %s.dmp\n\n",
$servicename,
$filename) ;
# build the command line
# ----------------------
my $cmd = sprintf("exp73 %s/%s@%s buffer=500000 file=%s.dmp log=%s.log
compress=N full=Y consistent=y direct=y %s",
$userid, $userpassword, $servicename, $filename, $filename);
# run the export
# --------------
if ($test eq 'TRUE')
{
print "Command I would have run if not in test mode = '$cmd'.\n\n";
#need to create the logfile
my $logfile = $filename . ".log" ;
open(LOG_FILE, "> $logfile") or error_handler("could not open the
file logfile") ;
print LOG_FILE "line 1\n" ; print LOG_FILE "line 2\n" ; print LOG_FILE "line 3\n" ; print LOG_FILE "$success_line\n" ; close(LOG_FILE);
system($cmd) && error_handler("Error executing $cmd"); }
# did the export log file get created?
# ------------------------------------
my $logfile = $filename . ".log" ;
if (! -e $logfile)
{
error_handler("Logfile $logfile does not exist.") ; }
# now copy overwrite the MP2.DAT file in the export directory with
# the export we just created. This file will be used by an import
# job on another server.
# ----------------------------------------------------------------
my $newdumpfile = $export_dir . "\\mp2.dmp" ;
my $olddumpfile = $filename . ".dmp";
unlink $newdumpfile;
if ( -e $newdumpfile)
{
error_handler("Logfile $newdumpfile exists and it should not.") ; }
File::Copy::copy($olddumpfile, $newdumpfile) ;
if (! -e $newdumpfile)
{
error_handler("Logfile $newdumpfile does not exist.") ; }
# read throught the log file and look for the
# string that says that the export terminated sucessfully
my $found = 0;
my $line;
open(LF, $logfile)
or error_handler("cannot open file $!");
while (<LF>)
{
$line = $_; # stuff $line with the line we just read $line =~ s/^\s+//; # get rid of control chars $line =~ s/\s+$//; # get rid of spacesif ($line eq $success_line) {$found = 1;} }
close(LF);
if ($found == 0)
{
# bail out here and do not perform the erase old files step.
error_handler("could not find '$success_line' in $logfile\n");
}
else { print "Export Successful.\n\n" ; }
# ---------------------------------------------------------------
# finally, work through the backup directory and erase files that
# are over $KEEP_DAYS old.
# ---------------------------------------------------------------
# read file names into array @filenames, exclude . & ..
opendir(EXPDIR,"$export_dir")
or error_handler("unable to open backup directory for reading.
$!\n");
my @filenames = grep (!/^\.\.?$/, readdir (EXPDIR));
closedir EXPDIR;
# check modification dates against $time_limit.
my $file;
foreach $file (@filenames) # work through the array of names
{
if (-M "$export_dir/$file" > $keepdays) {
print "need to delete $export_dir/$file\n"; unlink "$export_dir/$file";
# All done
# --------
print "Exiting normally.\n\n" ;
exit;
sub error_handler {
# need to see if we can come up with something more
# intelligent to do here, like notify someone via email.
die "\n$_[0]\n" ;
}
Sent via Deja.com http://www.deja.com/
Before you buy.
Received on Thu Nov 11 1999 - 10:37:42 CST