Reputation: 19725
I have a docker image for my mongoDB. I want to make a script that backup production data, and restore it in my local machine, so that I can have a dev environment similar to production.
Here is a part of my docker-compose.yml
mongo:
image: "mongo:latest"
container_name: "espace_client_mongo"
volumes:
- mongo-data:/data/db
I want to export data contained in mongo-data
volume.
Docs state that I can do it with:
docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
but I can't make it work.
When I run it, I get:
ec2-user@espace-client-prod prod]$ docker run --rm --volumes-from espace_client_mongo -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar ./backup
tar: ./backup/backup.tar: file is the archive; not dumped
./backup/
./backup/Caddyfile
./backup/docker-compose.yml
[ec2-user@espace-client-prod prod]$ ls
backup.tar Caddyfile docker-compose.yml
It will put the files in current folder into a backup folder. This is not what I want. I want to put the content of docker volume in the backup folder. Am I misunderstanding something ?
EDIT: I would like to backup and restore data with mongo tools ( mongodump and mongorestore)
Upvotes: 3
Views: 8012
Reputation: 2184
You can find the data you want to backup in the directory /var/lib/docker/volumes/mongo-data/_data
¹ on your host machine. This is what you have at /data/db
inside your container.
You don't have to run an additional Docker container just to backup your data if you already have them as a volume. So you only need to run sudo tar cvzf backup.tar.gz /var/lib/docker/volumes/mongo-data/_data
from your host machine.
UPDATE
Since you want to use mongodump
, just follow the official documentation for the Docker image you're using:
$ docker exec some-mongo sh -c 'exec mongodump -d <database_name> --archive' > /some/path/on/your/host/all-collections.archive
¹ Note that this volume might have another name, probably prefixed with the folder's name from where you run the docker-compose.yml
if you didn't define anything else.
Upvotes: 8
Reputation: 81
I use this command to backup the data from the container, note the directory "/data/db".
docker run --rm \
--volumes-from espace_client_mongo \
-v $(pwd):/backup \
ubuntu bash -c "cd /data/db && tar cvf /backup/backup.tar ."
Upvotes: 1