Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@lnasc256
Copy link

lnasc256 commented Jun 18, 2022

thanks

@Harshit2377
Copy link

Harshit2377 commented Jun 19, 2022

i am using this command for restore but the restore in not getting .
plzz help

@jrmarqueshd
Copy link

jrmarqueshd commented Jun 24, 2022

Awesome!

@HorselessName
Copy link

HorselessName commented Jun 29, 2022

Hey... What about a crontab for this?
Lets say: backupZabbix.sh

I do have a backup script that works well with crontab using Zabbix outside docker - For some reason if i use docker exec mysqldump it doesnt work... :(

Can u give us a tip?

@nhossaincse
Copy link

nhossaincse commented Jul 17, 2022

Can someone please tell me, how I get the database dump out of the docker container on my host? The command

docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql

seems to dump the file inside the docker container, but I need it on my host. Thanks!

I think, you may use this command to get the files in host from container:
docker cp :/file/path/within/container /host/path/target

@ambrosiora
Copy link

ambrosiora commented Jul 19, 2022

Can someone please tell me, how I get the database dump out of the docker container on my host? The command
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
seems to dump the file inside the docker container, but I need it on my host. Thanks!

I think, you may use this command to get the files in host from container: docker cp :/file/path/within/container /host/path/target

have you tried to map a volume to your host?
For example, if you dump the .sql file INSIDE the container on /home/dump (or whatever folder you want)
just map this folder as a volume on you host.

I can give you an example with docker-compose.yml. With pure docker I don't know how to map a volume (never needed since I always worked with docker-compose hahaha)

version: '3'
services:
  mydb:
    container_name: mydb
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: 123
      MYSQL_ROOT_PASSWORD: 123456
    volumes:
      - /home/host_folder_dump:/home/container_folder_dump

Since you take a mysql dump, automatically the .sql file will appear in your host folder.
Tell me what you think?

@blu-IT
Copy link

blu-IT commented Jul 19, 2022

Can someone please tell me, how I get the database dump out of the docker container on my host? The command
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
seems to dump the file inside the docker container, but I need it on my host. Thanks!

I think, you may use this command to get the files in host from container: docker cp :/file/path/within/container /host/path/target

@nhossaincse

Thanks! I already solved its (inside my script) this way:

docker cp <container_id>:/file/path/within/container /host/path/target

@blu-IT
Copy link

blu-IT commented Jul 19, 2022

Since you take a mysql dump, automatically the .sql file will appear in your host folder. Tell me what you think?

@ambrosiora

Thanks! I already solved its (inside my script) this way:

docker cp <container_id>:/file/path/within/container /host/path/target

@behai-nguyen
Copy link

behai-nguyen commented Aug 7, 2022

Very sweet. Thank you very much.

@echanobe
Copy link

echanobe commented Aug 14, 2022

Thank you!

@Tipset
Copy link

Tipset commented Aug 17, 2022

This helped a bunch, thank you!

@hkanizawa
Copy link

hkanizawa commented Aug 24, 2022

Thanks

@tomaszroot
Copy link

tomaszroot commented Aug 31, 2022

:-*

@botosdavid
Copy link

botosdavid commented Sep 19, 2022

thank you so much!

@dorimusz
Copy link

dorimusz commented Sep 19, 2022

Thanks a lot!

@ttcosta11
Copy link

ttcosta11 commented Sep 26, 2022

Thank you!

@tgiovanella87
Copy link

tgiovanella87 commented Oct 12, 2022

Great! Thanks!

@paulovnas
Copy link

paulovnas commented Nov 4, 2022

Very nice... Thank you so much.

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