| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: CloneCentralDatabases.pl,v 1.1 2008/02/21 00:21:28 kawas Exp $ | ||
| 5 | # $Source: /home/repository/moby/moby-live/Perl/MOBY-Server/share/scripts/CloneCentralDatabases.pl,v $ | ||
| 6 | # | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Getopt::Std; | ||
| 10 | use MOBY::Client::Central; | ||
| 11 | use MOBY::Config; | ||
| 12 | |||
| 13 | my $master_central; | ||
| 14 | |||
| 15 | # | ||
| 16 | # Split up text in 2 pieces to prevent CVS from filling in the id and source strings overhere. | ||
| 17 | # | ||
| 18 | my $cvs_id1 = '$I'; | ||
| 19 | my $cvs_id2 = 'd$'; | ||
| 20 | my $cvs_source1 = '$S'; | ||
| 21 | my $cvs_source2 = 'ource$'; | ||
| 22 | |||
| 23 | # | ||
| 24 | # Official MOBY Central. (Currently down) | ||
| 25 | # | ||
| 26 | my $master_central_uri = 'http://moby.ucalgary.ca/MOBY/Central'; | ||
| 27 | my $master_central_url = 'http://moby.ucalgary.ca/moby/MOBY-Central.pl'; | ||
| 28 | |||
| 29 | # | ||
| 30 | # Get options. | ||
| 31 | # | ||
| 32 | |||
| 33 | my %opts; | ||
| 34 | getopts('mc:p:', \%opts); | ||
| 35 | unless ($opts{'m'}) { | ||
| 36 | _Usage(); | ||
| 37 | } | ||
| 38 | unless ($opts{'p'}) { | ||
| 39 | print "\nPassword missing. Please specify -p [password]\n"; | ||
| 40 | _Usage(); | ||
| 41 | } | ||
| 42 | my $mysql_rootpwd = $opts{'p'}; | ||
| 43 | if ($opts{'c'}) { | ||
| 44 | ($master_central_uri, $master_central_url) = split '@', $opts{'c'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | _ExistsLocalCentralConfig(); | ||
| 48 | |||
| 49 | print "Fetching data from MOBY Central:\n\t$master_central_uri @ $master_central_url\n"; | ||
| 50 | print "to rebuild the local MOBY Central clone/mirror configuration.\n"; | ||
| 51 | |||
| 52 | # Set this to wherever your MOBY Central is. | ||
| 53 | $master_central = MOBY::Client::Central->new( | ||
| 54 | Registries => { | ||
| 55 | mobycentral => { | ||
| 56 | URL => $master_central_url, | ||
| 57 | URI => $master_central_uri | ||
| 58 | } | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | |||
| 62 | # | ||
| 63 | # Create or cleanup the SQL dump dir. | ||
| 64 | # | ||
| 65 | |||
| 66 | my $dumpdir = './sqldump/'; | ||
| 67 | |||
| 68 | if (-e $dumpdir && -d $dumpdir) { | ||
| 69 | |||
| 70 | print "SQL dump directory $dumpdir exists.\n"; | ||
| 71 | |||
| 72 | # | ||
| 73 | # Find old SQL dump files. | ||
| 74 | # | ||
| 75 | |||
| 76 | opendir (DUMPDIR, $dumpdir) or die "Couldn't open SQL dump directory: $!\n"; | ||
| 77 | my @sqlfiles = grep { /.+\.sql/i } readdir DUMPDIR; | ||
| 78 | closedir DUMPDIR; | ||
| 79 | |||
| 80 | # | ||
| 81 | # Remove old SQL dumps. | ||
| 82 | # | ||
| 83 | |||
| 84 | if (scalar(@sqlfiles)) { | ||
| 85 | |||
| 86 | print "Removing old SQL dumps...\n"; | ||
| 87 | |||
| 88 | foreach my $file (@sqlfiles) { | ||
| 89 | |||
| 90 | my $path = $dumpdir .+ $file; | ||
| 91 | print "\tRemoving $path..."; | ||
| 92 | unlink $path or die "\n\tCould not unlink $path: $!\n"; | ||
| 93 | print " done.\n"; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | } else { | ||
| 98 | |||
| 99 | print "Can not remove old SQL dumps: No old SQL dumps found.\n"; | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | } else { | ||
| 104 | |||
| 105 | print "Creating SQL dump directory $dumpdir..."; | ||
| 106 | mkdir $dumpdir,0755 or die "\n\tCouldn't create SQL dump directory $dumpdir: $!\n"; | ||
| 107 | print " done.\n"; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | # | ||
| 112 | # Fetch MOBY Central data. | ||
| 113 | # | ||
| 114 | |||
| 115 | print "MOBY Central Data Dump:\n"; | ||
| 116 | |||
| 117 | # A simple MOBY_Central call to get a complete dump of registered stuff. | ||
| 118 | my ($mobycentral, $mobyobject, $mobyservice, $mobynamespace, $mobyrelationship) = $master_central->MOBY::Client::Central::DUMP(); | ||
| 119 | #my ($mobycentral, $mobyobject, $mobyservice, $mobynamespace, $mobyrelationship) = $central->MOBY::Client::Central::DUMP(['mobycentral']); | ||
| 120 | |||
| 121 | _DumpSQL('mobycentral', $mobycentral); | ||
| 122 | _DumpSQL('mobyobject', $mobyobject); | ||
| 123 | _DumpSQL('mobyservice', $mobyservice); | ||
| 124 | _DumpSQL('mobynamespace', $mobynamespace); | ||
| 125 | _DumpSQL('mobyrelationship', $mobyrelationship); | ||
| 126 | |||
| 127 | # | ||
| 128 | # Create new MOBY Central. | ||
| 129 | # | ||
| 130 | |||
| 131 | print "Creating MOBY Central clone/mirror:\n"; | ||
| 132 | |||
| 133 | my $config = MOBY::Config->new(); | ||
| 134 | my @dbsections = ('mobycentral', 'mobyobject', 'mobyservice', 'mobynamespace', 'mobyrelationship'); | ||
| 135 | |||
| 136 | my $data; | ||
| 137 | |||
| 138 | $data .= "--\n"; | ||
| 139 | $data .= "-- $cvs_id1$cvs_id2\n"; | ||
| 140 | $data .= "-- $cvs_source1$cvs_source2\n"; | ||
| 141 | $data .= "--\n\n"; | ||
| 142 | |||
| 143 | foreach my $dbsection (@dbsections) { | ||
| 144 | |||
| 145 | my $dbname = ${${$config}{$dbsection}}{'dbname'}; | ||
| 146 | my $username = ${${$config}{$dbsection}}{'username'}; | ||
| 147 | my $password = ${${$config}{$dbsection}}{'password'}; | ||
| 148 | |||
| 149 | $data .= "DELETE FROM mysql.user WHERE user=\"$username\"\;\n"; | ||
| 150 | $data .= "DROP DATABASE IF EXISTS $dbname\;\n"; | ||
| 151 | $data .= "FLUSH PRIVILEGES\;\n"; | ||
| 152 | $data .= "CREATE DATABASE IF NOT EXISTS $dbname\;\n"; | ||
| 153 | $data .= "USE $dbname\;\n"; | ||
| 154 | $data .= "GRANT ALL PRIVILEGES ON $dbname.* TO \"$username\"\@\"localhost\" identified by \"$password\"\;\n"; | ||
| 155 | $data .= "FLUSH PRIVILEGES\;\n"; | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | $data .= "FLUSH PRIVILEGES\;\n"; | ||
| 160 | $data .= "FLUSH PRIVILEGES\;\n"; | ||
| 161 | $data .= "FLUSH PRIVILEGES\;\n"; | ||
| 162 | |||
| 163 | my $sqlfile = $dumpdir .+ 'createdatabases.sql'; | ||
| 164 | open (SQLSAVE,">>$sqlfile") or die "\tERROR: can't open output file $sqlfile: $!\n"; | ||
| 165 | print SQLSAVE $data or die "\tERROR: can't save output to file $sqlfile: $!\n"; | ||
| 166 | print "\tCreated script to create MySQL databases.\n"; | ||
| 167 | close SQLSAVE; | ||
| 168 | |||
| 169 | system("mysql -u root --password=$mysql_rootpwd < $sqlfile") == 0 or die "\n\tCan't create databases using sql file $sqlfile: $?\n"; | ||
| 170 | |||
| 171 | foreach my $dbsection (@dbsections) { | ||
| 172 | |||
| 173 | my $dbname = ${${$config}{$dbsection}}{'dbname'}; | ||
| 174 | my $username = ${${$config}{$dbsection}}{'username'}; | ||
| 175 | my $password = ${${$config}{$dbsection}}{'password'}; | ||
| 176 | my $host = ${${$config}{$dbsection}}{'url'}; | ||
| 177 | my $port = ${${$config}{$dbsection}}{'port'}; | ||
| 178 | |||
| 179 | _LoadSQL($dbsection, $dbname, $username, $password, $host, $port); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | print "Finished!\n"; | ||
| 184 | |||
| 185 | # | ||
| 186 | # Subs. | ||
| 187 | # | ||
| 188 | |||
| 189 | sub _DumpSQL { | ||
| 190 | my ($db, $data) = @_; | ||
| 191 | print "\tDumping $db..."; | ||
| 192 | my $pathto = $dumpdir .+ $db .+ '.sql'; | ||
| 193 | |||
| 194 | open (SQLSAVE,">>$pathto") or die "\n\tERROR: can't open output file $pathto: $!"; | ||
| 195 | print SQLSAVE "--\n" or die "\n\tERROR: can't save output to file $pathto: $!"; | ||
| 196 | print SQLSAVE "-- $cvs_id1$cvs_id2\n" or die "\n\tERROR: can't save output to file $pathto: $!"; | ||
| 197 | print SQLSAVE "-- $cvs_source1$cvs_source2\n" or die "\n\tERROR: can't save output to file $pathto: $!"; | ||
| 198 | print SQLSAVE "--\n\n" or die "\n\tERROR: can't save output to file $pathto: $!"; | ||
| 199 | print SQLSAVE $data or die "\n\tERROR: can't save output to file $pathto: $!"; | ||
| 200 | print " saved $db to $dumpdir\n"; | ||
| 201 | close SQLSAVE; | ||
| 202 | |||
| 203 | if (-z $pathto) { | ||
| 204 | die "\tERROR: database dump $pathto for $db is empty.\n"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _LoadSQL { | ||
| 209 | my ($dbsection, $dbname, $username, $password, $host, $port) = @_; | ||
| 210 | print "\tLoading data for $dbsection..."; | ||
| 211 | my $sqlfilepath = $dumpdir .+ $dbsection .+ '.sql'; | ||
| 212 | system("mysql -h $host -P $port -u $username --password=$password $dbname<$sqlfilepath") == 0 or die "\n\tCan't load data for $dbname: $!"; | ||
| 213 | print " done\n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _ExistsLocalCentralConfig { | ||
| 217 | |||
| 218 | my $confile = $ENV{MOBY_CENTRAL_CONFIG}; | ||
| 219 | |||
| 220 | unless ($confile) { | ||
| 221 | |||
| 222 | print "ERROR: MOBY_CENTRAL_CONFIG env var not set.\n\n"; | ||
| 223 | print " A MOBY Central is a combination of scripts accessed through a webserver\n"; | ||
| 224 | print " and some MySQL databases. This script will take care of the databases part\n"; | ||
| 225 | print " provided that you have already installed the scripts and configured your\n"; | ||
| 226 | print " webserver.\n"; | ||
| 227 | |||
| 228 | exit; | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | unless (-e $confile && -r $confile) { | ||
| 233 | |||
| 234 | exit "ERROR: MOBY_CENTRAL_CONFIG env var pointing to a missing or unreadable file.\n"; | ||
| 235 | |||
| 236 | } else { | ||
| 237 | |||
| 238 | print "Found MOBY Central config file for local mirror/clone at:\n\t$confile\n" | ||
| 239 | |||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | sub _Usage { | ||
| 244 | print "\n"; | ||
| 245 | print "Options: -m Mirror = clone = create a local MOBY Central mirror/clone from scratch.\n"; | ||
| 246 | print " -c [central] Central from which to fetch data. [central] is in format:\n"; | ||
| 247 | print " [centralURI]@[centralURL]\n"; | ||
| 248 | print " Default central is:\n"; | ||
| 249 | print " $master_central_uri\@$master_central_url\n"; | ||
| 250 | print " Edit this script to change the default MOBY Central.\n"; | ||
| 251 | print " -p [password] MySQL root password required for creating the MOBY Central databases.\n"; | ||
| 252 | print "\n"; | ||
| 253 | print "WARNING: This script will overwrite an existing local MOBY Central clone/mirror!\n"; | ||
| 254 | print " Note that the local MOBY Central config (SOAP endpoint) might point to\n"; | ||
| 255 | print " MySQL databases on a different machine.\n"; | ||
| 256 | print "\n"; | ||
| 257 | _ExistsLocalCentralConfig(); | ||
| 258 | print "Local MOBY Central config points to SQL databases:\n"; | ||
| 259 | |||
| 260 | my $config = MOBY::Config->new() or die "\tcan't find local MOBY Central config\n"; | ||
| 261 | my @dbsections = ('mobycentral', 'mobyobject', 'mobyservice', 'mobynamespace', 'mobyrelationship'); | ||
| 262 | |||
| 263 | foreach my $dbsection (@dbsections) { | ||
| 264 | |||
| 265 | my $dbname = ${${$config}{$dbsection}}{'dbname'}; | ||
| 266 | my $host = ${${$config}{$dbsection}}{'url'}; | ||
| 267 | my $port = ${${$config}{$dbsection}}{'port'}; | ||
| 268 | |||
| 269 | print "\tdbname: $dbname\n"; | ||
| 270 | print "\t host: $host\n"; | ||
| 271 | print "\t port: $port\n"; | ||
| 272 | |||
| 273 | } | ||
| 274 | |||
| 275 | print "\n"; | ||
| 276 | exit; | ||
| 277 | } |