MongoDB : Backup and restore

In this tutorial we will see about backup and restore data in Mongodb.


Starting MongoDB Server and MongoDB Shell

1) Open a new command prompt.

2) Enter “ mongod” and then press ENTER. This is going to run mongodb in the background and we wait for connections on port 27017.

Now MongoDB server is running , leave that command prompt window open. If we close that Window then Mongodb server will get close.

3) Open a new command prompt in parallel.

4) Enter “ mongo” and then press ENTER. This is going to run mongodb shell in the background and we wait for making a connection request on port 27017.

mongo

5) Again, open a new command prompt in parallel, having the access as administrator (run it in administrator mode).

6)As in previous tutorials we have taken c as the windows folder. So, the bin directory path as follows:

C:\Program Files\MongoDB\Server\3.6\bin

Write “cd C:\Program Files\MongoDB\Server\3.6\bin” in the command prompt and press ENTER, it will switch you path to the bin directory.

7) Switch to the mongo shell command prompt window.

8) Type “show dbs” and press ENTER, it will display all the databases that we have in our system.

SHOW DBS

Local database is the default database provided by the mongodb.


Backup in MongoDB

1) Switch it to administrator command prompt window.

2) Type “mongdump” and press ENTER.

mongodump is going to create data or the backup in a folder called dump in the bin directory.  It will create one folder for each database in the dump folder inside the bin directory. Each folder inside the dump folder has the name as the database names and contails the json files of the collections in it.
mongodump


Restore Data

Let’s delete testDB database from our system.

1) Switch it to mongo shell command prompt window.

2) Type “use testDB”, and press ENTER, it will switched it to testDB database.

Write “db.dropDatabase()” and press ENTER, it will delete the testDB database.

Now, again type “show dbs” and press ENTER, it will display all the databases that we have in our system, testDB database is been deleted now.

show dbs

Steps for restoring

1) Switch again to the administrator command prompt window.

2) Type “mongorestore” and press ENTER, wait for the done message.

mongorestore

mongorestore goes inside the dump folder, checks for the databases and restores them.

Now, Switch it to mongo shell command prompt window.

Type “show dbs” and press ENTER, you will now see the testDB database.

SHOW DBS


Backup of a Single Database

1) Switch it to administration command prompt window.

2) Write “mongodump --db databaseName” and press ENTER.

It will create a folder named as databaseName in the dump folder inside the bin folder, containing the json formats of all collections in it.


Restore of a Single Database

1) Switch it to administration command prompt window.

2) Write “mongorestore --db databaseName dump/databaseName” and press ENTER.


Backup of a Single Collection

1) Switch it to administration command prompt window.

2) Write “mongodump --db databaseName –collection collectionName” and press ENTER.

It will create a folder named as databaseName in the dump folder inside the bin folder, containing the json formats of collectionName collection in it.


Restore of a Single Collection

1) Switch it to administration command prompt window.

2) Write

mongorestore --db databaseName --collection collectionName dump/databaseName/collectionName” and press ENTER.

If you have any queries regarding backup and restore in MongoDB, drop a comment below.