Skip to content

Commit c7f0fb1

Browse files
committed
chore: exporter and importer tools
1 parent 54024b9 commit c7f0fb1

10 files changed

+412
-4
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.~
2+
**/*.tgz
3+
**/*.gz
4+
**/*.zip
5+
**/*.gzip
6+
**/*.log
7+
**/*.tar
8+
**/*.sql
9+
**/.DS_Store
10+
*.env
11+
12+
!mysql_exporter.env
13+
!mysql_importer.env

8.0/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ RUN set -eux \
9999
&& rm -rf /var/lib/apt/lists/* \
100100
&& true
101101

102+
# Include the Enve tool
103+
ARG ENVE_VERSION=1.4.0
104+
105+
RUN set -eux \
106+
&& wget -O /tmp/enve.tar.gz \
107+
"https://github.com/joseluisq/enve/releases/download/v${ENVE_VERSION}/enve_v${ENVE_VERSION}_linux_amd64.tar.gz" \
108+
&& tar xzvf /tmp/enve.tar.gz -C /usr/local/bin enve \
109+
&& enve -v \
110+
&& rm -rf /tmp/enve.tar.gz \
111+
&& chmod +x /usr/local/bin/enve \
112+
&& true
113+
102114
COPY 8.0/entrypoint.sh /usr/local/bin/docker-entrypoint.sh
115+
COPY 8.0/scripts/. /usr/local/bin/
103116

104117
VOLUME [ "/var/lib/mysql" ]
105118

8.0/env/mysql_exporter.env

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## MySQL 8 Export template settings
2+
## --------------------------------
3+
4+
# Connection settings (optional)
5+
DB_PROTOCOL=tcp
6+
DB_HOST=127.0.0.1
7+
DB_PORT=3306
8+
DB_DEFAULT_CHARACTER_SET=utf8
9+
10+
# GZip export file (optional)
11+
DB_EXPORT_GZIP=false
12+
13+
# SQL or Gzip export file (optional).
14+
# If `DB_IMPORT_GZIP` is `true` then file name should be `database_name.sql.gz`
15+
DB_EXPORT_FILE_PATH=database_name.sql
16+
17+
# Database settings (required)
18+
DB_NAME=""
19+
DB_USERNAME=""
20+
DB_PASSWORD=""
21+
22+
# Additional arguments (optional)
23+
DB_ARGS=

8.0/env/mysql_importer.env

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## MySQL 8 Import template settings
2+
## --------------------------------
3+
4+
# Connection settings (optional)
5+
DB_PROTOCOL=tcp
6+
DB_HOST=127.0.0.1
7+
DB_PORT=3306
8+
DB_DEFAULT_CHARACTER_SET=utf8
9+
10+
# GZip import support (optional)
11+
DB_IMPORT_GZIP=false
12+
13+
# SQL or Gzip import file (required)
14+
# If `DB_IMPORT_GZIP` is `true` then file name should be something like `database_name.sql.gz`
15+
DB_IMPORT_FILE_PATH=database_name.sql
16+
17+
# Database settings (required)
18+
DB_NAME=""
19+
DB_USERNAME=""
20+
DB_PASSWORD=""
21+
22+
# Additional arguments (optional)
23+
DB_ARGS=

8.0/scripts/___mysqlexport.sh

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
START=`date +%s`
4+
5+
XDB_PROTO="$DB_PROTOCOL"
6+
XDB_HOST="$DB_HOST"
7+
XDB_PORT="$DB_PORT"
8+
XDB_DEFAULT_CHARACTER_SET="$DB_DEFAULT_CHARACTER_SET"
9+
XDB_EXPORT_FILE="$DB_EXPORT_FILE_PATH"
10+
XDB_EXPORT_GZIP="$DB_EXPORT_GZIP"
11+
XDB_EXPORT=
12+
13+
# Required env variables
14+
if [[ -z "$DB_NAME" ]]; then "ERROR: `DB_NAME` env variable is required."; exit 1; fi
15+
if [[ -z "$DB_USERNAME" ]]; then "ERROR: `DB_USERNAME` env variable is required."; exit 1; fi
16+
if [[ -z "$DB_PASSWORD" ]]; then "ERROR: `DB_PASSWORD` env variable is required."; exit 1; fi
17+
18+
# Optional env variables
19+
if [[ -z "$XDB_PROTO" ]]; then XDB_PROTO="tcp"; fi
20+
if [[ -z "$XDB_HOST" ]]; then XDB_HOST="127.0.0.1"; fi
21+
if [[ -z "$XDB_PORT" ]]; then XDB_PORT="3306"; fi
22+
if [[ -z "$XDB_DEFAULT_CHARACTER_SET" ]]; then XDB_DEFAULT_CHARACTER_SET=utf8; fi
23+
if [[ -z "$DB_EXPORT_FILE_PATH" ]]; then XDB_EXPORT_FILE="./$DB_NAME.sql"; fi
24+
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
25+
if [[ -z $DB_EXPORT_FILE_PATH ]]; then XDB_EXPORT_FILE="$XDB_EXPORT_FILE.gz"; fi
26+
27+
XDB_EXPORT="| gzip -c > $XDB_EXPORT_FILE"
28+
else
29+
XDB_EXPORT="> $XDB_EXPORT_FILE"
30+
fi
31+
32+
DB_PASSWORD=$(echo -n $DB_PASSWORD | sed 's/"/\\"/g')
33+
34+
CMD="\
35+
--protocol=$XDB_PROTO \
36+
--host=$XDB_HOST \
37+
--port=$XDB_PORT \
38+
--default-character-set=$XDB_DEFAULT_CHARACTER_SET \
39+
--user=$DB_USERNAME \
40+
--password="\"$DB_PASSWORD"\" \
41+
$DB_ARGS $DB_NAME $XDB_EXPORT"
42+
43+
echo "MySQL 8 Client - Exporter"
44+
echo "========================="
45+
46+
mysqldump --version
47+
48+
echo
49+
echo "Exporting database \`$DB_NAME\` into a SQL script file..."
50+
51+
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
52+
echo "Output file: $XDB_EXPORT_FILE (SQL GZipped)"
53+
else
54+
echo "Output file: $XDB_EXPORT_FILE (SQL Text)"
55+
fi
56+
57+
OUTPUT=$(eval mysqldump ${CMD} 2>&1)
58+
exitcode=$?
59+
60+
if [[ $exitcode != 0 ]]; then echo $OUTPUT; exit $exitcode; fi
61+
62+
# Note: Ugly workaround here because `mysqldump` (unlike `mysql`) doesn't emit a proper exit code even in MySQL v8
63+
# See https://bugs.mysql.com/bug.php?id=90538
64+
if echo $OUTPUT | grep -qE '(.*)(mysqldump: Got error:|: eval:)(.*)'; then
65+
echo $OUTPUT
66+
exit 1
67+
fi
68+
69+
if [[ ! -z "$OUTPUT" ]]; then echo $OUTPUT; fi;
70+
71+
FILE_SIZE=$(du -sh $XDB_EXPORT_FILE | cut -f1)
72+
73+
END=`date +%s`
74+
RUNTIME=$((END-START))
75+
76+
echo "Database \`$DB_NAME\` was exported on ${RUNTIME}s successfully!"
77+
78+
if [[ -n "$XDB_EXPORT_GZIP" ]] && [[ "$XDB_EXPORT_GZIP" = "true" ]]; then
79+
echo "File exported: $XDB_EXPORT_FILE ($FILE_SIZE / SQL GZipped)"
80+
else
81+
echo "File exported: $XDB_EXPORT_FILE ($FILE_SIZE / SQL Text)"
82+
fi

8.0/scripts/___mysqlimport.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
START=`date +%s`
6+
7+
XDB_PROTO="$DB_PROTOCOL"
8+
XDB_HOST="$DB_HOST"
9+
XDB_PORT="$DB_PORT"
10+
XDB_DEFAULT_CHARACTER_SET="$DB_DEFAULT_CHARACTER_SET"
11+
XDB_IMPORT_FILE="$DB_IMPORT_FILE_PATH"
12+
XDB_IMPORT_GZIP="$DB_IMPORT_GZIP"
13+
XDB_IMPORT=
14+
15+
# Required env variables
16+
if [[ -z "$DB_NAME" ]]; then "ERROR: `DB_NAME` env variable is required."; exit 1; fi
17+
if [[ -z "$DB_USERNAME" ]]; then "ERROR: `DB_USERNAME` env variable is required."; exit 1; fi
18+
if [[ -z "$DB_PASSWORD" ]]; then "ERROR: `DB_PASSWORD` env variable is required."; exit 1; fi
19+
if [[ -z "$DB_IMPORT_FILE_PATH" ]]; then "ERROR: `DB_IMPORT_FILE_PATH` env variable is required."; exit 1; fi
20+
21+
# Optional env variables
22+
if [[ -z "$XDB_PROTO" ]]; then XDB_PROTO="tcp"; fi
23+
if [[ -z "$XDB_HOST" ]]; then XDB_HOST="127.0.0.1"; fi
24+
if [[ -z "$XDB_PORT" ]]; then XDB_PORT="3306"; fi
25+
if [[ -z "$XDB_DEFAULT_CHARACTER_SET" ]]; then XDB_DEFAULT_CHARACTER_SET=utf8; fi
26+
if [[ -n "$XDB_IMPORT_GZIP" ]] && [[ "$XDB_IMPORT_GZIP" = "true" ]]; then
27+
XDB_IMPORT="gzip -dc $XDB_IMPORT_FILE |"
28+
XDB_IMPORT_FILE=
29+
else
30+
XDB_IMPORT_FILE="< $XDB_IMPORT_FILE"
31+
fi
32+
33+
DB_PASSWORD=$(echo -n $DB_PASSWORD | sed 's/"/\\"/g')
34+
35+
CMD="\
36+
--protocol=$XDB_PROTO \
37+
--host=$XDB_HOST \
38+
--port=$XDB_PORT \
39+
--default-character-set=$XDB_DEFAULT_CHARACTER_SET \
40+
--user=$DB_USERNAME \
41+
--password="\"$DB_PASSWORD"\" \
42+
$DB_ARGS $DB_NAME $XDB_IMPORT_FILE"
43+
44+
echo "MySQL 8 Client - Importer"
45+
echo "========================="
46+
47+
mysql --version
48+
49+
FILE_SIZE=$(du -sh $DB_IMPORT_FILE_PATH | cut -f1)
50+
51+
echo
52+
echo "Importing a SQL script file into database \`$DB_NAME\`..."
53+
54+
if [[ -n "$XDB_IMPORT_GZIP" ]] && [[ "$XDB_IMPORT_GZIP" = "true" ]]; then
55+
echo "Input file: $DB_IMPORT_FILE_PATH ($FILE_SIZE / SQL GZipped)"
56+
else
57+
echo "Input file: $DB_IMPORT_FILE_PATH ($FILE_SIZE / SQL Text)"
58+
fi
59+
60+
eval "${XDB_IMPORT}mysql ${CMD}"
61+
62+
END=`date +%s`
63+
RUNTIME=$((END-START))
64+
65+
echo "Database \`$DB_NAME\` was imported on ${RUNTIME}s successfully!"

8.0/scripts/mysql_exporter

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
FILE_ENV=$1
6+
7+
if [[ -z "$FILE_ENV" ]]; then
8+
___mysqlexport.sh
9+
exit 0
10+
fi
11+
12+
if [[ -f $FILE_ENV ]]; then
13+
enve -f $FILE_ENV ___mysqlexport.sh
14+
else
15+
echo "ERROR: env file \`$FILE_ENV\` was not found"
16+
exit 1
17+
fi

8.0/scripts/mysql_importer

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
FILE_ENV=$1
6+
7+
if [[ -z "$FILE_ENV" ]]; then
8+
___mysqlimport.sh
9+
exit 0
10+
fi
11+
12+
if [[ -f $FILE_ENV ]]; then
13+
enve -f $FILE_ENV ___mysqlimport.sh
14+
else
15+
echo "ERROR: env file \`$FILE_ENV\` was not found"
16+
exit 1
17+
fi

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ build:
22
@docker build -t mysql-client:latest -f 8.0/Dockerfile .
33
.PHONY: build
44

5+
HOME_USER ?= $(shell echo $$(id -u $$USER):$$(id -g $$USER))
6+
57
export:
68
@docker run --rm -it \
79
--user $(HOME_USER) \
8-
--name mysql-client \
910
--volume $(PWD):/home/mysql/sample \
10-
--network mysql-net \
11+
--network mysql8_net \
1112
--workdir /home/mysql/sample \
1213
mysql-client:latest \
1314
mysql_exporter export.env
@@ -16,9 +17,8 @@ export:
1617
import:
1718
@docker run --rm -it \
1819
--user $(HOME_USER) \
19-
--name mysql-client \
2020
--volume $(PWD):/home/mysql/sample \
21-
--network mysql-net \
21+
--network mysql8_net \
2222
--workdir /home/mysql/sample \
2323
mysql-client:latest \
2424
mysql_importer import.env

0 commit comments

Comments
 (0)