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