Skip to content

Instantly share code, notes, and snippets.

@vanWittlaer
Last active August 4, 2021 14:57
Show Gist options
  • Save vanWittlaer/0a749169039a3433fc1e4f9bca2bd0d4 to your computer and use it in GitHub Desktop.
Save vanWittlaer/0a749169039a3433fc1e4f9bca2bd0d4 to your computer and use it in GitHub Desktop.
A simple bash script to mysqldump a Shopware 6 database using .env settings
#!/usr/bin/env bash
#
# usage: ./dbdump.sh <shopware project root dir> <target file for dump>
#
# note 1) this script requires PHP and gzip
# note 2) the --skip-triggers option breaks the blue-green-deployment however is usefull for re-importing on another server
#
# Trouble with generated columns?
# - See this: https://dba.stackexchange.com/questions/240882/how-to-take-mysqldump-with-generated-column
# - Though I recommend to find a compatible mysqldump client
#
ROOT=${1:-/app/shopware}
TARGET=${2:-test.sql}
#
export ENV_FILE=${ENV_FILE:-"${ROOT}/.env"}
source ${ENV_FILE}
#
DECODED=$( php -r "echo urldecode(\"$DATABASE_URL\");"; )
FIELDS=($(echo $( php -r "echo implode(' ', parse_url(\"$DECODED\"));"; )))
PROTO=${FIELDS[0]}
HOST=${FIELDS[1]}
PORT=${FIELDS[2]}
USER=${FIELDS[3]}
PASSWORD=${FIELDS[4]}
DATABASE=${FIELDS[5]#'/'}
#
echo Dumping database $DATABASE on host $HOST for user $USER into $TARGET
#
export MYSQL_PWD=$PASSWORD
mysqldump --no-tablespaces --single-transaction --skip-triggers --no-data -u$USER -h$HOST --port $PORT $DATABASE | gzip > $TARGET
mysqldump --no-tablespaces --single-transaction --skip-triggers --no-create-info --hex-blob \
--ignore-table=$DATABASE.enqueue --ignore-table=$DATABASE.dead_message --ignore-table=$DATABASE.message_queue_stats \
--ignore-table=$DATABASE.refresh_token \
-u$USER -h$HOST --port $PORT $DATABASE | gzip >> $TARGET
#
echo Finished dumping database $DATABASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment