V
V
vostotskiy2019-01-09 12:56:11
MongoDB
vostotskiy, 2019-01-09 12:56:11

How to implement a daily backup of a MongoDB database running in a docker container?

Hello. In my project on the MEAN stack, there is a Mongo database that is separately launched in a docker container
. An approximate container setup from a docker-compose file

mongodb:
    image: mongo
    ports:
      - "27017:27017"
    volumes:
          - ./db:/data/db
    networks:
      - mean-network

There is a command that runs directly inside a running database docker image
mongodump --db dbName --out /data/db/backups/`date +"%m-%d-%y"`,

which writes a backup of the required database to an external volume in a folder with the name of the current date of its creation
How to implement the launch of this command on an ongoing basis, as a periodic task, when the container is running?
That is, when the container is running, so that the database is backed up once a day at a certain time with the creation of a folder in the specified format.
Thanks in advance for advice and recommendations.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Zamyatin, 2019-03-07
@corw

Alternatively, put something like this on cron:

docker run --rm --link {{your-mongo-container-name}}:mongo \
  -v {{path_on_host & named volume}}:/data/db/backups/ \
  mongo bash -c ‘mongodump --out /data/db/backups/`date +"%m-%d-%y"` --host mongo:27017

In fact, you create another mongo instance and execute the dump through it. Upon completion of the operation, this instance will kill itself.

V
Vasily Shakhunov, 2019-03-11
@inf

I did something similar to the answer above. The cron runs `docker-compose up` which makes dumps. The main thing here is to get into the network you need, and contact the necessary host. If without certificates, then everything is simple.

networks:
  production_default:
    external: true

version: "3"
services:
  mongodump:
    image: mongo:4.0
    volumes:
    - ./mongodb/dump:/data/dump
    command: /bin/sh -c 'mongodump --quiet --host mongodb --archive=/data/dump/$$(date +"%Y-%m-%d").gz --gzip --db databasename_production'
    networks: 
      - production_default

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question