Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save catalincezarene/cc47c79e8c7d62693a6853f746f14c05 to your computer and use it in GitHub Desktop.
Save catalincezarene/cc47c79e8c7d62693a6853f746f14c05 to your computer and use it in GitHub Desktop.
Backup and restore a MariaDB/MySQL database running in a Docker container

Dump your DB running in a Docker container using the native MariaDB/MySQL mysqldump command

docker exec <container> \
  mysqldump \
    --user=<username> \
    --password=<password> \
    --single-transaction \
    <db_name> | gzip > <dump_name>.sql.gz

docker-compose exec <container> \
  mysqldump \
    --user=<username> \
    --password=<password> \
    --single-transaction \
    <db_name> | gzip > <dump_name>.sql.gz

Import the dump file into your DB running in a Docker container using the native MariaDB/MySQL mysql command

zcat <dump_name>.sql.gz | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | docker exec -i <container> \
  mysql \
    --user=<username> \
    --password=<password> \
    <db_name>

zcat <dump_name>.sql.gz | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | docker-compose exec -T <container> \
  mysql \
    --user=<username> \
    --password=<password> \
    <db_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment