Skip to content

Commit

Permalink
chore: exporter and importer tools
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Mar 15, 2022
1 parent 54024b9 commit c7f0fb1
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.~
**/*.tgz
**/*.gz
**/*.zip
**/*.gzip
**/*.log
**/*.tar
**/*.sql
**/.DS_Store
*.env

!mysql_exporter.env
!mysql_importer.env
13 changes: 13 additions & 0 deletions 8.0/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ RUN set -eux \
&& rm -rf /var/lib/apt/lists/* \
&& true

# Include the Enve tool
ARG ENVE_VERSION=1.4.0

RUN set -eux \
&& wget -O /tmp/enve.tar.gz \
"https://github.com/joseluisq/enve/releases/download/v${ENVE_VERSION}/enve_v${ENVE_VERSION}_linux_amd64.tar.gz" \
&& tar xzvf /tmp/enve.tar.gz -C /usr/local/bin enve \
&& enve -v \
&& rm -rf /tmp/enve.tar.gz \
&& chmod +x /usr/local/bin/enve \
&& true

COPY 8.0/entrypoint.sh /usr/local/bin/docker-entrypoint.sh
COPY 8.0/scripts/. /usr/local/bin/

VOLUME [ "/var/lib/mysql" ]

Expand Down
23 changes: 23 additions & 0 deletions 8.0/env/mysql_exporter.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## MySQL 8 Export template settings
## --------------------------------

# Connection settings (optional)
DB_PROTOCOL=tcp
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DEFAULT_CHARACTER_SET=utf8

# GZip export file (optional)
DB_EXPORT_GZIP=false

# SQL or Gzip export file (optional).
# If `DB_IMPORT_GZIP` is `true` then file name should be `database_name.sql.gz`
DB_EXPORT_FILE_PATH=database_name.sql

# Database settings (required)
DB_NAME=""
DB_USERNAME=""
DB_PASSWORD=""

# Additional arguments (optional)
DB_ARGS=
23 changes: 23 additions & 0 deletions 8.0/env/mysql_importer.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## MySQL 8 Import template settings
## --------------------------------

# Connection settings (optional)
DB_PROTOCOL=tcp
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DEFAULT_CHARACTER_SET=utf8

# GZip import support (optional)
DB_IMPORT_GZIP=false

# SQL or Gzip import file (required)
# If `DB_IMPORT_GZIP` is `true` then file name should be something like `database_name.sql.gz`
DB_IMPORT_FILE_PATH=database_name.sql

# Database settings (required)
DB_NAME=""
DB_USERNAME=""
DB_PASSWORD=""

# Additional arguments (optional)
DB_ARGS=
82 changes: 82 additions & 0 deletions 8.0/scripts/___mysqlexport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/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 "ERROR: `DB_NAME` env variable is required."; exit 1; fi
if [[ -z "$DB_USERNAME" ]]; then "ERROR: `DB_USERNAME` env variable is required."; exit 1; fi
if [[ -z "$DB_PASSWORD" ]]; then "ERROR: `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 "MySQL 8 Client - Exporter"
echo "========================="

mysqldump --version

echo
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
65 changes: 65 additions & 0 deletions 8.0/scripts/___mysqlimport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/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 "ERROR: `DB_NAME` env variable is required."; exit 1; fi
if [[ -z "$DB_USERNAME" ]]; then "ERROR: `DB_USERNAME` env variable is required."; exit 1; fi
if [[ -z "$DB_PASSWORD" ]]; then "ERROR: `DB_PASSWORD` env variable is required."; exit 1; fi
if [[ -z "$DB_IMPORT_FILE_PATH" ]]; then "ERROR: `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 "MySQL 8 Client - Importer"
echo "========================="

mysql --version

FILE_SIZE=$(du -sh $DB_IMPORT_FILE_PATH | cut -f1)

echo
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!"
17 changes: 17 additions & 0 deletions 8.0/scripts/mysql_exporter
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

set -e

FILE_ENV=$1

if [[ -z "$FILE_ENV" ]]; then
___mysqlexport.sh
exit 0
fi

if [[ -f $FILE_ENV ]]; then
enve -f $FILE_ENV ___mysqlexport.sh
else
echo "ERROR: env file \`$FILE_ENV\` was not found"
exit 1
fi
17 changes: 17 additions & 0 deletions 8.0/scripts/mysql_importer
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

set -e

FILE_ENV=$1

if [[ -z "$FILE_ENV" ]]; then
___mysqlimport.sh
exit 0
fi

if [[ -f $FILE_ENV ]]; then
enve -f $FILE_ENV ___mysqlimport.sh
else
echo "ERROR: env file \`$FILE_ENV\` was not found"
exit 1
fi
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ build:
@docker build -t mysql-client:latest -f 8.0/Dockerfile .
.PHONY: build

HOME_USER ?= $(shell echo $$(id -u $$USER):$$(id -g $$USER))

export:
@docker run --rm -it \
--user $(HOME_USER) \
--name mysql-client \
--volume $(PWD):/home/mysql/sample \
--network mysql-net \
--network mysql8_net \
--workdir /home/mysql/sample \
mysql-client:latest \
mysql_exporter export.env
Expand All @@ -16,9 +17,8 @@ export:
import:
@docker run --rm -it \
--user $(HOME_USER) \
--name mysql-client \
--volume $(PWD):/home/mysql/sample \
--network mysql-net \
--network mysql8_net \
--workdir /home/mysql/sample \
mysql-client:latest \
mysql_importer import.env
Expand Down
Loading

0 comments on commit c7f0fb1

Please sign in to comment.