ac2d9b769ba792095c969111d919dcb3f7090e52
[platal.git] / bin / export_sql.bash
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.
8 SHARED_TABLES="account_types \
9 forums \
10 geoloc_administrativeareas \
11 geoloc_countries \
12 geoloc_localities \
13 geoloc_subadministrativeareas \
14 log_actions \
15 newsletter_cat \
16 profile_binet_enum \
17 profile_corps_enum \
18 profile_corps_rank_enum \
19 profile_education_degree \
20 profile_education_degree_enum \
21 profile_education_enum \
22 profile_education_field_enum \
23 profile_job_entreprise_term \
24 profile_job_enum \
25 profile_job_term_enum \
26 profile_job_term_relation \
27 profile_langskill_enum \
28 profile_medal_enum \
29 profile_medal_grade_enum \
30 profile_networking_enum \
31 profile_section_enum \
32 profile_skill_enum \
33 reminder_type \
34 skins"
35
36 usage()
37 {
38 cat << EOF
39 usage: $0 OPTIONS
40
41 Will dump the necessary data from MySQL.
42 Note that all options should have sane defaults if you have correctly configured
43 your .my.cnf file.
44
45 OPTIONS:
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
55 EOF
56 }
57
58 HOST=
59 PASS=
60 PORT=
61 USER=
62 SQL_DB="x5dat"
63 FILE="-"
64 DRY_RUN=0
65
66 while getopts "hns:u:p:o:" OPTION
67 do
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
99 done
100
101 FILTER="sed -r s/AUTO_INCREMENT=[1-9]+/AUTO_INCREMENT=1/"
102 DUMPER="mysqldump --add-drop-table --default-character-set=utf8 --force"
103
104 if [ -n "$USER" ]; then
105 DUMPER="$DUMPER --user=$USER"
106 fi
107
108 if [ -n "$PASS" ]; then
109 DUMPER="$DUMPER --password=$PASS"
110 fi
111
112 if [ -n "$HOST" ]; then
113 DUMPER="$DUMPER --host=$HOST"
114 fi
115
116 if [ -n "$PORT" ]; then
117 DUMPER="$DUMPER --port=$PORT"
118 fi
119
120 if [ "$FILE" == "-" ]; then
121 FILE=""
122 else
123 if [ $DRY_RUN -ne 1 ]; then
124 # Blank the file
125 echo "" > $FILE
126 fi
127 fi
128
129 dump () {
130 local command=$1
131
132 if [ $DRY_RUN -eq 1 ]; then
133 if [ -n "$FILE" ]; then
134 echo "$command | $FILTER >> $FILE"
135 else
136 echo "$command | $FILTER"
137 fi
138 else
139 if [ -n "$FILE" ]; then
140 $command | $FILTER >> $FILE
141 else
142 $command | $FILTER
143 fi
144 fi
145 }
146
147 # Dump structure
148 STRUCT_DUMPER="$DUMPER --no-data $SQL_DB"
149 dump "$STRUCT_DUMPER"
150
151 SHARED_DUMPER="$DUMPER $SQL_DB $SHARED_TABLES"
152 dump "$SHARED_DUMPER"