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