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