-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
412 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.