Many modern web application developers today choose to use NoSQL databases in their projects, and MongoDB is usually their first choice. If you use MongoDB in a production scenario, it is very important to regularly create backups to avoid data loss. Fortunately, MongoDB provides simple command line tools to create and use backups. This tutorial will show you how to use these tools.
To understand how backups work without tampering with an existing database, this tutorial will first guide you through creating a new database and adding a small amount of data to it. Then you will create a backup of the database, then delete the database and use the backup to restore it.
To continue, you will need:
Creating a backup of an empty database is not very useful, so in this step, we will create a sample database and add some data to it.
The easiest way to interact with MongoDB instances is to use the mongo
shell. Use the mongo
command to open it.
mongo
After getting the MongoDB prompt, use the helper use
to create a new database named myDatabase.
use myDatabase
output
switched to db myDatabase
All data in the MongoDB database should belong to collection. However, you don't have to explicitly create a collection. When using the insert
method to write a collection that does not exist, the collection will be created automatically before writing the data.
You can use the following code to use the insert
method to add three small documents to a collection named MyCollection.
db.myCollection.insert([{'name':'Alice','age':30},{'name':'Bill','age':25},{'name':'Bob','age':35}]);
If the insertion is successful, you will see the following message:
BulkWriteResult({"writeErrors":[],"writeConcernErrors":[],"nInserted":3,"nUpserted":0,"nMatched":0,"nModified":0,"nRemoved":0,"upserted":[]})
Now that you have a database with data, you can create a backup for it. However, if you have a large database, the backup will be large, and to avoid the risk of running out of storage space, thereby slowing down or crashing the server, you should check the size of the database before creating the backup.
You can use the stats
method and check the value of the dataSize
key to know the size of the database (in bytes).
db.stats().dataSize;
For the current database, the value of dataSize
will be a small number:
output
592
Please note that the value of dataSize
is only a rough estimate of the backup size.
To create a backup, you can use a command-line utility called mongodump
. By default, mongodump
will create a backup of all databases present in the MongoDB instance. To create a backup of a specific database, you must use the -d
option and specify the name of the database. In addition, to let mongodump
know where to store the backup, you must use the -o
option and specify the path.
If you are still in the mongo
shell, please press CTRL+D
to exit.
Enter the following command to create a backup of myDatabase and store it in ~/backups/first_backup
:
mongodump -d myDatabase -o ~/backups/first_backup
If the backup is created successfully, you will see the following log message:
2015- 11- 24 T18:11:58.590-0500 writing myDatabase.myCollection to /home/me/backups/first_backup/myDatabase/myCollection.bson
2015- 11- 24 T18:11:58.591-0500 writing myDatabase.myCollection metadata to /home/me/backups/first_backup/myDatabase/myCollection.metadata.json
2015- 11- 24 T18:11:58.592-0500 done dumping myDatabase.myCollection(3 documents)2015-11-24T18:11:58.592-0500 writing myDatabase.system.indexes to /home/me/backups/first_backup/myDatabase/system.indexes.bson
Note that the backup is not a single file; it is actually a directory with the following structure:
first_backup
└── myDatabase
├── myCollection.bson
├── myCollection.metadata.json
└── system.indexes.bson
To test the backup you created, you can use a MongoDB instance running on a different server, or you can delete the database on the current server. In this tutorial, we will perform the latter.
Open the mongo
shell and connect to myDatabase.
mongo myDatabase
Use the dropDatabase` method to delete the database.
db.dropDatabase();
If the deletion is successful, you will see the following message:
{" dropped":"myDatabase","ok":1}
You can now use the collection method find
to see if all the previously inserted data has disappeared.
db.myCollection.find();
There is no output from this command because there is no data to display in the database.
To restore the database using a backup created with mongodump
, another command line utility called mongorestore
can be used. Before using it, press CTRL+D
to exit the mongo
shell.
Using mongorestore
is very simple. All it needs is the path to the directory containing the backup. Here is how to restore the database using the backup stored in the location of ~/backupts/first_backup
:
mongorestore ~/backups/first_backup/
If the restore operation is successful, you will see the following log message:
2015- 11- 24 T18:27:04.250-0500 building a list of dbs and collections to restore from/home/me/backups/first_backup/ dir
2015- 11- 24 T18:27:04.251-0500 reading metadata file from/home/me/backups/first_backup/myDatabase/myCollection.metadata.json
2015- 11- 24 T18:27:04.252-0500 restoring myDatabase.myCollection from file /home/me/backups/first_backup/myDatabase/myCollection.bson
2015- 11- 24 T18:27:04.309-0500 restoring indexes for collection myDatabase.myCollection from metadata
2015- 11- 24 T18:27:04.310-0500 finished restoring myDatabase.myCollection(3 documents)2015-11-24T18:27:04.310-0500 done
To check the restored data, first open the mongo
shell and connect to myDatabase
.
mongo myDatabase
Then, call the find
method in your collection
.
db.myCollection.find();
If all goes well, you should now be able to see all the data inserted earlier.
output
{"_ id":ObjectId("5654e76f21299039c2ba8720"),"name":"Alice","age":30}{"_id":ObjectId("5654e76f21299039c2ba8721"),"name":"Bill","age":25}{"_id":ObjectId("5654e76f21299039c2ba8722"),"name":"Bob","age":35}
In this tutorial, you learned how to use mongodump
and mongorestore
to backup and restore MongoDB databases. Please note that creating a backup is an expensive operation and may degrade the performance of the MongoDB instance. Therefore, it is recommended that you only create backups during off-peak hours.
To learn more about creating and using MongoDB backup related tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How to Create and Use MongoDB Backups on Ubuntu 14.04"
Recommended Posts