Taking backups and importing data for mongodb
Posted By : Akash Sharma | 06-Sep-2013
In this blog I am going to share the details of how to import mongodb database.
There are two ways to import data from mongodb database from one server to another server.In each case we have first get the data from database in a particular file from one server and then move it to database of another server.
1. mongodump - mongorestore
2. mongoimport - mongoexport
The major difference in the two techniques is the type of file formats created and understood by the tools.
mongodump - mongorestore work on BSON file formats.
mongoexport - mongoimport work on JSON , CSV , TSV files.
mongodump - mongorestore creates the dump of full database system in one go.
mongoexport - mongoimport creates data dump on per collection basis.
So while working on export - import you have to first manually export all collections from one database and then import it to other database for each collection.
Lets start first with mongodump - mongorestore scripts.
mongodump --host <server_ip>:<port_number> --db <database_name> -u <username> -p <password>
By default mongodump will try to connect to port_number will be 27017 (optional).
You have to specify username and password of your database in -u and -p options.
This will create a dump folder and a database data dump inside it.
e.g: ./dump/database_name
Now to move this data to another mongodb server you have to use mongorestore.
mongorestore -h <target_server_ip> -u <username> -p <password> dump/database_name/
This will write database dump binary data to target_server_ip mongodb instance.
If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.mongorestore recreates indexes recorded by mongodump.
You might get an error like this while restore:
authentication failed: { errmsg: "auth fails", ok: 0.0 }
For this you can add username and password of restore command to admin database of target_server_ip mongodb.
>use admin >db.addUser(<username>,<password>)
Now moving to mongoimport - mongoexport
mongoexport is a utility that produces a JSON or CSV export of data stored in a MongoDB instance.
NOTE
Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information.
mongoexport -c <collection> -d <database_name> -u <username> -p <password> --file collection.json
This will create a file collection.json which contains data of specified collection.
Now to import this collection to the target mongodb instance we will use mongoimport.
The mongoimport tool provides a route to import content from a JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
mongoimport -c <collection> -d <database_name> -u <username> -p <password> --file collection.json
Akash Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis