-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-transfer
executable file
·357 lines (321 loc) · 12.3 KB
/
mysql-transfer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# mysql-transfer
# Copyright (C) 2018 Webelop Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# USAGE:
# ./mysql-transfer PATH FORCE
# - Argument 1 - PATH: configuration path. eg: ./sample-transfer
# - Argument 2 - FORCE: optional "force" - bypass configuration questions and run all data migrations
#
# CONFIG FOLDER STRUCTURE:
# - ./structure-data-tables.dist.txt > one table per line, containing all default tables to dump with data
# - ./structure-only-tables.dist.txt > one table per line, containing all default tables to dump without data
# - ./configuration.dist.sh > configuration file, contains clauses for dumping subsets of tables (eg: most recent data)
# - ./data-updates.dist.sql > SQL queries to run after transfer
#
# NOTES:
# - The config folder basename must be unique for each transfer, as it is used as a mysql "login path"
# - All files can exist in 2 versions: *.dist.* files and *.user.* files
# *.user.* files have precedence and are ignored in git
# *.dist.* files can be created from *.user.* files to share a transfer profile across a team
# - mysqldump and mysql_config_editor MUST BE in the path
# ################################################################################################################ #
echo ""
echo "mysql-transfer Copyright (C) 2018 Webelop Ltd."
echo "This program comes with ABSOLUTELY NO WARRANTY; This is free software,"
echo "and you are welcome to redistribute it under certain conditions;"
echo "Read LICENSE file for details"
echo ""
CONFIG_DIR="$1"
if [ "$CONFIG_DIR" == "" ] || [ ! -d "$CONFIG_DIR" ]; then
echo "Usage: ./mysql-transfer PATH FORCE"
echo " - Argument 1: configuration path prefix. eg: ~/transfers/from-test"
echo " - Argument 2: optional 'force' - bypass configuration questions and run all data migrations"
echo ""
exit;
fi
LOGINPATH=$(basename "$CONFIG_DIR" | sed "s/[^a-zA-Z0-9]/_/")
FORCE="$2"
echo "Transfer $FORCE on profile $LOGINPATH"
echo ""
MYSQLDUMP_OPTIONS="--quick --single-transaction --add-drop-table --skip-add-locks --skip-comments --skip-disable-keys --set-gtid-purged=OFF"
LIMITEDDATA_TABLES=()
# DEFAULT CONFIGURATION: Pull database to local
SRC_DB_PORT="3306"
DEST_DB_PORT="3306"
DEST_DB_USER="root"
DEST_HOST="localhost"
#READ CONFIG
STRUCTURE_DATA_TABLES=$(cat "$CONFIG_DIR/structure-data-tables.dist.txt" | grep "^[a-z0-9]")
STRUCTURE_ONLY_TABLES=$(cat "$CONFIG_DIR/structure-only-tables.dist.txt" | grep "^[a-z0-9]")
CONFIG_PATH="$CONFIG_DIR/configuration.dist.sh"
if [ -f "$CONFIG_PATH" ]; then
. "$CONFIG_PATH"
else
echo "Config file does not exist at $CONFIG_PATH"
fi
CONFIG_PATH="$CONFIG_DIR/import-config.user.sh"
if [ -f "$CONFIG_PATH" ]; then
. "$CONFIG_PATH"
fi
function ask_config_question () {
local VARNAME=$1
local VARFILE=$2
local VARDESCR=$3
local CURRENTVALUE="${!VARNAME}"
if [ ! -f "$CONFIG_DIR/$VARFILE" ]; then
if [ "$FORCE" == "1" ]; then
echo "Missing $CONFIG_DIR/$VARFILE!"
exit
fi
else
CURRENTVALUE=$(cat $CONFIG_DIR/$VARFILE)
fi
# Check saved value
if [ "$CURRENTVALUE" != "" ] && [ "$FORCE" != "force" ]; then
echo "> Use '$CURRENTVALUE' for $VARDESCR ? (Y/n)";
read USETHIS
if [ "$USETHIS" == "n" ]; then
CURRENTVALUE="";
fi
fi
# Request user value
if [ "$CURRENTVALUE" == "" ]; then
echo "> What is the $VARDESCR?"
read CURRENTVALUE;
if [ "$CURRENTVALUE" != "" ]; then
echo "$CURRENTVALUE" > $CONFIG_DIR/$VARFILE
fi
fi
# Exit if empty value provided
if [ "$CURRENTVALUE" == "" ]; then
echo "Missing dump host! Exiting..."
exit;
fi
# Assign value
eval "$VARNAME='$CURRENTVALUE'"
}
echo ">>>>>>> SOURCE CONFIGURATION >>>>>>>>"
ask_config_question "MYSQLDUMP_HOST" "mysqldump-host.user.txt" "mysqldump server (eg: localhost or username@ip_or_domain for ssh)"
ask_config_question "SRC_DB_NAME" "source-db-name.user.txt" "database name to import data from"
if [ "$MYSQLDUMP_HOST" != "" ] && [ "$MYSQLDUMP_HOST" != "localhost" ]; then
SRC_LOGINPATH="${LOGINPATH}_ssh_dump_src"
else
MYSQLDUMP_HOST=""
SRC_LOGINPATH="${LOGINPATH}_dump_src"
MYSQLDUMP_OPTIONS="$MYSQLDUMP_OPTIONS --compress"
fi
SRC_LOGIN_OPTION="--login-path=${SRC_LOGINPATH}"
#Helper function to execute a command locally or over ssh
function exec_on_src {
if [ "$MYSQLDUMP_HOST" != "" ]; then
ssh $MYSQLDUMP_HOST "$@"
else
eval "$@"
fi
}
DUMPSRC="0"
exec_on_src "mysql $SRC_LOGIN_OPTION -ss $SRC_DB_NAME -e 'select 123' 2> /dev/null" | grep 123 > /dev/null && DUMPSRC="1"
if [ "$DUMPSRC" == "1" ] && [ "$FORCE" != "force" ]; then
echo "> Import data FROM $SRC_LOGINPATH? (Y/n)"
exec_on_src "mysql_config_editor print $SRC_LOGIN_OPTION"
read USETHIS
[ "$USETHIS" == "n" ] && DUMPSRC=0
fi
if [ "$DUMPSRC" == "0" ]; then
if [ "$FORCE" == "force" ]; then
echo "Ouch! Source database is undefined!"
exit;
fi
echo "> What is the source database host? [$SRC_DB_HOST]"
read NEWSRC_DB_HOST
if [ "$NEWSRC_DB_HOST" != "" ]; then
SRC_DB_HOST="$NEWSRC_DB_HOST"
fi
echo "> What is the source database port? [$SRC_DB_PORT]"
read NEWSRC_DB_PORT
if [ "$NEWSRC_DB_PORT" != "" ]; then
SRC_DB_PORT="$NEWSRC_DB_PORT"
fi
echo "> Who is the source database user? [$SRC_DB_USER]"
read NEWSRC_DB_USER
if [ "$NEWSRC_DB_USER" != "" ]; then
SRC_DB_USER="$NEWSRC_DB_USER"
fi
SQLCMD="mysql_config_editor set --skip-warn $SRC_LOGIN_OPTION --host='$SRC_DB_HOST' --user='$SRC_DB_USER' --password --port='$SRC_DB_PORT'"
if [ "$MYSQLDUMP_HOST" != "" ] && [ "$MYSQLDUMP_HOST" != "localhost" ]; then
ssh $MYSQLDUMP_HOST -t "$SQLCMD; exit"
else
$SQLCMD
fi
# Test connection
exec_on_src "mysql $SRC_LOGIN_OPTION $SRC_DB_NAME -e 'select 123' 2> /dev/null" | grep "123" && DUMPSRC="1"
if [ "$DUMPSRC" == "0" ]; then
echo "Invalid connection: Import cancelled!"
exit
fi
fi
echo ""
echo ">>>>>>> DESTINATION CONFIGURATION >>>>>>>>"
ask_config_question "DEST_DB_NAME" "dest-db-name.user.txt" "database name to import data to"
DEST_LOGINPATH="--login-path=${LOGINPATH}_dump_dst"
DUMPDEST="0"
mysql $DEST_LOGINPATH $DEST_DB_NAME -e 'select 123' 2>/dev/null | grep 123 > /dev/null && DUMPDEST=1
if [ "$DUMPDEST" == "1" ] && [ "$FORCE" != "force" ]; then
echo "> Import data TO this database? (Y/n)"
mysql_config_editor print $DEST_LOGINPATH
read USETHIS
[ "$USETHIS" == "n" ] && DUMPDEST=0
fi
if [ "$DUMPDEST" == "0" ]; then
if [ "$FORCE" == "1" ]; then
echo "Ouch! Destination database is unconfigured!"
exit;
fi
echo "> What is the destination database host? [$DEST_HOST]"
read NEWDEST_HOST
if [ "$NEWDEST_HOST" != "" ]; then
DEST_HOST="$NEWDEST_HOST"
fi
echo "> What is the destination database port? [$DEST_DB_PORT]"
read NEWDEST_DB_PORT
if [ "$NEWDEST_DB_PORT" != "" ]; then
DEST_DB_PORT="$NEWDEST_DB_PORT"
fi
echo "> Who is the destination database user? [$DEST_DB_USER]"
read NEWDEST_DB_USER
if [ "$NEWDEST_DB_USER" != "" ]; then
DEST_DB_USER="$NEWDEST_DB_USER"
fi
mysql_config_editor set --skip-warn $DEST_LOGINPATH --host="$DEST_HOST" --user="$DEST_DB_USER" --port="$DEST_DB_PORT" --password > /dev/null
# Test connection
mysql $DEST_LOGINPATH $DEST_DB_NAME -e 'select 123' 2> /dev/null | grep "123" > /dev/null && DUMPDEST="1"
if [ "$DUMPDEST" == "0" ]; then
echo "Invalid connection: Import cancelled"
exit
fi
fi
DUMPED="0"
echo ""
echo ">>>>>>> STRUCTURE ONLY >>>>>>>>"
if [ "$STRUCTURE_ONLY_TABLES" != "" ] && [ "$FORCE" != "force" ]; then
echo "$STRUCTURE_ONLY_TABLES"
echo "> Dump structure only for these tables? (y/N)"
read YES
if [ "$YES" != "y" ]; then
STRUCTURE_ONLY_TABLES=""
fi
fi
if [ "$STRUCTURE_ONLY_TABLES" == "" ] && [ "$FORCE" != "force" ]; then
echo "> Dump structure only for custom table list? (empty to skip)"
read STRUCTURE_ONLY_TABLES
fi
if [ "$STRUCTURE_ONLY_TABLES" != "" ] && [ "$FORCE" != "force" ]; then
for table in $(echo "$STRUCTURE_ONLY_TABLES" | sed 's/\W\+/&\n/g'| grep -oiE '[a-z0-9_]+')
do
echo "< $(date '+%H:%M:%S') - Dumping structure of $table"
exec_on_src "mysqldump $SRC_LOGIN_OPTION $MYSQLDUMP_OPTIONS --no-data $SRC_DB_NAME $table" | mysql $DEST_LOGINPATH $DEST_DB_NAME
done
fi
echo ""
echo ">>>>>>> STRUCTURE AND DATA >>>>>>>>"
if [ "$STRUCTURE_DATA_TABLES" != "" ] && [ "$FORCE" != "force" ]; then
echo "$STRUCTURE_DATA_TABLES"
echo "> Dump structure and data for these tables? (y/N)"
read YES
if [ "$YES" != "y" ]; then
TABLES=""
fi
fi
if [ "$STRUCTURE_DATA_TABLES" == "" ] && [ "$FORCE" != "force" ]; then
echo "> Dump structure and data for custom table list? (empty to skip)"
read TABLES
fi
if [ "$STRUCTURE_DATA_TABLES" != "" ]; then
for table in $(echo "$STRUCTURE_DATA_TABLES" | sed 's/\W\+/&\n/g'| grep -oiE '[a-z0-9_]+')
do
echo "< $(date '+%H:%M:%S') - Dumping $table"
SQLCMD="nice mysqldump $SRC_LOGIN_OPTION $MYSQLDUMP_OPTIONS $SRC_DB_NAME $table"
if [ "$MYSQLDUMP_HOST" != "" ] && [ "$MYSQLDUMP_HOST" != "localhost" ]; then
ssh $MYSQLDUMP_HOST "$SQLCMD | gzip" | gunzip | mysql $DEST_LOGINPATH $DEST_DB_NAME
else
$SQLCMD | mysql $DEST_LOGINPATH $DEST_DB_NAME
fi
done
fi
echo ""
echo ">>>>>>> STRUCTURE AND RECENT DATA >>>>>>>>"
LIMITEDDATA_SQLCMD=()
# see ./configuration.dist.sh for exemples
for clause in "${LIMITEDDATA_TABLES[@]}"
do
PLACEHOLDER=$(echo "$clause" | grep -oE '\{\{[^}]+\}\}' | sed 's#[{ }]##g')
if [ "$PLACEHOLDER" != "" ]; then
VALUE=""
if [ "${!PLACEHOLDER}" == "" ]; then
SQLVAR="${PLACEHOLDER}_QUERY"
SQLSTMT=${!SQLVAR}
if [ "$SQLSTMT" != "" ]
then
VALUE=$(exec_on_src "mysql $SRC_LOGIN_OPTION $SRC_DB_NAME -ss -e '$SQLSTMT'" | grep -oE '^[0-9]+$')
fi
else
VALUE="${!PLACEHOLDER}"
fi
if [ "$VALUE" == "" ]; then
VALUE=0
fi
eval "$PLACEHOLDER='$VALUE'"
NUMERICVALUE=0
echo "$VALUE" | grep -oE '^[0-9]+$' > /dev/null && NUMERICVALUE=1
if [ "$NUMERICVALUE" == "1" ]; then
clause=$(echo "$clause" | sed "s/{{ *$PLACEHOLDER *}}/$VALUE/g")
else
echo "< $PLACEHOLDER is not numeric [$VALUE]... skipping!"
continue;
fi
fi
if [ "$FORCE" != "force" ]; then
echo "> Dump recent data for $clause? (y/N)"
read YES
if [ "$YES" != 'y' ]; then
continue;
fi
fi
LIMITEDDATA_SQLCMD+=("$clause")
done
for clause in "${LIMITEDDATA_SQLCMD[@]}"; do
echo "< $(date '+%H:%M:%S') - Dumping structure and data : $clause"
SQLCMD="nice mysqldump $SRC_LOGIN_OPTION $MYSQLDUMP_OPTIONS $SRC_DB_NAME $clause"
if [ "$MYSQLDUMP_HOST" != "" ] && [ "$MYSQLDUMP_HOST" != "localhost" ]; then
ssh $MYSQLDUMP_HOST "$SQLCMD | bzip2" | bunzip2 | mysql $DEST_LOGINPATH $DEST_DB_NAME
else
eval "$SQLCMD" | mysql $DEST_LOGINPATH $DEST_DB_NAME
fi
done
echo ""
echo ">>>>>>> DATABASE UPDATES >>>>>>>>"
IMPORT_UPDATES_PATH="$CONFIG_DIR/data-updates.dist.sql"
if [ -f "$IMPORT_UPDATES_PATH" ]
then
echo "< $(date '+%H:%M:%S') - Executing database updates at $IMPORT_UPDATES_PATH"
mysql $DEST_LOGINPATH $DEST_DB_NAME < "$IMPORT_UPDATES_PATH" > /dev/null
else
echo "< $(date '+%H:%M:%S') - Database update file does not exist at $IMPORT_UPDATES_PATH"
fi
IMPORT_UPDATES_PATH="$CONFIG_DIR/data-updates.user.sql"
if [ -f "$IMPORT_UPDATES_PATH" ]
then
echo "< $(date '+%H:%M:%S') - Executing database updates at $IMPORT_UPDATES_PATH"
mysql $DEST_LOGINPATH $DEST_DB_NAME < "$IMPORT_UPDATES_PATH" > /dev/null
fi