MongoDB Backup and Restore

MongoDB is one of the most popular NoSQL database engines. MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use

JSON

JavaScript Object Notation (JSON) is an open, human and machine-readable standard that facilitates data interchange, and along with XML is the main format for data interchange used on the modern web. JSON supports all the basic data types you’d expect: numbers, strings, and boolean values, as well as arrays and hashes.

Binary JSON (BSON)

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.

MongoDB, BSON, and JSON

The MongoDB BSON implementation is lightweight, fast and highly traversable. Like JSON, MongoDB’s BSON implementation supports embedding objects and arrays within other objects and arrays – MongoDB can even ‘reach inside’ BSON objects to build indexes and match objects against query expressions on both top-level and nested BSON keys. This means that MongoDB gives users the ease of use and flexibility of JSON documents together with the speed and richness of a lightweight binary format.

MongoDB backup methods

mongodump
mongorestore
mongoexport
mongoimport

mongodump

Backup using mongodump.

-v, –verbose = verbose, extend the no. of v to get more detailed verbose e.g. -vvvvv or –verbose=N
-o, –out = output dierctory defaults to dump
-h, –host = <hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
-u, –username = <username> username for authentication
-p, –password = <password> password for authentication
–port = <port> server port (can also use –host hostname:port)
–gzip = compress archive our collection output with Gzip

mongodump -v --out=/home/krishna/Documents/mongodump

mongodump -v --db=<database-name> --collection=<collection-name> --out=/home/krishna/Documents/mongodump

mongorestore

Restore using mongodump.

-v, –verbose = verbose, extend the no. of v to get more detailed verbose e.g. -vvvvv or –verbose=N
-o, –out = output dierctory defaults to dump
-h, –host = <hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
-u, –username = <username> username for authentication
-p, –password = <password> password for authentication
-d, –db = <database-name> database to use
-c, –collection = <collection-name> collection to use
–port = <port> server port (can also use –host hostname:port)
–gzip = compress archive our collection output with Gzip
–drop = drop each collection before import

mongorestore -v /home/krishna/Documents/

mongorestore -v --drop --db=<database-name> --collection=<collection-name> /home/krishna/Documents/

mongoexport

mongoexport -vv --db <databasename> --collection <collection name> --type=<json> --out=outdatafile.json

mongoimport

mongoexport -vv --db <databasename> --collection <collection name> --type=<json> outdatafile.json

Conclusion

We have successfully took the mongodb backup.

Leave a Reply

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