3

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 2

3

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
Sign up to request clarification or add additional context in comments.

7 Comments

But wouldn't this store the backups in the mongo_db_backup container?
@user3142695 No, it has the /backup volume mapped to the host as you did on the command line.
Maybe you want to use something like /opt/mongo/project/live_backup:/backup for the volume to house it near the data?
Left hand side of : is the server path or volume name. Right hand side is the path inside the container where the left hand volume will appear. This also means the UID the container process runs as will need write access to the servers /opt/mongo/project/live_backup directory. You might need to chown 999:999 /opt/mongo/project/live_backup in mongodb's case
output options are available in mongodump --help. -o=$(date +%s) and --gzip
|
2

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.