Add simple script to allow for exporting database structure.
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 28 Sep 2010 00:35:32 +0000 (02:35 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Thu, 30 Sep 2010 20:37:11 +0000 (22:37 +0200)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
bin/export_sql.bash [new file with mode: 0755]

diff --git a/bin/export_sql.bash b/bin/export_sql.bash
new file mode 100755 (executable)
index 0000000..ceefc73
--- /dev/null
@@ -0,0 +1,156 @@
+#!/bin/bash
+
+# This script is used to export the MySQL structure of the plat/al MySQL
+# databases.
+
+
+# Developers: list 'public' tables here.
+SHARED_TABLES="account_types
+forums
+geoloc_administrative_areas
+geoloc_countries
+geoloc_localities
+geoloc_subadministrativeareas
+log_actions
+newsletter_cat
+profile_binet_enum
+profile_corps_enum
+profile_corps_rank_enum
+profile_education_degree_enum
+profile_education_enum
+profile_education_field_enum
+profile_job_alternate
+profile_job_entreprise_term
+profile_job_enum
+profile_job_sector_enum
+profile_job_subsector_enum
+profile_job_subsubsector_enum
+profile_job_term_enum
+profile_job_term_relation
+profile_langskills_enum
+profile_medal_enum
+profile_medal_grade_enum
+profile_name_enum
+profile_networking_enum
+profile_section_enum
+profile_skill_enum
+reminder_type
+skin"
+
+usage()
+{
+cat << EOF
+usage: $0 OPTIONS
+
+Will dump the necessary data from MySQL.
+Note that all options should have sane defaults if you have correctly configured
+your .my.cnf file.
+
+OPTIONS:
+-h          Show this message
+-H HOST     MySQL host to connect to (default: none)
+-P PORT     MySQL port to connect to (default: none)
+-u USER     User for MySQL (default: none)
+-p PASS     MySQL password to use (default: none)
+-d SQL_DB   Database to read from (default: x5dat)
+-n          Dry run, don't actually write anything
+-o FILE     Write to file instead of stdout (- for stdout as well)
+
+EOF
+}
+
+HOST=
+PASS=
+PORT=
+USER=
+SQL_DB="x5dat"
+FILE="-"
+DRY_RUN=0
+
+while getopts "hns:u:p:o:" OPTION
+do
+    case $OPTION in
+      h)
+        usage
+        exit 1
+        ;;
+      n)
+        DRY_RUN=1
+        ;;
+      o)
+        FILE=$OPTARG
+        ;;
+      p)
+        PASS=$OPTARG
+        ;;
+      H)
+        HOST=$OPTARG
+        ;;
+      P)
+        PORT=$OPTARG
+        ;;
+      u)
+        USER=$OPTARG
+        ;;
+      d)
+        SQL_DB=$OPTARG
+        ;;
+      ?)
+        usage
+        exit
+        ;;
+    esac
+done
+
+
+DUMPER="mysqldump --add-drop-table --default-character-set=utf8 --force"
+
+if [ -n "$USER" ]; then
+    DUMPER="$DUMPER --user=$USER"
+fi
+
+if [ -n "$PASS" ]; then
+    DUMPER="$DUMPER --password=$PASS"
+fi
+
+if [ -n "$HOST" ]; then
+    DUMPER="$DUMPER --host=$HOST"
+fi
+
+if [ -n "$PORT" ]; then
+    DUMPER="$DUMPER --port=$PORT"
+fi
+
+if [ "$FILE" == "-" ]; then
+    FILE=""
+else
+    if [ $DRY_RUN -ne 1 ]; then
+        # Blank the file
+        echo "" > $FILE
+    fi
+fi
+
+dump () {
+    local command=$1
+
+    if [ $DRY_RUN -eq 1 ]; then
+        if [ -n "$FILE" ]; then
+            echo "$command >> $FILE"
+        else
+            echo $command
+        fi
+    else
+        if [ -n "$FILE" ]; then
+            $command >> $FILE
+        else
+            $command
+        fi
+    fi
+}
+
+# Dump structure
+STRUCT_DUMPER="$DUMPER --no-data $SQL_DB"
+dump "$STRUCT_DUMPER"
+
+SHARED_DUMPER="$DUMPER $SQL_DB $SHARED_TABLES"
+dump "$SHARED_DUMPER"