-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__mysqldump.sh
executable file
·81 lines (62 loc) · 2.48 KB
/
__mysqldump.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
START=`date +%s`
XDB_PROTO="$DB_PROTOCOL"
XDB_HOST="$DB_HOST"
XDB_PORT="$DB_PORT"
XDB_DEFAULT_CHARACTER_SET="$DB_DEFAULT_CHARACTER_SET"
XDB_EXPORT_FILE="$DB_EXPORT_FILE_PATH"
XDB_EXPORT_GZIP="$DB_EXPORT_GZIP"
XDB_EXPORT=
# 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
# 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 [[ -z "$DB_EXPORT_FILE_PATH" ]]; then XDB_EXPORT_FILE="./$DB_NAME.sql"; fi
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
if [[ -z $DB_EXPORT_FILE_PATH ]]; then XDB_EXPORT_FILE="$XDB_EXPORT_FILE.gz"; fi
XDB_EXPORT="| gzip -c > $XDB_EXPORT_FILE"
else
XDB_EXPORT="> $XDB_EXPORT_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_EXPORT"
echo "Alpine / MySQL Client - Exporter"
echo "================================"
mysqldump --version
echo "Exporting database \`$DB_NAME\` into a SQL script file..."
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
echo "Output file: $XDB_EXPORT_FILE (SQL GZipped)"
else
echo "Output file: $XDB_EXPORT_FILE (SQL Text)"
fi
OUTPUT=$(eval mysqldump ${CMD} 2>&1)
exitcode=$?
if [[ $exitcode != 0 ]]; then echo $OUTPUT; exit $exitcode; fi
# Note: Ugly workaround here because `mysqldump` (unlike `mysql`) doesn't emit a proper exit code even in MySQL v8
# See https://bugs.mysql.com/bug.php?id=90538
if echo $OUTPUT | grep -qE '(.*)(mysqldump: Got error:|: eval:)(.*)'; then
echo $OUTPUT
exit 1
fi
if [[ ! -z "$OUTPUT" ]]; then echo $OUTPUT; fi;
FILE_SIZE=$(du -sh $XDB_EXPORT_FILE | cut -f1)
END=`date +%s`
RUNTIME=$((END-START))
echo "Database \`$DB_NAME\` was exported on ${RUNTIME}s successfully!"
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
echo "File exported: $XDB_EXPORT_FILE ($FILE_SIZE / SQL GZipped)"
else
echo "File exported: $XDB_EXPORT_FILE ($FILE_SIZE / SQL Text)"
fi