Backup and Restore MongoDB Databases Using Mongodump and Mongorestore – Part 1

undefined

In this article, we’ll show you the  first and most important task every database administrator / sysadmins must master “Backup and Restore your databases”. We divided this article into two parts. In part 1, we’ll use mongodump to perform a backup for MongoDB databases for a single database, several databases, and all the databases. Also we’ll backup a single collection from a database. In part 2, we’ll use mongorestore to restore the different types of backups to ensure that it’s in a usable format and has captured the data in a consistent state. We gives you some examples in each part of this article.

mongodump is the MongoDB backup utility that is supplied as part of the MongoDB distribution. It works as a regular client by querying a MongoDB instance and writing all the read documents to the disk.

mongorestore is an useful tool to restore the MongoDB backup that was taken using mongodump utility.

Let’s start our two parts.

Part 1: Performing a MongoDB Database Backup.

There are various options to take backups of MongoDB database. We’ll use mongodump command to take a backup for all databases, a single database, and a single collection from a database. We’ll perform a hot backup “With MongoDB instance is running”, Just stop the MongoDB instance before running the following examples to perform a cold backup.

Before we start with performing backups, we need to see the syntax and options of mongodump command:

Mongodump Syntax and Options

Here’s the basic syntax and options of the mongodump command:

Usage:

$ mongodump <options>

Very simple usage, it exports the content of a running server into .bson files.

Options:

The following table lists the most used options, which used through this post.

Option Usage
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port> server port (can also use –host hostname:port)
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user’s credentials
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
-o, --out=<directory-path> output directory, or ‘-‘ for stdout (defaults to ‘dump’)
--gzip compress archive our collection output with Gzip
--oplog use oplog for taking a point-in-time snapshot

These are the most common options I use in my backups. Let’s continue to our examples.

 

  • Backup All Databases

If you need to backup all the databases on your MongoDB instance, Here’s two examples we provide to backup all the MongoDB databases.

Example 1: Backup all database in the current path and with no authentication used

In case you do not use authentication with your mongoDB server, to backup all databases you just need to run following command:

 # mongodump 

A very simple command, with no options used, it’ll backup all the databases and put them in a new created “dump” directory in the current path, here’s the output of the command and the created directory:

