missing project/build files
[client-tools.git] / src / game / server / database / build / linux / copy_cluster2.pl
blobfadbea1a77e4f1d45fcc07dd13a715d9759529f1
1 #!/usr/bin/perl
3 use strict;
4 use Getopt::Long;
6 &main;
8 sub main
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";
22 print "\n";
23 print "Commands:\n";
24 print " --help Display these options.\n";
25 print " --copycluster Perform the complete copy to user schema.\n";
26 print "\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";
32 print "\n";
33 print "Options:\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";
41 print "\n";
43 exit;
46 $starttime = time();
48 print "Start time:\t",scalar localtime($starttime),"\n\n";
50 SWITCH: {
52 if ($::opt_copycluster) { &copycluster; 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" );
62 $endtime = time();
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
71 sub copycluster
73 my ($dumpfile);
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
80 my $cnt = 1;
81 my $total = 0;
83 while (($cnt > 0 ) && ($total < 120))
85 open (INFILE,"ps -ef | grep exp | grep -v grep | wc -l |");
86 $cnt = <INFILE>;
87 $cnt =~ s/\s+//;
88 close(INFILE);
90 if ( $cnt > 0 )
92 print "An export is already running thru another process ... waiting for it to complete!\n";
93 $total++;
94 sleep 60;
97 if ($cnt > 0)
99 print "Export still running after two hours!\n Aborting copy cluster!\n";
100 exit 1;
104 $dumpfile = &username($::opt_sourcename).".dmp";
105 if (-e $dumpfile)
107 print "**** SKIPPING EXPORING CLUSTER BECAUSE EXPORT ALREADY EXISTS****\n\n";
109 else
111 print "**** EXPORING CLUSTER ****\n\n";
112 &exportcluster;
114 print "**** DROPPING ALL OBJECTS IN USER SCHEMA ****\n\n";
115 &wipeschema;
116 print "**** RECREATING ORACLE USER TYPES ****\n\n";
117 &createtypes;
118 print "**** IMPORTING CLUSTER ****\n\n";
119 &importcluster;
120 print "**** RECOMPILING USER PACKAGES ****\n\n";
121 &recompilepackages;
122 print "**** CLUSTER COPY COMPLETE ****\n\n";
125 #-----------------------------------------------------------------------
127 #export cluster to file using direct path
128 sub exportcluster
130 if ($::opt_sourcename eq "")
132 print "You must select a valid source schema!\n\n";
133 die;
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
142 sub importcluster
144 if ($::opt_sourcename eq "" || $::opt_targetname eq "")
146 print "You must select valid source and target schemas!\n\n";
147 die;
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");
156 else
158 print "You may only import to a gold schema!\n\n";
159 die;
163 #-----------------------------------------------------------------------
165 #wipe all objects in user schema
166 sub wipeschema
168 $_ = $::opt_targetname;
170 if (/gold/ || /GOLD/ || /publish/ || /PUBLISH/)
172 system (&sqlplus." @../../queries/drop_all_objects.sql >> output.tmp");
174 else
176 print "You may only drop objects in a gold schema!\n\n";
177 die;
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
193 sub createtypes
195 open (CREATELIST,"ls ../../schema/*.type|");
196 while (<CREATELIST>)
198 print $_;
199 chop;
200 print "DEBUG: sqlplus command is:\n",&sqlplus," < ",$_,"\n\n";
201 system (&sqlplus." < $_ > /tmp/database_update.tmp");
202 &checkError("/tmp/database_update.tmp");
204 close (CREATELIST);
207 #-----------------------------------------------------------------------
209 # Return oracle username string
210 sub username
212 my ($user)=@_;
213 $user =~ tr/A-Z/a-z/;
215 return "$user";
218 #-----------------------------------------------------------------------
220 # Return oracle password string
221 sub password
223 my ($pwd)=@_;
224 $pwd=$ENV{"DB_PASSWORD"} if ($pwd eq "");
225 $pwd="changeme" if ($pwd eq "");
227 return "$pwd";
230 #-----------------------------------------------------------------------
232 # Return oracle service string
233 sub service
235 my ($db)=@_;
236 $db="swodb" if ($db eq "");
238 return "$db";
241 #-----------------------------------------------------------------------
243 sub targetlogin
245 return &username($::opt_targetname)."/".&password($::opt_targetpassword)."\@".&service($::opt_targetservice);
248 #-----------------------------------------------------------------------
250 sub sourcelogin
252 return &username($::opt_sourcename)."/".&password($::opt_sourcepassword)."\@".&service($::opt_sourceservice);
255 #-----------------------------------------------------------------------
257 # Return the sqlplus command string
258 sub sqlplus
260 print "DEBUG: targetlogin is:\n",&targetlogin,"\n\n";
261 return "sqlplus ".&targetlogin;
264 #-----------------------------------------------------------------------
266 sub checkError
268 my ($filename,$dontdie)=@_;
270 open (CHECKFILE,$filename);
271 while (<CHECKFILE>)
273 if (/ERROR/ || /created with compilation errors/ )
275 print;
276 while (<CHECKFILE>)
278 last if (/Disconnected from Oracle/);
279 print;
281 close CHECKFILE;
282 #system("rm $filename");
283 die unless $dontdie;
284 last;
287 close (CHECKFILE);
288 system("rm -f $filename");