export_tables.in 2.49 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3

#
4
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29 30 31 32
#
# export_tables - Create an SQL file with the contents of selected tables 
# that should more or less be the same across all sites.
#

use English;

33 34 35 36 37
my $dist = 0;
if (@ARGV && $ARGV[0] eq "-i") {
    $dist = 1;
}

38 39 40 41 42
#
# Configure variables
#
my $DBNAME = '@TBDBNAME@';

43
my $isvers5   = system("mysql -V | egrep -q -s 'Distrib 5.'") == 0;
44 45
my $extraopts = ($isvers5 ? "--skip-opt" : "");
my $mysqldump = "mysqldump $extraopts";
46

47
#
48
# We use the bare module instead of the testbed libdb, so that this
49 50 51
# script can run from a first-time installation, before the latter is
# installed.
#
52
use DBI;
53

54 55 56
#
# Build a list of tables to export
#
57 58 59
my $dbh = DBI->connect("DBI:mysql:database=$DBNAME;host=localhost",
		       undef, "none",
		       {'PrintError' => 0});
60 61 62 63
if (!$dbh) {
    die("Unable to connect to mysqld - Either it's not running or \n" .
    	  "permission was denied.\n");
}
64 65
my $sth = $dbh->prepare("select table_name from exported_tables");
$sth->execute();
66
my $table_names;
67
while (my ($table_name) = $sth->fetchrow()) {
68 69 70 71 72 73
    $table_names .= "$table_name ";
}

#
# Run mysqldump, and read its stdout
#
74 75
open(FH, "$mysqldump " .
     ($dist ? "--add-locks" : "") . " -t $DBNAME $table_names |") or
76 77 78 79 80 81 82 83
    die "Unable to run $mysqldump: $!\n";

#
# Change INSERT to REPLACE, so that the resulting SQL can be used to update
# existing databases.
#
while (<FH>) {
    s/^INSERT/REPLACE/g;
84 85 86 87 88 89 90
    #
    # Kinda hackish, but gets the job done...
    #
    # Don't put any temporary entries from state_triggers out
    if (/INTO state_triggers VALUES \(\'([^\']*)\',/i &&
	$1 ne '*') { next; }

91
    print;
92 93

    if ($dist && /LOCK TABLES ([-\w]*) WRITE/i) {
94 95
	print "DELETE FROM $1;\n"
	    if ($1 ne "state_triggers");
96
    }
97
}