MongoDB in Docker - backup database when closing container

I am trying to configure docker-compose with Node and MongoDB. In combination with the official Mongo container, I use a dedicated container ( mongodb-backup ) for continuous backup of the database and initial restore to start.

My problem is how to call the backup in case of a graceful shutdown of docker-compose stop .
Therefore, you must first close the Node container to ensure that the process does not write to the database. Then back up the database and finally close the mongodb and mongodb containers backups.

This repository contains my layout configuration of the mongodb and mongodb docker file.

Does anyone have an idea how to implement this?

+5
source share
1 answer

You can write a little maintenance script for this case. Instead of doing a raw docker-compose stop , you can simply run this script. Name it maintenance.sh:

 #!/bin/sh docker-compose stop app docker-compose stop mongo-backup # ensure only one is active docker-compose run --rm -e INIT_BACKUP=1 # do the backup manually docker-compose stop mongo # stop the database 
0
source

Source: https://habr.com/ru/post/1259547/


All Articles