10 my ($starttime, $endtime);
12 &GetOptions
("copycluster","exportcluster","importcluster","createtypes","wipeschema","recompilepackages","help","targetname:s","targetpassword:s","targetservice:s","sourcename:s","sourcepassword:s","sourceservice:s" );
14 if ($::opt_help
|| !($::opt_copycluster
|| $::opt_exportcluster
|| $::opt_importcluster
|| $::opt_createtypes
|| $::opt_wipeschema
|| $::opt_recompilepackages
))
16 print "Usage: copy_cluster.pl [COMMAND] [OPTION]... \n";
17 print "Copy a cluster using Oracle export/import.\n\n";
18 print "******************* WARNING *******************.\n";
19 print "* This utility will delete all existing user *\n";
20 print "* objects in your schema *\n";
21 print "***********************************************.\n";
24 print " --help Display these options.\n";
25 print " --copycluster Perform the complete copy to user schema.\n";
27 print " --exportcluster Export cluster to file using direct path.\n";
28 print " --importcluster Import cluster from file using direct path.\n";
29 print " --createtypes Recreate oracle typedefs.\n";
30 print " --wipeschema Delete all objects from users schema.\n";
31 print " --recompilepackages Recompile user database packages.\n";
34 print " --targetname=NAME Specify the target schema name. (Defaults to \$USER.)\n";
35 print " --targetpassword=PASSWORD Specify the target password. (Defaults to \"changeme\".)\n";
36 print " --taregetservice=NAME Specify the target database service name. (Defaults to \"swodb\".)\n";
37 print " --sourcename=NAME Specify the source schema name.\n";
38 print " --sourcepassword=PASSWORD Specify the source password. (Defaults to \"changeme\".)\n";
39 print " --sourceservice=NAME Specify the source database service name. (Defaults to \"swodb\".)\n";
48 print "Start time:\t",scalar localtime($starttime),"\n\n";
52 if ($::opt_copycluster
) { ©cluster
; last SWITCH
; }
53 if ($::opt_exportcluster
) { &exportcluster
; last SWITCH
; }
54 if ($::opt_importcluster
) { &importcluster
; last SWITCH
; }
55 if ($::opt_createtypes
) { &createtypes
; last SWITCH
; }
56 if ($::opt_wipeschema
) { &wipeschema
; last SWITCH
; }
57 if ($::opt_recompilepackages
) { &recompilepackages
; last SWITCH
; }
58 print "Warning unknown option \n";
59 system("echo Warning unknown command > sqloutput.tmp" );
63 print "End time:\t",scalar localtime($endtime),"\n\n";
64 print "Elasped time:\t",($endtime - $starttime)," second(s)\n";
68 #=======================================================================
70 #run all the steps at once involved in copying the cluster
75 print "**** STARTING CLUSTER COPY ****\n\n";
77 # check if another export has already been started from parallel execution
78 # wait for up to 2 hours
83 while (($cnt > 0 ) && ($total < 120))
85 open (INFILE
,"ps -ef | grep exp | grep -v grep | wc -l |");
92 print "An export is already running thru another process ... waiting for it to complete!\n";
99 print "Export still running after two hours!\n Aborting copy cluster!\n";
104 $dumpfile = &username
($::opt_sourcename
).".dmp";
107 print "**** SKIPPING EXPORING CLUSTER BECAUSE EXPORT ALREADY EXISTS****\n\n";
111 print "**** EXPORING CLUSTER ****\n\n";
114 print "**** DROPPING ALL OBJECTS IN USER SCHEMA ****\n\n";
116 print "**** RECREATING ORACLE USER TYPES ****\n\n";
118 print "**** IMPORTING CLUSTER ****\n\n";
120 print "**** RECOMPILING USER PACKAGES ****\n\n";
122 print "**** CLUSTER COPY COMPLETE ****\n\n";
125 #-----------------------------------------------------------------------
127 #export cluster to file using direct path
130 if ($::opt_sourcename
eq "")
132 print "You must select a valid source schema!\n\n";
135 system ("exp ".&sourcelogin
." file=".&username
($::opt_sourcename
).".dmp owner=".&username
($::opt_sourcename
)." direct=y statistics=none > output.tmp");
139 #-----------------------------------------------------------------------
141 #import cluster from file using direct path
144 if ($::opt_sourcename
eq "" || $::opt_targetname
eq "")
146 print "You must select valid source and target schemas!\n\n";
150 $_ = $::opt_targetname
;
152 if (/gold/ || /GOLD/ || /publish/ || /PUBLISH/)
154 system ("imp ".&targetlogin
." file=".&username
($::opt_sourcename
).".dmp fromuser=".&username
($::opt_sourcename
)." touser=".&username
($::opt_targetname
)." statistics=none ignore=y grants=n >> output.tmp");
158 print "You may only import to a gold schema!\n\n";
163 #-----------------------------------------------------------------------
165 #wipe all objects in user schema
168 $_ = $::opt_targetname
;
170 if (/gold/ || /GOLD/ || /publish/ || /PUBLISH/)
172 system (&sqlplus
." @../../queries/drop_all_objects.sql >> output.tmp");
176 print "You may only drop objects in a gold schema!\n\n";
181 #-----------------------------------------------------------------------
183 #recompile invalid packages
184 sub recompilepackages
186 system ("./check_objects.sh ".&targetlogin
." >> output.tmp");
187 system (&sqlplus
." \@check_objects.sql >> output.tmp");
190 #-----------------------------------------------------------------------
192 # recreate Oracle typedefs after wipe since typedef can't be imported
195 open (CREATELIST
,"ls ../../schema/*.type|");
200 print "DEBUG: sqlplus command is:\n",&sqlplus
," < ",$_,"\n\n";
201 system (&sqlplus
." < $_ > /tmp/database_update.tmp");
202 &checkError
("/tmp/database_update.tmp");
207 #-----------------------------------------------------------------------
209 # Return oracle username string
213 $user =~ tr/A-Z/a-z/;
218 #-----------------------------------------------------------------------
220 # Return oracle password string
224 $pwd=$ENV{"DB_PASSWORD"} if ($pwd eq "");
225 $pwd="changeme" if ($pwd eq "");
230 #-----------------------------------------------------------------------
232 # Return oracle service string
236 $db="swodb" if ($db eq "");
241 #-----------------------------------------------------------------------
245 return &username
($::opt_targetname
)."/".&password
($::opt_targetpassword
)."\@".&service
($::opt_targetservice
);
248 #-----------------------------------------------------------------------
252 return &username
($::opt_sourcename
)."/".&password
($::opt_sourcepassword
)."\@".&service
($::opt_sourceservice
);
255 #-----------------------------------------------------------------------
257 # Return the sqlplus command string
260 print "DEBUG: targetlogin is:\n",&targetlogin
,"\n\n";
261 return "sqlplus ".&targetlogin
;
264 #-----------------------------------------------------------------------
268 my ($filename,$dontdie)=@_;
270 open (CHECKFILE
,$filename);
273 if (/ERROR/ || /created with compilation errors/ )
278 last if (/Disconnected from Oracle/);
282 #system("rm $filename");
288 system("rm -f $filename");