0

I'm using the following script to upload dump files onto Amazon Oracle RDS instance. This process is well documented in books. However after some work done on the database I need to get the dump back. Unfortunately this side of the task is not documented at all. Amazon does not plan his customer to leave RDS in an easy way.

Please help to convert this script to do a reverse job, namely to get files FROM Amazon Oracle RDS save it TO my workstation. The dump file has been prepared on RDS using Oracle's DBMS_DATAPUMP function.

use DBI;
use warnings;
use strict;

# RDS instance info
my $RDS_PORT  = 1521;
my $RDS_HOST  = "blabla12345.us-west-2.rds.amazonaws.com";
my $RDS_LOGIN = "username/123456";
my $RDS_SID   = "ORCL";

#The $ARGV[0] is a parameter you pass into the script
my $dirname = "DATA_PUMP_DIR";
my $fname   = $ARGV[0];

my $data  = "dummy";
my $chunk = 8192;

my $sql_open   = "BEGIN perl_global.fh := utl_file.fopen(:dirname, :fname, 'wb', :chunk); END;";
my $sql_write  = "BEGIN utl_file.put_raw(perl_global.fh, :data, true); END;";
my $sql_close  = "BEGIN utl_file.fclose(perl_global.fh); END;";
my $sql_global = "create or replace package perl_global as fh utl_file.file_type; end;";

my $conn = DBI->connect(
    'dbi:Oracle:host=' . $RDS_HOST . ';sid=' . $RDS_SID . ';port=' . $RDS_PORT,
    $RDS_LOGIN,
    ''
) || die($DBI::errstr . "\n");

my $updated = $conn->do($sql_global);
my $stmt    = $conn->prepare($sql_open);
$stmt->bind_param_inout(":dirname", \$dirname, 12);
$stmt->bind_param_inout(":fname",   \$fname,   12);
$stmt->bind_param_inout(":chunk",   \$chunk,   4);
$stmt->execute() || die($DBI::errstr . "\n");

open(INF, $fname) || die "\nCan't open $fname for reading: $!\n";
binmode(INF);
$stmt = $conn->prepare($sql_write);
my %attrib = ('ora_type', '24');
my $val = 1;
while ( $val > 0 ) {
    $val = read(INF, $data, $chunk);
    $stmt->bind_param(":data", $data, \%attrib);
    $stmt->execute() || die($DBI::errstr . "\n");
}
die "Problem copying: $!\n" if $!;
close INF || die "Can't close $fname: $!\n";
$stmt = $conn->prepare($sql_close);
$stmt->execute() || die($DBI::errstr . "\n");

2 Answers 2

1

I found myself in the exact same situation. I'm no Perl programmer and flipping this script around was just too much for me. Thankfully I found a java program to retrieve files from RDS at http://wordpress.filefoundry.co.uk/update. The author was even kind enough to provide the additional information below. This worked great for me!

You'll need to do a couple of things to compile and run the program.

First, get hold of the Java JDK from Oracle so you can compile the software. Then run the software, which you can also do using the JDK.

If you already have an Oracle installation on your server, chances are there already is a version of the JDK installed somewhere. Otherwise, it's a free download.

The compiler is the javac command - exactly like the C# compiler csc.exe. It generates .class files from .java files, just like csc generates ..exe files from .cs files.

On your windows server, to compile the program the command line is javac DataPumpDownloader.java

It should generate a .class file in the current directory.

Then, to run the program, the command line is java -cp .;ojdbc6.jar DataPumpDownloader host port service username password filename

You may need to adjust the paths of the above depending on where you have installed the JDK and the Oracle JDBC driver. You don't need anything else apart from the JDK and the Oracle JDBC driver to make it work.

1
0

I't not perfect, but works

# RDS instance info

use DBI;

# Autoflush
$|=1;

$datestring = gmtime();
print "$datestring\n";

my $RDS_PORT=1521;
my $RDS_HOST="orcl.XXXXXXXXXXXXXXXX.eu-west-1.rds.amazonaws.com";
my $RDS_LOGIN="root/XXXXXXXXXXXXX";
my $RDS_SID="ORCL";

my $dirname = "DATA_PUMP_DIR";
my $fname = $ARGV[0];
my $data = "dummy";
my $chunk = 32767;

my $sql_open = "BEGIN perl_global.fh := utl_file.fopen(:dirname, :fname, 'rb', :chunk); END;";
my $sql_read = "BEGIN utl_file.get_raw(perl_global.fh, :data, NULL); END;";
my $sql_close = "BEGIN utl_file.fclose(perl_global.fh); END;";
my $sql_global = "create or replace package perl_global as fh utl_file.file_type; end;";
my $conn = DBI->connect('dbi:Oracle:host='.$RDS_HOST.';sid='.$RDS_SID.';port='.$RDS_PORT,$RDS_LOGIN, '') || die ( $DBI::errstr .  "\n");
my $updated=$conn->do($sql_global);

my $stmt = $conn->prepare ($sql_open);
$stmt->bind_param_inout(":dirname", \$dirname, 12);
$stmt->bind_param_inout(":fname", \$fname, 12);
$stmt->bind_param_inout(":chunk", \$chunk, 4);
$stmt->execute() || die ( $DBI::errstr . "\n");

open (INF, ">:raw" , $fname) || die "\nCan't open $fname for writing: $!\n";
binmode(INF);

$stmt = $conn->prepare ($sql_read);
my %attrib = ('ora_type','24');
my $val=1;
while ($val > 0) {
 $stmt->bind_param_inout(":data", \$data , 32768, \%attrib);
 $stmt->execute() || die ( $DBI::errstr . "\n");
 print INF $data;
 print ".";
};
print "\n";

die "Problem copying: $!\n" if $!;
close INF || die "Can't close $fname: $!\n";
$stmt = $conn->prepare ($sql_close);
$stmt->execute() || die ( $DBI::errstr . "\n");

$datestring = gmtime();
print "$datestring\n";

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.