Add simple script to allow for exporting database structure.
[platal.git] / bin / export_sql.bash
CommitLineData
62450d20
RB
1#!/bin/bash
2
3# This script is used to export the MySQL structure of the plat/al MySQL
4# databases.
5
6
7# Developers: list 'public' tables here.
8SHARED_TABLES="account_types
9forums
10geoloc_administrative_areas
11geoloc_countries
12geoloc_localities
13geoloc_subadministrativeareas
14log_actions
15newsletter_cat
16profile_binet_enum
17profile_corps_enum
18profile_corps_rank_enum
19profile_education_degree_enum
20profile_education_enum
21profile_education_field_enum
22profile_job_alternate
23profile_job_entreprise_term
24profile_job_enum
25profile_job_sector_enum
26profile_job_subsector_enum
27profile_job_subsubsector_enum
28profile_job_term_enum
29profile_job_term_relation
30profile_langskills_enum
31profile_medal_enum
32profile_medal_grade_enum
33profile_name_enum
34profile_networking_enum
35profile_section_enum
36profile_skill_enum
37reminder_type
38skin"
39
40usage()
41{
42cat << EOF
43usage: $0 OPTIONS
44
45Will dump the necessary data from MySQL.
46Note that all options should have sane defaults if you have correctly configured
47your .my.cnf file.
48
49OPTIONS:
50-h Show this message
51-H HOST MySQL host to connect to (default: none)
52-P PORT MySQL port to connect to (default: none)
53-u USER User for MySQL (default: none)
54-p PASS MySQL password to use (default: none)
55-d SQL_DB Database to read from (default: x5dat)
56-n Dry run, don't actually write anything
57-o FILE Write to file instead of stdout (- for stdout as well)
58
59EOF
60}
61
62HOST=
63PASS=
64PORT=
65USER=
66SQL_DB="x5dat"
67FILE="-"
68DRY_RUN=0
69
70while getopts "hns:u:p:o:" OPTION
71do
72 case $OPTION in
73 h)
74 usage
75 exit 1
76 ;;
77 n)
78 DRY_RUN=1
79 ;;
80 o)
81 FILE=$OPTARG
82 ;;
83 p)
84 PASS=$OPTARG
85 ;;
86 H)
87 HOST=$OPTARG
88 ;;
89 P)
90 PORT=$OPTARG
91 ;;
92 u)
93 USER=$OPTARG
94 ;;
95 d)
96 SQL_DB=$OPTARG
97 ;;
98 ?)
99 usage
100 exit
101 ;;
102 esac
103done
104
105
106DUMPER="mysqldump --add-drop-table --default-character-set=utf8 --force"
107
108if [ -n "$USER" ]; then
109 DUMPER="$DUMPER --user=$USER"
110fi
111
112if [ -n "$PASS" ]; then
113 DUMPER="$DUMPER --password=$PASS"
114fi
115
116if [ -n "$HOST" ]; then
117 DUMPER="$DUMPER --host=$HOST"
118fi
119
120if [ -n "$PORT" ]; then
121 DUMPER="$DUMPER --port=$PORT"
122fi
123
124if [ "$FILE" == "-" ]; then
125 FILE=""
126else
127 if [ $DRY_RUN -ne 1 ]; then
128 # Blank the file
129 echo "" > $FILE
130 fi
131fi
132
133dump () {
134 local command=$1
135
136 if [ $DRY_RUN -eq 1 ]; then
137 if [ -n "$FILE" ]; then
138 echo "$command >> $FILE"
139 else
140 echo $command
141 fi
142 else
143 if [ -n "$FILE" ]; then
144 $command >> $FILE
145 else
146 $command
147 fi
148 fi
149}
150
151# Dump structure
152STRUCT_DUMPER="$DUMPER --no-data $SQL_DB"
153dump "$STRUCT_DUMPER"
154
155SHARED_DUMPER="$DUMPER $SQL_DB $SHARED_TABLES"
156dump "$SHARED_DUMPER"