In this tutorial, we will walk you through the steps to backup and restore MongoDB database.
What We Used
- Microsoft Windows 7
- MongoDB 3.2.9
1. Create Example Data
This is an optional step. If you have no data in test database, you can create some example data from countries.json before perform the backup operation.
mongoimport --drop --jsonArray -d test -c countries countries.json
The following output will be generated if import data is successful:
2016-09-30T23:14:37.027+0800 connected to: localhost
2016-09-30T23:14:37.051+0800 dropping: test.countrie
2016-09-30T23:14:37.291+0800 imported 245 documents
2. Check Data
After data imported, check the data of countries
collection.
Open Command Prompt window to connect test
database with MongoDB shell.
mongo test
Check the total records of the countries
collection.
db.countries.count()
It will return the output as below:
245
3. Backup Data
Backup test
database and export the data to C:\mongodb\backup
directory.
mongodump -h localhost --port 27017 -d test -o C:\mongodb\backup
The following output will be generated if backup data is successful:
2016-09-30T23:04:49.758+0800 writing test.countries to
2016-09-30T23:04:49.809+0800 done dumping test.countries (245 documents)
C:\mongodb\backup
directory structure:
backup └── test ├── countries.bson └── countries.metadata.json
Tip: You can ignore the -h
and --port
options if you are connect to a local MongoDB server with default port 27017
.
You can backup all databases in MongoDB server.
mongodump -h localhost --port 27017 -o C:\mongodb\backup
You can backup only countries
collection in MongoDB server.
mongodump -h localhost --port 27017 -d test -c countries -o C:\mongodb\backup
You can backup a database with authentication.
mongodump -h localhost --port 27017 -u user1 -p pass1 -d test -o C:\mongodb\backup
4. Restore Data
Restore all the data from C:\mongodb\backup
directory back to MongoDB server.
mongorestore -h localhost --port 27017 --drop C:\mongodb\backup
The following output will be generated if restore data is successful:
2016-09-30T23:40:00.779+0800 building a list of dbs and collections to restore from C:\mongodb\backup dir
2016-09-30T23:40:00.804+0800 reading metadata for test.countries from C:\mongodb\backup\test\countries.metadata.json
2016-09-30T23:40:00.936+0800 restoring test.countries from C:\mongodb\backup\test\countries.bson
2016-09-30T23:40:00.953+0800 restoring indexes for collection test.countries from metadata
2016-09-30T23:40:00.954+0800 finished restoring test.countries (245 documents)
2016-09-30T23:40:00.955+0800 done
Tip: You can ignore the -h
and --port
options if you are connect to a local MongoDB server with default port 27017
.
You can restore a database with authentication.
mongorestore -h localhost --port 27017 -u user1 -p pass1 --drop C:\mongodb\backup
5. Verify Data
After database restored , verify the data of countries
collection.
Open Command Prompt window to connect test
database with MongoDB shell.
mongo test
Check the total records of the countries
collection.
db.countries.count()
You will get the same total records count as below:
245
Summary
Congratulation! In this tutorial, you have learned how to import data, backup and restore a MongoDB database.