Create (partial) backup of local MongoDB database

If one is developing some web application that uses a MongoDB database and wants to demonstrate the application, one might need some exemplary test data. This data needs to be created beforehand and it might be necessary to whipe the database between the creation of the test data and the demo. Or one might want to check the data into some version control systems for other developers to use this data to explore the application. The problem now is that one might have the data on a locally running MongoDB and wants to share this data with others or back it up for later uasge. Also one might only want to create a backup of ceratin parts of the database, like for example only of the test users, but no the test content createt for those users. It would be a very lengthy process to delete all the content by hand and then backup the whole database
3 answers

Use mongodump and mongorestore to backup and restore MongoDB databases

In order to make a backup of the local MongoDB one can use the mongodump program. This program is a command line tool that allows to dump the complete or partial content of a MongoDB instance to some file.
To create such a dump one simply calls the mongodump binary. It takes a multitude of different parameters, where the most importatnt ones are:

  • --host HOSTNAME:PORT which defaults to localhost:27017
  • --username USERNAME
  • --password PASSWORD
  • --db DATABASE_NAME if not specified all databases are dumped
  • --collection COLLECTION_NAME needs --db to be specified, and if omitted all collections are dumped
  • --query QUERY only dumps objects matching the query
  • --out PATH if ommited the programs dumps the database to ./dump/

After dumping the database one can then use the created file(s) and copy them into a directory under version control or simply send them to other people. If one receives such a database dump one can use the mongorestore program in order to restore the database.
Again one simply has to call the mongorestore binary with the desired parameters. The most important ones are the same as for the mongodump except that it does not accept a query or out parameter, instead one can specify a --dir PATH paramter to tell the program there to look for the dump files and a --drop parameter that instructs the program to drop the old database.

Use mongodump and mongorestore to backup and restore MongoDB databases

In order to make a backup of the local MongoDB one can use the mongodump program. This program is a command line tool that allows to dump the complete or partial content of a MongoDB instance to some file.
To create such a dump one simply calls the mongodump binary. It takes a multitude of different parameters, where the most importatnt ones are:

  • --host HOSTNAME:PORT which defaults to localhost:27017
  • --username USERNAME
  • --password PASSWORD
  • --db DATABASE_NAME if not specified all databases are dumped
  • --collection COLLECTION_NAME needs --db to be specified, and if omitted all collections are dumped
  • --query QUERY only dumps objects matching the query
  • --out PATH if ommited the programs dumps the database to ./dump/

After dumping the database one can then use the created file(s) and copy them into a directory under version control or simply send them to other people. If one receives such a database dump one can use the mongorestore program in order to restore the database.
Again one simply has to call the mongorestore binary with the desired parameters. The most important ones are the same as for the mongodump except that it does not accept a query or out parameter, instead one can specify a --dir PATH paramter to tell the program there to look for the dump files and a --drop parameter that instructs the program to drop the old database.

Taggings:

Use mongodump and mongorestore to backup and restore MongoDB databases

In order to make a backup of the local MongoDB one can use the mongodump program. This program is a command line tool that allows to dump the complete or partial content of a MongoDB instance to some file.
To create such a dump one simply calls the mongodump binary. It takes a multitude of different parameters, where the most importatnt ones are:

  • --host HOSTNAME:PORT which defaults to localhost:27017
  • --username USERNAME
  • --password PASSWORD
  • --db DATABASE_NAME if not specified all databases are dumped
  • --collection COLLECTION_NAME needs --db to be specified, and if omitted all collections are dumped
  • --query QUERY only dumps objects matching the query
  • --out PATH if ommited the programs dumps the database to ./dump/

After dumping the database one can then use the created file(s) and copy them into a directory under version control or simply send them to other people. If one receives such a database dump one can use the mongorestore program in order to restore the database.
Again one simply has to call the mongorestore binary with the desired parameters. The most important ones are the same as for the mongodump except that it does not accept a query or out parameter, instead one can specify a --dir PATH paramter to tell the program there to look for the dump files and a --drop parameter that instructs the program to drop the old database.