Data is the center of an enterprise and application runs top of this data. It’s crucial being DBA that you protect it and make it available in case of disaster. The basic and finest way to protect your organization’s data, implementing database backup and restore plan.

Backing up databases, (either in SQL or mongo world) can protect against not only accidental loss application data, corruption, hardware failures but also basic element of having disaster recovery site in case of natural disasters. It is DBA’s job as an administrator to make sure that backups are performed and that backup tapes/drives are shipped/stored in a secure location.

Mongodump is a utility for creating a binary export of the contents of a database. It can use on database or collection level. Mongodump can work either Mongod or mongos instances.

**mongodump and mongorestore are command line utilities.**

Mongodb has released a newer version of mongo backup and restore utilities, It would be helpful to know the version first, not all options works with all versions of these utilities.

>mongorestore /version

mongorestore version: r3.4.6

git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5 –if it appears blank most probably you are running a community edition.

Go version: go1.7

   os: windows

   arch: amd64

   compiler: gc

OpenSSL version: OpenSSL 1.0.1u  22 Sep 2016

>mongodump /version

mongodump version: r3.4.6

git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5 –-if it appears blank you are running a community edition.

Go version: go1.7

os: windows

arch: amd64

compiler: gc

OpenSSL version: OpenSSL 1.0.1u  22 Sep 2016

Here are the backup/restore commands with basic options and examples.

Backup mongo database

>mongodump /host hostname /port portnumber /u username /p password –authenticationDatabase admin /d databasename /out directorypath

 Example

>mongodump /host mymongohost /port 27002 /u root /p ****** –authenticationDatabase admin /d foo /out E:\MongoDump\July

Backup mongo database collection level

>mongodump /host hostname /port portnumber /u username /p password –authenticationDatabase admin /d databasename /c collectionname /out directorypath

 Where?

Where hostname is the name of the Mongod instance, which has the database you want to backup. In case of replica, –host is prefixed by the replica set name, mongodump reads from the primary replica set member by default.

/port use this if you have mongo deployment other than default port 27017.

 Restore mongo database

>mongorestore –db databasename -u username -p password –port portnumber  –authenticationDatabase databasename  –dir E directorypath –drop

 Restore mongo database collection only

>mongorestore –db databasename –collection collectionname -u username -p password –port portnumber  –authenticationDatabase databasename  –dir E directorypath.filename.bson  –drop

 Example

>mongorestore –db foo –collection foo1 –u root -p password –port 27002 –dir E:\Mong

dump\july\foo.bson –authenticationDatabase admin –drop

 Where?

 If user is authenticated from admin use –authenticationDatabase admin , use –authenticationDatabase userdatabaename where user could be authenticated.

 Caution!

mongodump overwrites output files if they exist in the backup data folder.

mongod instance that uses the WiredTiger storage engine, mongodump outputs uncompressed data.

Advertisement

One thought on “All about Mongo Backups & Restore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s