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