Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB in Docker - backup database on container shutdown

I'm trying to setup docker-compose with Node and MongoDB. In combination with the official Mongo container I'm using a dedicated container (mongodb-backup) for continuous backups of the database and an initial restore at startup.

My problem is how to trigger a backup in case of a graceful shutdown by docker-compose stop.
Therefore it's necessary to shut down the Node container at first to guarantee no process is writing to the database. Then backup the database and finally shutdown the mongodb and mongodb-backup conatainers.

This repository contains my docker-compose setup of mongodb and mongodb-backup.

Does anyone have an idea how to implement this?

like image 743
Tobias Wittwer Avatar asked Nov 10 '16 15:11

Tobias Wittwer


1 Answers

You could write a small maintenance script for this case. Instead of doing a raw docker-compose stop, you could just run this script instead. Let's call 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
like image 139
ShrimpPhaser Avatar answered Nov 09 '22 09:11

ShrimpPhaser