Ask Your Question
1

After obtaining a new mongo image, what are the steps to recover the mongodb data from an external docker volume?

asked 2023-05-28 06:18:42 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-28 06:19:01 +0000

qstack gravatar image
  1. Start a new MongoDB container with the same version as the original container.

  2. Mount the external volume to the new container using the -v flag in the docker run command. For example:

docker run -d --name new-mongo -v my_volume:/data/db mongo

Note that my_volume is the name of the volume holding the MongoDB data.

  1. Once the new container is running, open a shell in the container using the docker exec command:
docker exec -it new-mongo /bin/bash
  1. Use the mongodump command to create a backup of the data in the mounted volume:
mongodump --dbpath /data/db --out /backup

Here, /backup is the location where the backup will be stored inside the container.

  1. Once the backup is complete, exit the shell.

  2. Copy the backup from the container to a local directory using the docker cp command:

docker cp new-mongo:/backup /path/to/local/backup
  1. Stop and remove the new container:
docker stop new-mongo
docker rm new-mongo
  1. Start a new MongoDB container using the same command as in step 2, but this time add the --auth flag to require authentication.

  2. Use the mongorestore command to restore the data from the backup:

mongorestore --dbpath /data/db --drop /path/to/local/backup

Here, /path/to/local/backup is the location of the backup file on the host machine.

  1. Verify that the data has been successfully restored by connecting to the MongoDB instance and checking the databases and collections.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-28 06:18:42 +0000

Seen: 11 times

Last updated: May 28 '23