Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover MySQL data from Docker container

I was running mariadb instance on docker windows toolkit. I did a env vaiable change on the mariaDB container using kitematic. Now it has recreated an instance loosing all my database. Is there a way to recover from this ?

Checked if threre are dangling volumes, and there are few

docker volume ls -f dangling=true

like image 244
Don Srinath Avatar asked Jun 25 '20 09:06

Don Srinath


People also ask

How to restore a local or remote MySQL database using Docker?

Command to restore a local or remote MySQL database using Docker with compression (using gzip): $ gunzip< backup.sql.gz | docker run -i\mysql /usr/bin/mysqldump \-h[MYSQL_HOST] -u[MYSQL_USER] \--password=[MYSQL_PASSWORD] [MYSQL_DATABASE] Restore backup into a MySQL Server Docker container Command to restore a database from plain SQL file:

How to run MySQL in a docker container?

How to Run MySql in a Docker Container - Step by Step 1.Downloading a MySQL Server Docker Image. The :latest tag will download the latest version of MySQL. ... 2.Start a MySQL Container in Docker. The next step is to run a container in Docker with the MySQL image. ... This will... 3.Connecting to ...

How do I backup a MSSQL database using Docker?

The following commands navigate to the home/user directory and downloads the backup file as wwi.bak. Use docker cp to copy the backup file into the container in the /var/opt/mssql/backup directory.

How to download the latest version of MySQL in Docker?

The :latest tag will download the latest version of MySQL. If you want do download a specific version, simply replace the The next step is to run a container in Docker with the MySQL image. To do this, execute the next command: Let’s break down this command to understand it better:


2 Answers

Got the data recovered using the dangling volumes. Approach is as following.

First get the list of dangling volumes.

$ docker volume ls -f dangling=true
DRIVER              VOLUME NAME
local               6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19
local               47bb077ef6f6df9f56bd30c35eeb45f35e62213d2c50db6f078bfdeeee6698ec

Then mounted it on to a Ubuntu container (so that you can go inside the directory and check what is there, as there is no other way to do this when you are using Docker Tool Box on windows)

$ docker run --name tempContainer1-UBUNTU -v 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19:/var/lib/backup -t -i ubuntu /bin/bash

Then you will be inside the bash of newly created contianer. Go to newly mounted directory and check content

$cd /var/lib/backup
$ls
$aria_log.00000001  aria_log_control  ib_buffer_pool  ib_logfile0  ib_logfile1  ibdata1  ibtmp1  multi-master.info  mysql  performance_schema
-- once you are sure directory data is what you require, make a zip file of the folder 
$apt-get update
$apt-get install zip
$cd ..
$zip -r backup.zip backup

On another terminal from host copy the content of container backup.zip to host

$docker cp tempContainer1-UBUNTU:/var/lib/backup.zip .

Then create a docker compose file like following and mount the backup folder as data directory. Run this on linux host as this mounting will not work as expected for mysql on windows.

version: "3.2"
services:
  mysql:
    image: mariadb:10.4.12
    restart: always
    ports:
      - "3306:3306"      
    command: mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON
    volumes:
            - ./backup_data_folder:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: somepassword
      TZ: Asia/Singapore
    networks:
      - frontend
    container_name: maria
networks:
  frontend:

Start

$docker-compose up

Once it is up, from another terminal go inside newly created container

$docker exec -t -i maria /bin/bash
-- Take dump of all the DBS 
$mysqldump -u root -p --all-databases > alldb.sql

Copy content of the dump to host from another terminal from host

$docker cp maria:/alldb.sql .

Now this sql file is a full dump, restore it as usual on your mysql DB or contianer.

mysql -u root -p < alldb.sql 
like image 95
Don Srinath Avatar answered Sep 27 '22 22:09

Don Srinath


Recently I had to face the same problem for a lost wordpress container and I've followed the instructions from Don. However, as there were lots of dangling volumes, I had to optimize the process. I've managed a way to do it simpler, in the same terminal, resulting in the following steps:

docker volume ls -f dangling=true

DRIVER              VOLUME NAME
local               43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e
local               4fde3ea412e54a1b8b42fce6dae5de5135b9fd12933b020b43bd482cd5fd2225
local               52074ccfd62fb83b8b40cff5f8024215b34f79ad09b630e067ec732f811f798c
...

Then, for each container execute the following instruction, replacing 43277666c8bc3d... with each VOLUME NAME found. This instruction will remove previously maria-restore containers if they exist, create a new one and attach to it:

docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore; docker run --name maria-restore -v 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e:/var/lib/mysql -d mariadb:10.4.12 mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON && docker exec -it maria-restore bash

If it wasn't a mysql volume, it will fail and exit immediately. If it is a mysql volume, you'll be inside the mariadb container. The database will be already started. You can then connect to the database to see if it is the right one and backup it:

root@8b35c8e2c474:/# mysql -uadmin -p
root@8b35c8e2c474:/# mysqldump -uadmin -p --all-databases > alldb.sql
root@8b35c8e2c474:/# exit

Copy the backed up database:

docker cp mysql-restore:/alldb.sql .

Finally you'll have to clean up the maria-restore container:

docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore
like image 28
Miquel Avatar answered Sep 27 '22 23:09

Miquel