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