Skip to content

Instantly share code, notes, and snippets.

@Rillke
Last active June 13, 2021 17:24
Show Gist options
  • Save Rillke/a53e74299384eea8e2a5d84613ee8baf to your computer and use it in GitHub Desktop.
Save Rillke/a53e74299384eea8e2a5d84613ee8baf to your computer and use it in GitHub Desktop.
Restore database dump to mariadb docker container

Create database dump from MariaDB Docker container

docker-compose exec mariadb sh -c \
  'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' \
  ____encoding____

encoding

xz

| xz --stdout --verbose > [filename].sql.xz

gz

| gzip -c > [filename].sql.gz

bz2

| bzip2 -c > [filename].sql.bz2

Restore database dump to MariaDB Docker container

Scenario: You got a database dump and should restore it. You are making use of the official MariaDB Docker image and docker-compose.

docker-compose up -d mariadb && \
sleep 20 && \
____decoding____ \
docker exec -i "$(docker-compose ps -q mariadb)" \
sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" $MYSQL_DATABASE'
  • docker-compose up -d mariadb: Create and start a service named mariadb. The service name may vary, dependend how you call it in your docker-compose.yaml
  • sleep 20: Give the DBMS time to start. You may, instead create a bash script polling for the server to have startet and being ready for connections.
  • ____decoding____: See below.
  • docker exec -i: There is/was an issue with piping into docker-compose directly. May be solved now.
  • "$(docker-compose ps -q mariadb)": Find the full identifier for the docker-compose mariadb service.
  • sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" $MYSQL_DATABASE': Inside the container, execute MySQL and take the connection details from the environment.

Double encoding

Dumps may be double-encoded (you can tell your DBA that's unnecessary):

  • tgz = tar.gz
  • tbz2 = tar.bz2
  • tar.xz

First, decompress, then untar, for instance:

docker-compose up -d mariadb && sleep 20 && gzip -cd [filename] | tar -Ox | docker exec -i "$(docker-compose ps -q mariadb)" sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" $MYSQL_DATABASE'

decoding

xz

xz --stdout --force --keep --verbose --decompress [filename] |

gz

gzip -cd [filename] |
pv [filename] | gzip -cd |

bz2

bzip2 -cd [filename] |
pv [filename] | bzip2 -cd |

tar

tar -Ox |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment