-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__mysqlimport.sh
executable file
·64 lines (49 loc) · 1.88 KB
/
__mysqlimport.sh
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
#!/bin/sh
set -e
START=`date +%s`
XDB_PROTO="$DB_PROTOCOL"
XDB_HOST="$DB_HOST"
XDB_PORT="$DB_PORT"
XDB_DEFAULT_CHARACTER_SET="$DB_DEFAULT_CHARACTER_SET"
XDB_IMPORT_FILE="$DB_IMPORT_FILE_PATH"
XDB_IMPORT_GZIP="$DB_IMPORT_GZIP"
XDB_IMPORT=
# Required env variables
if [[ -z "$DB_NAME" ]]; then "`DB_NAME` env variable is required."; exit 1; fi
if [[ -z "$DB_USERNAME" ]]; then "`DB_USERNAME` env variable is required."; exit 1; fi
if [[ -z "$DB_PASSWORD" ]]; then "`DB_PASSWORD` env variable is required."; exit 1; fi
if [[ -z "$DB_IMPORT_FILE_PATH" ]]; then "`DB_IMPORT_FILE_PATH` env variable is required."; exit 1; fi
# Optional env variables
if [[ -z "$XDB_PROTO" ]]; then XDB_PROTO="tcp"; fi
if [[ -z "$XDB_HOST" ]]; then XDB_HOST="127.0.0.1"; fi
if [[ -z "$XDB_PORT" ]]; then XDB_PORT="3306"; fi
if [[ -z "$XDB_DEFAULT_CHARACTER_SET" ]]; then XDB_DEFAULT_CHARACTER_SET=utf8; fi
if [[ -n "$XDB_IMPORT_GZIP" ]] && [[ "$XDB_IMPORT_GZIP" = "true" ]]; then
XDB_IMPORT="gzip -dc $XDB_IMPORT_FILE |"
XDB_IMPORT_FILE=
else
XDB_IMPORT_FILE="< $XDB_IMPORT_FILE"
fi
DB_PASSWORD=$(echo -n $DB_PASSWORD | sed 's/"/\\"/g')
CMD="\
--protocol=$XDB_PROTO \
--host=$XDB_HOST \
--port=$XDB_PORT \
--default-character-set=$XDB_DEFAULT_CHARACTER_SET \
--user=$DB_USERNAME \
--password="\"$DB_PASSWORD"\" \
$DB_ARGS $DB_NAME $XDB_IMPORT_FILE"
echo "Alpine / MySQL Client - Importer"
echo "================================"
mysql --version
FILE_SIZE=$(du -sh $DB_IMPORT_FILE_PATH | cut -f1)
echo "Importing a SQL script file into database \`$DB_NAME\`..."
if [[ -n "$XDB_IMPORT_GZIP" ]] && [[ "$XDB_IMPORT_GZIP" = "true" ]]; then
echo "Input file: $DB_IMPORT_FILE_PATH ($FILE_SIZE / SQL GZipped)"
else
echo "Input file: $DB_IMPORT_FILE_PATH ($FILE_SIZE / SQL Text)"
fi
eval "${XDB_IMPORT}mysql ${CMD}"
END=`date +%s`
RUNTIME=$((END-START))
echo "Database \`$DB_NAME\` was imported on ${RUNTIME}s successfully!"