How To Use mongodump for MongoDB Backups

The need to keep backups up-to-date is essential for any organization. Backups are security measures where information can be restored or recovered in the event in the event of an emergency. Typically backups of databases are created by replicating the database employing either

  • Built-in tools
  • External backups that are specifically designed for specific needs.

MongoDB offers a variety of built-in backup options, based of your MongoDB methods of deployment you select. We’ll take a look at the options available, but we’ll demonstrate how to make use of a specific option, MongoDB mongodump for the backup procedure.

(This post is part our MongoDB Guide. Make use of the menu on the right to navigate. )

Backups built-in to MongoDB

Here are the different options available to back your data MongoDB:

MongoDB Atlas Backups

When MongoDB is hosted on the MongoDB Atlas Cloud Database Service the Altas service will automatically take incremental backups.

In addition, Altas can be used to create cloud provider snapshots which are local database snapshots created by using the cloud provider’s snapshot function.

MongoDB Cloud Manager or Ops Manager

Cloud Manager provides a cloud-hosted backup as well as monitoring and automation service that is available for MongoDB. Cloud Manager allows easy backup and restore functionality, as well as offsite backups.

Ops Manager has the same features that Cloud Manager does. Cloud Manager but it can be used as an on-premise option.

MongoDB mongodump

Mongodump It is an easy MongoDB backup tool that produces high-quality BSON files from the existing database. The backup archives can also be restored with Mongorestore, a Mongorestore utility.

Mongodump is a perfect backup solution for smaller MongoDB instances because of its simplicity of use and its portability.

Backups of the file system

In this way it is a matter of keeping copies of the primary data files that are part of the MongoDB installation. You can use snapshots when the file system allows it.

Another method is to utilize tools such as the rsync tool which allows us to directly transfer data files into an archive directory.

What exactly is MongoDB mongodump?

mongodump mongodump is a program to create backups of databases. The program can be used to create an export binary of data in the database. Mongodump is able to export information from mongod and mongos instances. making backups of:

  • A standalone, replica set
  • A sharded cluster comprising MongoDB deployments

Prior to MongoDB 4.4, mongodump was launched alongside the MongoDB server and relied on matching versions. The latest versions of mongodump are available as a separate application included in MongoDB Database Tools. Mongodump ensures compatibility for MongoDB 4.4, 4.2, 4.0 as well as 3.6.

The Mongodump utility works on all x86_64 platforms, as well as certain ARM64 PPC64LE, s390x, and PPC64 platforms. You can get the complete range of operating systems that mongodump can work with by reading the document.

Mongodump actions

The following list explains the expected behavior and the limitations for the Mongodump tool.

  • The utility mongodump directs reading operations only to primary members of replica sets, setting the default preference for reading the primary.
  • The backup process will not include any “local” database and only will capture the document without the index data. The indexes have to be rebuilt following an restoration procedure.
  • In terms of backing up only read-only views mongodump can only record metadata about views. If you want to capture documents within a view, use the “-viewsAsCollections” flag.
  • To ensure compatibility, you should use extended JSON v2.0 (Canonical) for mongodump metadata files. It is recommended that you use the equivalent versions of mongodump and mongorestore when performing restore and backup operations.
  • The mongodump command overwrites the files that are already present within the backup folder. The default backup location is the dump/folder.
  • If WiredTiger is the WiredTiger storage engine is employed in an MongoDB instance, then the result will be raw data.
  • Mongodump backup operations are dependent on memory available on the system. In the event that the backup data are bigger than the memory of the system the mongodump utility will be able to push the working data into memory.
  • When the access control is set to allow access to MongoDB, and access control is configured to allow MongoDB database, users should have sufficient access rights to each database to perform backups. MongoDB comes with a built-in backup role, which requires permissions to backup all databases.
  • MongoDB lets mongodump be used as a component of the backup strategy used for the standalone or replica set.
  • Beginning with MongoDB 4.2 Mongodump is no longer utilized as a component of the backup strategy backing up clusters with shards which have sharded operations that are in process. In these cases it is suggested to utilize a solution such as MongoDB Cloud Manager or Ops Manager to maintain the consistency for transactions across the shards.
  • The mongodump command has to be run from the shell for system commands since it is an independent utility.
  • There is no way to make incremental backups. Backups that are made will create an entire replica of your database.

