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

#
4
# Copyright (c) 2000-2017 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 44 45 46 47 48 49 50 51 52 53 54 55 56
my $extraopts  = "";
my $mysqlvers = `mysql -V`;
if ($mysqlvers =~ /Distrib (\d\.\d)/) {
    my $vers = $1 + 0.0;
    if ($vers >= 5.0) {
	$extraopts .= "--skip-opt ";
    }
    if ($vers >= 5.7) {
	$extraopts .= "--set-gtid-purged=off ";
    }
}
else {
    fatal("Could not determine mysql version");
}
57
my $mysqldump = "mysqldump $extraopts";
58

59
#
60
# We use the bare module instead of the testbed libdb, so that this
61 62 63
# script can run from a first-time installation, before the latter is
# installed.
#
64
use DBI;
65

66 67 68
#
# Build a list of tables to export
#
69 70 71
my $dbh = DBI->connect("DBI:mysql:database=$DBNAME;host=localhost",
		       undef, "none",
		       {'PrintError' => 0});
72 73 74 75
if (!$dbh) {
    die("Unable to connect to mysqld - Either it's not running or \n" .
    	  "permission was denied.\n");
}
76 77
my $sth = $dbh->prepare("select table_name from exported_tables");
$sth->execute();
78
my $table_names;
79
while (my ($table_name) = $sth->fetchrow()) {
80 81 82 83 84 85
    $table_names .= "$table_name ";
}

#
# Run mysqldump, and read its stdout
#
86 87
open(FH, "$mysqldump " .
     ($dist ? "--add-locks" : "") . " -t $DBNAME $table_names |") or
88 89 90 91 92 93 94 95
    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;
96 97 98 99 100 101 102
    #
    # 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; }

103
    print;
104 105

    if ($dist && /LOCK TABLES ([-\w]*) WRITE/i) {
106 107
	print "DELETE FROM $1;\n"
	    if ($1 ne "state_triggers");
108
    }
109
}