Correct syntax to do mongodump of mongoDb docker instance?

Viewed 4552

I'm running an ubuntu 16.04 LTS server with some docker container. One of these containers is a mongoDB container, where my data is stored. Now I'm trying to make a backup by mongodump. The problem for me is, that mongoDb is running as a docker container, and the backup should be stored outside of the docker container.

I think the syntax for this is something like this:

docker run \
  --rm \
  -it \
  --link DOCKER_CONTAINER_NAME:mongo_alias \
  -v /backup:/backup \
  mongo mongodump \
  --host mongo_alias \
  --out /backup/

But I'm not sure for the parameters I have to use...

This is what I get for my mongoDb container via docker ps:

7bee41bfa08a  mongo:3.4  "docker-entrypoint..."   4 months ago   Up 2 months   27017/tcp   mongo_db

And this is my docker-compose file:

version: "3"
services:
  mongo_db:
    container_name: mongo_db
    image: 'mongo:3.4'
    restart: 'always'
    volumes:
      - '/opt/mongo/project/live:/data/db'

So it should look like this?

docker run \
  --rm \
  -it \
  --link mongo_db:mongo_alias \ # mongo_alias can be choosen freely?
  -v /backup:/backup \          # Don't understand /backup:/backup
  mongo mongodump \
  --host mongo_alias \
  --out /backup/                # This is in the root of the server?
2 Answers

Define the backup to run via compose as well. This will create the new container on the same network as the main mongo container. If you have any compose network definitions you will need to duplicate them in each compose file.

Create a second compose file for the backup command: docker-compose-backup.yml

version: "3"
services:
  mongo_db_backup:
    image: 'mongo:3.4'
    volumes:
      - '/opt/mongo/project/live_backup:/backup'
    command: |
      mongodump --host mongo_db --out /backup/

Then run the backup

docker-compose -f docker-compose-backup.yml run mongo_db_backup

You can also do this without docker-composer, directly from your host

Backup all databases

docker exec -t your-db-container mongodump --host mongo_db --out /backup/

Restore all databases

  • Move your backup folder to your host volume folder
  • docker exec -t your-db-container mongorestore /backup/
Related