How to create and use MongoDB backups on Ubuntu 14.04

Introduction

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.

Preparation

To continue, you will need:

Step 1-Create a sample database

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":[]})

Step 2-Check the size of the database

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.

Step 3-Create a backup

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

Step 4-Delete database

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.

Step 5-Restore 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 conclusion

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

How to create and use MongoDB backups on Ubuntu 14.04
How to install and use Docker on Ubuntu 20.04
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install and use Docker on Ubuntu 16.04
How to install and use MySQL Workbench on Ubuntu 18.04
How to install MongoDB on Ubuntu 16.04
How to use Samba server on Ubuntu 16.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
How to install and configure NATS on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04
How to install and use Docker on CentOS 7
How to install and configure Cyberpanel on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to use Nginx's map module on Ubuntu 16.04
How to install and configure GitLab on Ubuntu 18.04
How to install and configure Ansible on Ubuntu 18.04
How to use Docker data volumes on Ubuntu 14.04
How to install and use Composer on CentOS 8
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and configure VNC on Ubuntu 18.04
How to install and configure Sphinx on Ubuntu 16.04
How to use Jenkins to build automatically on Ubuntu
How to install and configure OrientDB on Ubuntu 14.04
How to install and use Curl on CentOS 8
How to install and configure AppScale on Ubuntu 12.04
How to install and configure PostGIS on Ubuntu 14.04
How to use LVM to manage storage devices on Ubuntu 18.04
How to backup and restore Redis data on Ubuntu 14.04
How to install and use Cockpit on CentOS 8/RHEL 8
How to install theano and keras on ubuntu system
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to use hanlp in ubuntu
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04