MongoDB Database Tools

MongoDB Database Tools is an assortment of command-line utility that aid in maintaining and administration of an MongoDB instance. They are compatible with the following environments: MongoDB Database tools are compatible with the following environments:

  • Windows
  • Linux
  • macOS

In this article we’ll explore how to install Database Tools on a Linux server.

Checking for Database Tools

To determine if the databases tools are installed to the machine, make use of the command below.

sudo dpkg -l mongodb-database-tools

Results for Database Tools installed:

Result for Database Tools unavailable:

Installing Database Tools

If your system does not have Database Tools, here’s how to set it up.

The MongoDB download center has the most recent versions of MongoDB Database Tools. Download the most current version for your platform and type of package. In an CLI environment, you can duplicate the downloaded link, and use the wget as well as curl to download the package.

In the case below we will use to install the Database Tools Version 100.2.1 on Ubuntu as the deb package. Then, we will install it using the downloaded file.

curl -o mongodb-database-tools-ubuntu2004-x86_64-100.2.1.deb https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.2.1.deb sudo apt install ./mongodb-database-tools-ubuntu2004-x86_64-100.2.1.deb

Result:

Using MongoDB mongodump

In this article we will discuss the fundamental use of the mongodump as a standalone MongoDB instance.

Basic mongodump Syntax

Copy

mongodump  

The most simple method to make backups is to use the mongodump command with no options. It is assumed that the database is on the localhost (127.0.0.1) and running port 27017 without requirement for authentication. The backup procedure is to create a dump directory within the directory currently in.

mongodump

Result:

You can browse to the dump folder in order to check the backups we have created.

Backup of the remote MongoDB instance

It is possible to choose a host as well as a port with the connection string -uri.

Connect with the URL option:

mongodump --uri="mongodb://:" [additional options]

Connect with your host choice:

mongodump --host=":" [additional options]

Connect via the host and port options:

mongodump --host="" --port= [additional options]

The following example illustrates how to make backups to the distant MongoDB instance:

mongodump --host="10.10.10.59" --port=27017

Result:

Backups of an encrypted MongoDB instance

If we are looking to connect to an MongoDB instance using access-control, we must be able to:

  • Username
  • Password
  • Authentication database options

Authentication SyntaxCopy

mongodump --authenticationDatabase= -u= -p=[Additional options]

Let’s take a look at how to connect to an external MongoDB instance by using an account user name and password.

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword"

Result:

Selecting databases & collections

By using the -db and –collection options, you can select the existence of a database as well as a collection to be back up. The -db option is an independent option, however for a collection to be selected, a database needs to be specified. To exclude an entire collection from backup it is possible to use the option -excludeCollection.

The database you choose to select:

mongodump --db= [additional options]

Picking a collectionCopy

mongodump --db= --collection= [additional options]

Excluding collections:

mongodump --db= --excludeCollection= [additional options]

In the next example, we will define as the “vehicleinformation” collection as the sole backup destination.

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --db=vehicles --collection=vehicleinformation

Result:

Modifying the backup directory

The -out option is used to indicate which location to save the backup files.

mongodump --out= [additional options]

Change the backup directory into the “dbbackup” folder

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --out=dbbackup

Result:

The process of creating an archive file

The Mongodump utility permits users to make an archived file. The -archive option is utilized to define the type of file. If there is no file specified, it will get recorded in standard format ( stdout).

The -archive option can’t be combined to the out option.

mongodump --archive= [additional options]

This example shows how to identify an archived file.

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive

Result:

The backup is compressed

Backup documents can also be compressed by using the gzip option. This option can compress each of the JSON and BSON files.Copy

mongodump --gzip (additional options)

Let’s compress the entire MongoDB database.

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --gzip

Result:

In the article below, we’ve found out about MongoDB mongodump, and how it can be utilized to create backups and manage backups for databases.

Leave a Reply

Your email address will not be published. Required fields are marked *