2017-02-27T07:53:25.960+0200 writing admin.system.users to 
2017-02-27T07:53:25.969+0200 done dumping admin.system.users (1 document)
2017-02-27T07:53:25.969+0200 writing admin.system.version to 
2017-02-27T07:53:25.999+0200 done dumping admin.system.version (2 documents)
2017-02-27T07:53:25.999+0200 writing elcinema_nosql_staging.order_logs to 
2017-02-27T07:53:28.819+0200 [#.......................] elcinema_nosql_staging.order_logs 314878/5394411 (5.8%)
2017-02-27T07:53:31.819+0200 [##......................] elcinema_nosql_staging.order_logs 602167/5394411 (11.2%)
2017-02-27T07:53:34.819+0200 [###.....................] elcinema_nosql_staging.order_logs 751163/5394411 (13.9%)
......
2017-02-27T07:54:52.819+0200 [#######################.] elcinema_nosql_staging.order_logs 5284552/5394411 (98.0%)
2017-02-27T07:54:55.228+0200 [########################] elcinema_nosql_staging.order_logs 5394411/5394411 (100.0%)
2017-02-27T07:54:56.014+0200 done dumping elcinema_nosql_staging.order_logs (5394411 documents)

As you see, it performs the backup on two databases “admin and elcinema_nosql_staging“, let’s check on the created directories and their sizes:

# ls 
dump 
# ls dump/
admin elcinema_nosql_staging
# du -sh dump/*
20K dump/admin
4.9G dump/elcinema_nosql_staging

Yes, it backed up all the existing databases. And this is the basic all databases backup command.

Example 2: Backup all databases with a gzip compression in a selected backup location

Here, we’ll add some options to our backup command. In case you use authentication with your mongoDB server, to backup all databases with compression and store the compressed backup in a selected location, you just need to run following command:

 # mongodump -u<Your_User> -p<Your_Password> --gzip -o Semari

Very simple command, just we added the authentication options along with the gzip compression option and the storage destination option. It’ll backup all the databases and put the compressed backup in a new created “Semari” directory in the current path, here’s the output of the command and the created directory:

2017-02-27T08:15:33.190+0200 writing admin.system.users to 
2017-02-27T08:15:33.208+0200 done dumping admin.system.users (1 document)
2017-02-27T08:15:33.208+0200 writing admin.system.version to 
2017-02-27T08:15:33.208+0200 done dumping admin.system.version (2 documents)
2017-02-27T08:15:33.208+0200 writing elcinema_nosql_staging.order_logs to 
2017-02-27T08:15:35.962+0200 [........................] elcinema_nosql_staging.order_logs 153038/5394411 (2.8%)
2017-02-27T08:15:38.962+0200 [#.......................] elcinema_nosql_staging.order_logs 349653/5394411 (6.5%)
.......
2017-02-27T08:17:44.965+0200 [#######################.] elcinema_nosql_staging.order_logs 5276005/5394411 (97.8%)
2017-02-27T08:17:47.933+0200 [########################] elcinema_nosql_staging.order_logs 5394411/5394411 (100.0%)
2017-02-27T08:17:47.933+0200 done dumping elcinema_nosql_staging.order_logs (5394411 documents)

As you see, it performs the backup on two databases “admin and elcinema_nosql_staging“, let’s check on the created directories and their sizes with compression:

# ls 
dump Semari
# ls Semari/
admin elcinema_nosql_staging
# du -sh Semari/*
20K Semari/admin
312M Semari/elcinema_nosql_staging

Yes, it backed up all the existing databases. Compression is amazing option you need to use as it decreased the size of the databases. for this example it decreased the size of elcinema_nosql_staging database from 4.9 GB to 312 MB which saves your storage and of course your money.

 

  • Backup a Single Database

We simply can backup a single database by adding -d, --db=<database-name> option, this will only backup one database. In the following two examples, we backup the elcinema_nosql_staging database

Example 1: Backup a single database  with no authentication used

In case you do not use authentication with your mongoDB server, to backup a single database, you just need to run following command:

# mongodump -d elcinema_nosql_staging --gzip -o OneBD_no_auth

We used a new option -d  to specify the database name, also we are using compression and store our backed up database in a a new location. Here’s the output of the command and the created directory:

2017-02-27T08:54:08.717+0200 writing elcinema_nosql_staging.order_logs to 
2017-02-27T08:54:11.695+0200 [........................] elcinema_nosql_staging.order_logs 179421/5394411 (3.3%)
....
2017-02-27T08:56:20.694+0200 [#######################.] elcinema_nosql_staging.order_logs 5266618/5394411 (97.6%)
2017-02-27T08:56:23.695+0200 [#######################.] elcinema_nosql_staging.order_logs 5376985/5394411 (99.7%)
2017-02-27T08:56:24.026+0200 [########################] elcinema_nosql_staging.order_logs 5394411/5394411 (100.0%)
2017-02-27T08:56:24.026+0200 done dumping elcinema_nosql_staging.order_logs (5394411 documents)

As you see, it performs the backup on only one database  ” elcinema_nosql_staging“, let’s check on the created directories and their sizes:

# ls OneBD_no_auth/
elcinema_nosql_staging
# ls OneBD_no_auth/elcinema_nosql_staging/
order_logs.bson.gz order_logs.metadata.json.gz
# du -sh OneBD_no_auth/elcinema_nosql_staging/
312M OneBD_no_auth/elcinema_nosql_staging/

Yes, it backed up one database with compression.

Example 2: Backup a single database  with authentication used

In case you  use authentication with your mongoDB server, you must use --authenticationDatabase=<database-name>  option along with the username and password options otherwise you will face an error.

To backup a single database with authentication enabled on the MongoDB instance, you just need to run following command:

# mongodump -u<Your_User> -p<Your_Password> --authenticationDatabase=admin -d elcinema_nosql_staging --gzip -o OneBD

We used a new option --authenticationDatabase=<database-name> to specify the authentication database name. Here’s the output of the command and the created directory:

2017-02-27T08:54:08.717+0200 writing elcinema_nosql_staging.order_logs to 
2017-02-27T08:54:11.695+0200 [........................] elcinema_nosql_staging.order_logs 179421/5394411 (3.3%)
....
2017-02-27T08:56:20.694+0200 [#######################.] elcinema_nosql_staging.order_logs 5266618/5394411 (97.6%)
2017-02-27T08:56:23.695+0200 [#######################.] elcinema_nosql_staging.order_logs 5376985/5394411 (99.7%)
2017-02-27T08:56:24.026+0200 [########################] elcinema_nosql_staging.order_logs 5394411/5394411 (100.0%)
2017-02-27T08:56:24.026+0200 done dumping elcinema_nosql_staging.order_logs (5394411 documents)

As you see, it performs the backup on only one database  ” elcinema_nosql_staging“, let’s check on the created directories and their sizes:

# ls OneBD/
elcinema_nosql_staging
# ls OneBD/elcinema_nosql_staging/
order_logs.bson.gz order_logs.metadata.json.gz
# du -sh OneBD/elcinema_nosql_staging/
312M OneBD/elcinema_nosql_staging/

Yes, it backed up one database with compression.

Hints:

1. For a single database backup with authentication, you must use --authenticationDatabase=<database-name> otherwise the following error will appear:
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.

2. If you didn't specify the username and password for MongoDB instance with authentication enabled, then you will see the following error:
Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }

Check the next page for the rest of the backup and restore methods.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

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