Skip to content

Instantly share code, notes, and snippets.

@FARUK-YILDIRIM
Last active January 10, 2025 01:32
Show Gist options
  • Select an option

  • Save FARUK-YILDIRIM/d1045020683b515927ecd1e67271e517 to your computer and use it in GitHub Desktop.

Select an option

Save FARUK-YILDIRIM/d1045020683b515927ecd1e67271e517 to your computer and use it in GitHub Desktop.
mysql dump and import script for docker
#!/usr/bin/env bash
SOURCE_HOST=''
SOURCE_PORT='3306'
SOURCE_USER=''
SOURCE_PASSWORD=''
SOURCE_DB=''
DUMP_FILE='dump.sql'
DEST_HOST='host.docker.internal'
DEST_PORT='3306'
DEST_USER=''
DEST_PASSWORD=''
DEST_DB=''
DOCKER_CONTAINER_NAME='mysql'
echo "Starting mysqldump from $SOURCE_DB..."
docker exec "$DOCKER_CONTAINER_NAME" mysqldump \
--single-transaction \
--column-statistics=0 \
-v \
-h"$SOURCE_HOST" \
-P"$SOURCE_PORT" \
-u"$SOURCE_USER" \
-p"$SOURCE_PASSWORD" \
"$SOURCE_DB" > "$DUMP_FILE"
if [[ $? -ne 0 ]]; then
echo "Error: mysqldump failed!"
exit 1
fi
echo "mysqldump completed successfully."
echo "Importing dump file $DUMP_FILE into $DEST_DB..."
docker exec -i "$DOCKER_CONTAINER_NAME" mysql \
-h"$DEST_HOST" \
-P"$DEST_PORT" \
-u"$DEST_USER" \
-p"$DEST_PASSWORD" \
"$DEST_DB" < "$DUMP_FILE"
if [[ $? -ne 0 ]]; then
echo "Error: Import to $DEST_DB failed!"
exit 1
fi
echo "Import completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment