MongoDB Backup & Restore in Docker Container

MongoDB backup and restore from docker container is pretty easy. Here’s you could follow.

Please visit my blog previous blog Install MongoDB Docker to setup docker.

Let’s Started

I’ve a running MongoDB Instance.

krishna@ubuntu:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
21ea276f002e        mongo               "docker-entrypoint..."   46 seconds ago      Up 2 seconds        0.0.0.0:27017->27017/tcp   mongo
krishna@ubuntu:~$

Backup

Backup of MongoDB database illustrated below.

krishna@ubuntu:~$ docker run -it --rm --link mongo:mongo -v /tmp/mongodump:/tmp mongo bash -c 'mongodump -v --host $MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT --out=/tmp'
2017-05-23T06:36:47.788+0000 writing admin.system.version to 
2017-05-23T06:36:47.789+0000 done dumping admin.system.version (1 document)
2017-05-23T06:36:47.789+0000 dumping up to 3 collections in parallel
2017-05-23T06:36:47.789+0000 writing mydb.movie to 
2017-05-23T06:36:47.789+0000 writing test.product to 
2017-05-23T06:36:47.790+0000 done dumping mydb.movie (1 document)
2017-05-23T06:36:47.790+0000 done dumping test.product (1 document)
krishna@ubuntu:~$ 

krishna@ubuntu:~$ ls -ltrh /tmp/mongodump/
total 12K
drwxr-xr-x 2 root root 4.0K May 23 12:06 test
drwxr-xr-x 2 root root 4.0K May 23 12:06 mydb
drwxr-xr-x 2 root root 4.0K May 23 12:06 admin
krishna@ubuntu:~$ 

Restore

Restore of MongoDB database illustrated below.

krishna@ubuntu:~$ docker run -it --rm --link mongo:mongo -v /tmp/mongodump:/tmp mongo bash -c 'mongorestore -v --host $MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT /tmp'
2017-05-23T06:48:56.687+0000 using write concern: w='1', j=false, fsync=false, wtimeout=0
2017-05-23T06:48:56.687+0000 preparing collections to restore from
2017-05-23T06:48:56.687+0000 found collection admin.system.version bson to restore to admin.system.version
2017-05-23T06:48:56.687+0000 found collection metadata from admin.system.version to restore to admin.system.version
2017-05-23T06:48:56.687+0000 found collection mydb.movie bson to restore to mydb.movie
2017-05-23T06:48:56.687+0000 found collection metadata from mydb.movie to restore to mydb.movie
2017-05-23T06:48:56.688+0000 found collection test.product bson to restore to test.product
2017-05-23T06:48:56.688+0000 found collection metadata from test.product to restore to test.product
2017-05-23T06:48:56.688+0000 reading metadata for test.product from /tmp/test/product.metadata.json
2017-05-23T06:48:56.688+0000 creating collection test.product using options from metadata
2017-05-23T06:48:56.697+0000 reading metadata for mydb.movie from /tmp/mydb/movie.metadata.json
2017-05-23T06:48:56.698+0000 creating collection mydb.movie using options from metadata
2017-05-23T06:48:56.702+0000 restoring test.product from /tmp/test/product.bson
2017-05-23T06:48:56.710+0000 no indexes to restore
2017-05-23T06:48:56.710+0000 finished restoring test.product (1 document)
2017-05-23T06:48:56.710+0000 restoring mydb.movie from /tmp/mydb/movie.bson
2017-05-23T06:48:56.712+0000 no indexes to restore
2017-05-23T06:48:56.712+0000 finished restoring mydb.movie (1 document)
2017-05-23T06:48:56.712+0000 done
krishna@ubuntu:~$

Conclusion:

WOW…. We have successfully backup and restore from dockerised MongoDB instance.

2 Comments

  1. Razva-Horia Pop

    Very good tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *