1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#!/usr/bin/perl
#------------------------------------------------------------------------------
# Project : Oracle to Postgresql converter
# Name : ora2pg_scanner
# Author : Gilles Darold, gilles _AT_ darold _DOT_ net
# Copyright: Copyright (c) 2000-2025 : Gilles Darold - All rights reserved -
# Function : Script used to scan a list of DSN and generate reports
# Usage : ora2pg_scanner -l dsn_csv_file -o outdir
#------------------------------------------------------------------------------
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see < http://www.gnu.org/licenses/ >.
#
#------------------------------------------------------------------------------
use strict;
use Getopt::Long qw(:config no_ignore_case bundling);
my $VERSION = '25.0';
my @DB_DSN = ();
my $OUTDIR = '';
my $DRYRUN = 0;
my $INPUT_FILE = '';
my $CONF_FILE = '';
my $HELP = 0;
my $ORA2PG_CMD = 'ora2pg';
my $BINPATH = '';
my $SEP = ($^O =~ /MSWin32|dos/i) ? "\\" : "/";
my $COST_UNIT = 5;
my $FORMAT = 'html';
# Collect command line arguments
GetOptions
(
'c|config=s' => \$CONF_FILE,
'b|binpath=s' => \$BINPATH,
'f|format=s' => \$FORMAT,
'l|list=s' => \$INPUT_FILE,
't|test!' => \$DRYRUN,
'o|outdir=s' => \$OUTDIR,
'u|unit=s' => \$COST_UNIT,
'h|help!' => \$HELP,
);
$OUTDIR = 'output' if (!$OUTDIR);
if (!$INPUT_FILE || !-e $INPUT_FILE || $HELP)
{
usage();
}
$FORMAT = lc($FORMAT);
if ($FORMAT ne 'html' && $FORMAT ne 'json')
{
die "FATAL: output format for reports must be html or json\n";
}
if ($BINPATH)
{
$BINPATH =~ s/\Q$SEP\E$//;
if (-e "$BINPATH$SEP$ORA2PG_CMD")
{
$ORA2PG_CMD = "$BINPATH$SEP$ORA2PG_CMD";
}
else
{
die "FATAL: path to ora2pg binary must exists: $BINPATH$SEP$ORA2PG_CMD\n";
}
}
if ($^O =~ /MSWin32|dos/i)
{
$ORA2PG_CMD = "perl $ORA2PG_CMD";
}
open(IN, $INPUT_FILE) or die "FATAL: can not read file $INPUT_FILE, $!\n";
while (my $l = <IN>)
{
#"type","schema/database","dsn","user","password","audit users"
#"MYSQL","sakila","dbi:mysql:host=192.168.1.10;database=sakila;port=3306","root","mysecret"
#"ORACLE","HR","dbi:Oracle:host=192.168.1.10;sid=XE;port=1521","system","manager","hr;system;scott"
#"MSSQL","HR","dbi:ODBC:driver=msodbcsql18;server=srv.database.windows.net;database=testdb","usrname","pwd"
# skip header line
chomp($l);
$l =~ s/\r//;
next if ($l !~ /^["]*(MYSQL|ORACLE|MSSQL)["]*,/i);
$l =~ s/"//gs;
my @data = split(/,/, $l);
if ($#data < 4)
{
die "FATAL: wrong number of field at line: $l\n";
}
my ($type, $schema, $dsn, $user, $passwd, $audit_user) = split(/,/, $l);
push(@DB_DSN, { (
'type' => uc($type),
'schema' => $schema,
'dsn' => $dsn,
'user' => $user,
'pwd' => $passwd,
'audit_user' => $audit_user,
'sid' => '',
'host' => ''
)
}
);
}
close(IN);
# Create the output directory
if (!$DRYRUN)
{
if (!-d "$OUTDIR")
{
mkdir "$OUTDIR";
}
else
{
print "FATAL: output directory already exists, $OUTDIR.\n";
exit 1;
}
}
else
{
print "Performing connection test only by retrieving the requested schema or database.\n";
}
# Start to generate call to ora2pg
my $header = ' --print_header';
for (my $i = 0; $i < @DB_DSN; $i++)
{
$header = '' if ($i > 0);
$ENV{ORA2PG_USER} = $DB_DSN[$i]->{user};
$ENV{ORA2PG_PASSWD} = $DB_DSN[$i]->{pwd};
# Used to pass additional information to ora2pg command
my $info = '';
# Set RDBMS type
$info = ' -m' if ($DB_DSN[$i]->{type} eq 'MYSQL');
$info = ' -M' if ($DB_DSN[$i]->{type} eq 'MSSQL');
# Add custom configuration file if set
$info .= ' -c ' . $CONF_FILE if ($CONF_FILE);
my $cmd_ora2pg = $ORA2PG_CMD . $info;
my $audit = '';
$audit = " --audit_user \"$DB_DSN[$i]->{audit_user}\"" if ($DB_DSN[$i]->{audit_user});
# Extract SID or db name from the DSN
# dbi:Oracle:host=foobar;sid=ORCL;port=1521
# dbi:Oracle:DB
# dbi:Oracle://192.168.1.10:1521/XE
# DBI:mysql:database=$db;host=$host
if ($DB_DSN[$i]->{dsn} =~ m/(?:sid|database|service_name)=([^;]+)/ ||
$DB_DSN[$i]->{dsn} =~ m/dbi:Oracle:([\w]+)$/ ||
$DB_DSN[$i]->{dsn} =~ m/dbi:Oracle:\/\/[^\/]+\/([\w]+)/ )
{
$DB_DSN[$i]->{sid} = $1;
}
elsif (!$DB_DSN[$i]->{schema})
{
print "WARNING: couldn't determine sid/database name for DSN ". $DB_DSN[$i]->{dsn} .", without explicit schema can not processed this entry. Skiping.\n";
next;
}
else
{
$DB_DSN[$i]->{sid} = 'schema';
}
# Extract host
if ($DB_DSN[$i]->{dsn} =~ m/(?:host|server)=([^;]+)/ || $DB_DSN[$i]->{dsn} =~ m/dbi:Oracle:\/\/([^\/]+)/)
{
$DB_DSN[$i]->{host} = $1;
$DB_DSN[$i]->{host} =~ s/:\d$+//;
$DB_DSN[$i]->{host} .= '_';
}
# When no schema or database is set, let Ora2Pg autodetect the list of available schema
if ($DB_DSN[$i]->{schema} eq '')
{
if ($DRYRUN)
{
print "Running: $cmd_ora2pg -t SHOW_SCHEMA -s '$DB_DSN[$i]->{dsn}'\n";
print `$cmd_ora2pg -t SHOW_SCHEMA -s "$DB_DSN[$i]->{dsn}"`;
print "For each schema returned the following commands will be executed:\n";
print " $cmd_ora2pg -t SHOW_REPORT --dump_as_sheet --cost_unit_value $COST_UNIT --estimate_cost$header$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"<schema_returned>\" >> $OUTDIR${SEP}dbs_scan.csv\n";
print " $cmd_ora2pg -t SHOW_REPORT --dump_as_$FORMAT --cost_unit_value $COST_UNIT --estimate_cost$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"<schema_returned>\" >> \"$OUTDIR${SEP}$DB_DSN[$i]->{host}$DB_DSN[$i]->{sid}_<schema_returned>-report.$FORMAT\"\n";
}
else
{
my @schema_list = `$cmd_ora2pg -t SHOW_SCHEMA -s "$DB_DSN[$i]->{dsn}"`;
foreach my $line (@schema_list)
{
my ($type, $schemaname) = split(/\s+/, $line);
$DB_DSN[$i]->{schema} = $schemaname;
# Escape some chars for file path use
$schemaname = quotemeta($schemaname);
print "Running: $cmd_ora2pg -t SHOW_REPORT --dump_as_sheet --cost_unit_value $COST_UNIT --estimate_cost$header$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"$DB_DSN[$i]->{schema}\" >> $OUTDIR${SEP}dbs_scan.csv\n";
`$cmd_ora2pg -t SHOW_REPORT --dump_as_sheet --cost_unit_value $COST_UNIT --estimate_cost$header$audit -s "$DB_DSN[$i]->{dsn}" -n "$DB_DSN[$i]->{schema}" >> $OUTDIR${SEP}dbs_scan.csv`;
$header = '';
print "Running: $cmd_ora2pg -t SHOW_REPORT --dump_as_$FORMAT --cost_unit_value $COST_UNIT --estimate_cost$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"$DB_DSN[$i]->{schema}\" >> \"$OUTDIR${SEP}$DB_DSN[$i]->{host}$DB_DSN[$i]->{sid}_$schemaname-report.$FORMAT\"\n";
`$cmd_ora2pg -t SHOW_REPORT --dump_as_$FORMAT --cost_unit_value $COST_UNIT --estimate_cost$audit -s "$DB_DSN[$i]->{dsn}" -n "$DB_DSN[$i]->{schema}" >> "$OUTDIR${SEP}$DB_DSN[$i]->{host}$DB_DSN[$i]->{sid}_$schemaname-report.$FORMAT"`;
}
}
}
else
{
# Escape some chars for file path use
my $schemaname = quotemeta($DB_DSN[$i]->{schema});
if ($DRYRUN)
{
print "Running: $cmd_ora2pg -t SHOW_SCHEMA -s \"$DB_DSN[$i]->{dsn}\" -n \"$DB_DSN[$i]->{schema}\"\n";
print `$cmd_ora2pg -t SHOW_SCHEMA -s "$DB_DSN[$i]->{dsn}" -n "$DB_DSN[$i]->{schema}"`
}
else
{
print "Running: $cmd_ora2pg -t SHOW_REPORT --dump_as_sheet --cost_unit_value $COST_UNIT --estimate_cost$header$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"$DB_DSN[$i]->{schema}\" >> $OUTDIR${SEP}dbs_scan.csv\n";
`$cmd_ora2pg -t SHOW_REPORT --dump_as_sheet --cost_unit_value $COST_UNIT --estimate_cost$header$audit -s "$DB_DSN[$i]->{dsn}" -n "$DB_DSN[$i]->{schema}" >> $OUTDIR${SEP}dbs_scan.csv`;
print "Running: $cmd_ora2pg -t SHOW_REPORT --dump_as_$FORMAT --cost_unit_value $COST_UNIT --estimate_cost$audit -s \"$DB_DSN[$i]->{dsn}\" -n \"$DB_DSN[$i]->{schema}\" >> \"$OUTDIR${SEP}$DB_DSN[$i]->{sid}_$schemaname-report.$FORMAT\"\n";
`$cmd_ora2pg -t SHOW_REPORT --dump_as_$FORMAT --cost_unit_value $COST_UNIT --estimate_cost$audit -s "$DB_DSN[$i]->{dsn}" -n "$DB_DSN[$i]->{schema}" >> "$OUTDIR${SEP}$DB_DSN[$i]->{sid}_$schemaname-report.$FORMAT"`;
}
}
}
exit 0;
sub usage
{
my $msg = shift;
print "$msg\n" if ($msg);
print qq{
Usage: ora2pg_scanner -l CSVFILE [-o OUTDIR]
-b | --binpath DIR: full path to directory where the ora2pg binary stays.
Might be useful only on Windows OS.
-c | --config FILE: set custom configuration file to use otherwise ora2pg
will use the default: /etc/ora2pg/ora2pg.conf.
-f | --format FMT: set the output format for the reports. Can be html or
json. Default to html.
-l | --list FILE : CSV file containing a list of databases to scan with
all required information. The first line of the file
can contain the following header that describes the
format that must be used:
"type","schema/database","dsn","user","password"
-o | --outdir DIR : (optional) by default all reports will be dumped to a
directory named 'output', it will be created automatically.
If you want to change the name of this directory, set the name
at second argument.
-t | --test : just try all connections by retrieving the required schema
or database name. Useful to validate your CSV list file.
-u | --unit MIN : redefine globally the migration cost unit value in minutes.
Default is taken from the ora2pg.conf (default 5 minutes).
Here is a full example of a CSV databases list file:
"type","schema/database","dsn","user","password"
"MYSQL","sakila","dbi:mysql:host=192.168.1.10;database=sakila;port=3306","root","secret"
"ORACLE","HR","dbi:Oracle:host=192.168.1.10;sid=XE;port=1521","system","manager"
"MSSQL","HR","dbi:ODBC:driver=msodbcsql18;server=srv.database.windows.net;database=testdb","system","manager"
The CSV field separator must be a comma.
Note that if you want to scan all schemas from an Oracle instance you just
have to leave the schema field empty, Ora2Pg will automatically detect all
available schemas and generate a report for each one. Of course you need to
use a connection user with enough privileges to be able to scan all schemas.
For example:
"ORACLE","","dbi:Oracle:host=192.168.1.10;sid=XE;port=1521","system","manager"
"MSSQL","","dbi:ODBC:driver=msodbcsql18;server=srv.database.windows.net;database=testdb","system","manager"
will generate a report for all schema in the XE instance. Note that in this
case the SCHEMA directive in ora2pg.conf must not be set.
};
exit 1;
}
|