Project test environment database data loss, this is to record. At that time, docker was installed to temporary use for a period of time, and did not persist. Suddenly the docker log was full the day before yesterday. My colleague wanted to clear the log and used the following command:

docker system prune
Copy the code

It turned out that the MySQL container was normally in a stopped state, and the container suddenly dried up. Our backup data was still from March, which was terrible. And then all kinds of research started to come back.

Then I went to the official documentation to find out what this command does. The docker system prune used above means:

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

Remove all unused containers, networks, images (dangling and unreferenced images), and volumes (optional).

By default, volumes are not removed to prevent important data from being deleted if there is currently no container using the volume. Use the --volumes flag when running the command to prune volumes as well:

By default, if no container currently uses the volume, the volume is not deleted to prevent important data from being deleted. You can also use the –volumes flag when running commands to trim volumes:

Fortunately, the data volume has not been deleted. We can use the data volume to recover data. Let’s write down my recovery plan.

1. Locate the data volume

Data volume directory under/var/lib/docker/volumes, each container will have a folder in the directory, if the container still exists, we can use the docker inspect container ID to view the data volume, the container to be deleted, can do, can only turn to find the, The MySQL container data volume directory will have a _data directory. This directory will display the folders of each database.

This CXHello is our test library, and now we can restore the data.

2. Restore

  1. useDocker Volume Create Name of the data volumeCommand to create a new data volume,docker volume lsView the data volume list

Note: When using a data volume for mounting, the data volume must be an empty directory, that is, no data.

  1. And then create the container
Docker run -d -p 3309:3306 -v mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name huifu mysql:5.7Copy the code

  1. Before data restoration, delete the content associated with the newly created data volume and copy the content of the original data volume to the current data volume for data restoration.
cd /var/lib/docker/volumes/mysqldata/_data/
rm -f *
rm -f -R *
Copy the code

  1. Copy content to data volumes
cd /var/lib/docker/volumes/1db16a9dfdf3442b117ebc2ec11df5df4db717cfd567c77fa0a49905a9652fa0/_data/
cp -R * /var/lib/docker/volumes/mysqldata/_data/
Copy the code

At this point, the database data recovery is complete. Go to the recovery container to check

Refer to the article

Docs.docker.com/engine/refe…

www.cnblogs.com/cheyunhua/p…