MongoDB backup within docker container
Here’s a pet project I’m working on - It is a simple blog engine written in java 8 that uses mongodb as a primary database. I was pretty excited about docker when I heard about it for the first time. Recently, I wrote a small article describing docker essentials - it’s a post showing basic fundamental commands that you can use when working with docker.
Today I’m going to share the scripts I wrote to make and restore backups of a mongodb database. I was surprised that I didn’t find anything similar on google so I hope somebody finds these scripts helpful.
Backup
If you name your container something other than mongo
, you need to change the link parameter from mongo:mongo
to [name]:mongo
. Also, if you don’t use the default port (27017), you’ll need to modify the variables $MONGO_PORT_27017_TCP_ADDR
and $MONGO_PORT_27017_TCP_PORT
.
You can now use it as follows:
Now backup.tar
will contain all your collections packed in a tar
file.
Restoring
This script has two possible modes: tar restoring and collection restoring.
The first mode looks like this:
The second one may look like this:
If you want to help me improve my scripts, you can send me a pull request to this repository.