From 2434516a9556473d74890f791d98ce7c5d36b6ff Mon Sep 17 00:00:00 2001
From: Robert Ricci <ricci@cs.utah.edu>
Date: Mon, 1 Jul 2002 20:15:52 +0000
Subject: [PATCH] New script: export_tables . Simply spits to stdout a set of
 SQL commands to fill a few tables that should be common across most sites, so
 that this data can be distributed to them.

The simple single-column table exported_tables controls which tables
are output.
---
 configure               |  4 ++--
 configure.in            |  2 +-
 sql/database-create.sql |  9 +++++++++
 utils/GNUmakefile.in    |  2 +-
 utils/export_tables.in  | 44 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 57 insertions(+), 4 deletions(-)
 create mode 100755 utils/export_tables.in

diff --git a/configure b/configure
index 1a0b8d7eae..b8b10680b0 100755
--- a/configure
+++ b/configure
@@ -1124,7 +1124,7 @@ fi
 # SVR4 /usr/ucb/install, which tries to use the nonexistent group "staff"
 # ./install, which can be erroneously created by make from ./install.sh.
 echo $ac_n "checking for a BSD compatible install""... $ac_c" 1>&6
-echo "configure:1127: checking for a BSD compatible install" >&5
+echo "configure:1128: checking for a BSD compatible install" >&5
 if test -z "$INSTALL"; then
 if eval "test \"`echo '$''{'ac_cv_path_install'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -1237,7 +1237,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
 	tmcd/tmcd.restart \
 	utils/GNUmakefile utils/vlandiff utils/vlansync utils/delay_config \
 	utils/sshtb utils/create_image utils/node_admin utils/webcreateimage \
-	utils/firstuser \
+	utils/firstuser utils/export_tables\
 	www/GNUmakefile www/defs.php3 www/dbdefs.php3 \
 	vis/GNUmakefile vis/vistopology vis/webvistopology vis/top2gif \
 	vis/dbvistopology vis/dbtopper \
diff --git a/configure.in b/configure.in
index 2367f2d3f1..7b47375979 100755
--- a/configure.in
+++ b/configure.in
@@ -309,7 +309,7 @@ outfiles="$outfiles Makeconf GNUmakefile \
 	tmcd/tmcd.restart \
 	utils/GNUmakefile utils/vlandiff utils/vlansync utils/delay_config \
 	utils/sshtb utils/create_image utils/node_admin utils/webcreateimage \
-	utils/firstuser \
+	utils/firstuser utils/export_tables\
 	www/GNUmakefile www/defs.php3 www/dbdefs.php3 \
 	vis/GNUmakefile vis/vistopology vis/webvistopology vis/top2gif \
 	vis/dbvistopology vis/dbtopper \
diff --git a/sql/database-create.sql b/sql/database-create.sql
index 1aded4da9e..f4adccfa36 100644
--- a/sql/database-create.sql
+++ b/sql/database-create.sql
@@ -188,6 +188,15 @@ CREATE TABLE experiments (
   PRIMARY KEY  (eid,pid)
 ) TYPE=MyISAM;
 
+#
+# Table structure for table 'exported_tables'
+#
+
+CREATE TABLE exported_tables (
+  table_name varchar(64) NOT NULL default '',
+  PRIMARY KEY  (table_name)
+) TYPE=MyISAM;
+
 #
 # Table structure for table 'exppid_access'
 #
diff --git a/utils/GNUmakefile.in b/utils/GNUmakefile.in
index de7e2cb971..c61315fda6 100644
--- a/utils/GNUmakefile.in
+++ b/utils/GNUmakefile.in
@@ -10,7 +10,7 @@ UNIFIED         = @UNIFIED_BOSS_AND_OPS@
 include $(OBJDIR)/Makeconf
 
 BIN_SCRIPTS	= delay_config sshtb create_image node_admin
-SBIN_SCRIPTS	= vlandiff vlansync withadminprivs
+SBIN_SCRIPTS	= vlandiff vlansync withadminprivs export_tables
 LIBEXEC_SCRIPTS	= webcreateimage
 
 #
diff --git a/utils/export_tables.in b/utils/export_tables.in
new file mode 100755
index 0000000000..24453a5050
--- /dev/null
+++ b/utils/export_tables.in
@@ -0,0 +1,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;
+}
-- 
GitLab