export_tables.in 867 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#!/usr/bin/perl -w
#
# 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;

#
# Configure variables
#
use lib '@prefix@/lib';
use libdb;
my $DBNAME = '@TBDBNAME@';

my $mysqldump = "mysqldump";

if (!TBAdmin($UID)) {
    die("Only admins get to use this program\n");
}

#
# Build a list of tables to export
#
my $result = DBQueryFatal("select table_name from exported_tables");
my $table_names;
while (my ($table_name) = $result->fetchrow()) {
    $table_names .= "$table_name ";
}

#
# Run mysqldump, and read its stdout
#
open(FH,"$mysqldump -t $DBNAME $table_names |") or
    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;
    print;
}