EMAILHow to Backup & Restore the Data using Mongodump

How to Backup & Restore the Data using Mongodump

How to Backup & Restore the Data using Mongodump

Mongodump is a simple MongoDB backup tool that generates high-quality BSON files inside the MongoDB database. It is a good backup option for small MongoDB instances due to its ease of use and portability. This tool offers commands for backing up data at the query, collection, or database level. Furthermore, MongoDB has a complimentary tool called mongorestore, which allows you to restore data from distributed backups to a new or existing MongoDB database.

So, now let’s follow the steps to backup and restore data using the Mongodump database command.

  1. Create Direct Backups

To run mongodump, use the syntax provided in the system command line as shown below:

mongodump <options> <connection-string>

This structure also allows you to connect to a Mongo database using the -uri command followed by a formatted string or flag, such as -user, -db, or -password. However, you cannot use several flags in a single command.

You can use the default settings to create a Mongo Backup with the Mongodump command:

Mongodump

This will assume that the database is on port 27017 and located on localhost (127.0.0.1), without any authentication. A dump folder will be created in the current directory during the backup procedure.

 

  1. Backup a Remote MongoDB Instance

The –uri connection string allows you to specify a host and a port.

Use the uri option to connect:

mongodump --uri="mongodb://<host URL/IP>:<Port>" [additional options]
Connect with the host option:
mongodump --host="<host URL/IP>:<Port>" [additional options]
Connect with the host and port options:
mongodump --host="<host URL/IP>" --port=<Port> [additional options]

The below code shows how to create a backup of the remote MongoDB instance:

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

 

  1. Backup a Secure MongoDB Instance

To ensure secure data backups, MongoDB’s Mongodump command enables the implementation of access control mechanisms. To do this, you need to provide a Username, Password, and specify Authentication options using the following syntax:

mongodump --authenticationDatabase=<Database> -u=<Username> -p=<Password> [additional options]

For example, you can use the following command to connect to a remote MongoDB instance with a username and password:

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

 

  1. Select the Databases & Collections

By using the -db and -collection options, you can specify which databases and collections should be backed up. The -db option can be performed independently, but you must specify a database to select a collection. You can also remove a collection from the backup process by using the      -excludeCollection option.

To select a Specify database:

mongodump  --db=<Backup Target - Database> [additional options]

To select a collection:

mongodump  --db=<Backup Target - Database> --collection=<Collection Name> [additional options]

Run the following command to exclude the collection:

mongodump  --db=<Backup Target - Database> --excludeCollection=<Collection Name> [additional options]

 

  1. Changing the Backup Directory

The -out option specifies the location of the backup folder as follows:

mongodump --out=<Directory Location> [additional options]

To change the backup directory to the “dbbackup” folder, run the following command:

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

 

  1. Create an Archive File

The Mongodump utility allows you to build an archive file. Now, the -archive option can be used to specify a file and if there is no file is provided, then the output will be in the standard output (stdout).

Remember that you cannot use both the -archive and -out options together.

mongodump --archive=<file> [additional options]

Run the following command to easily specify any archive file:

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

 

  1. Compressing the MongoDB Backup

Now let us see the process to compress these files. The backup files can be compressed with the -gzip option and it will compress the individual JSON and BSON files with the following command:

mongodump --gzip [additional options]

Compress the complete MongoDB database with the following command:

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

 

  1. Database Restoring

Along with backups, MongoDB also offers the facility to restore your data. The Mongorestore command will load data from Mongodumb backups and restore your Mongo database. But, if a document’s id already exists in the database, the mongorestore command cannot overwrite it. Alternatively, Mongorestore will either create a new database or add data to an existing one.

The Mongorestore command requires specific the path to your dump directory:

mongorestore dump/

To restore it in your local environment, you can also isolate and import the db1 database with the following command:

mongorestore --db=redbase --nsInclude="db1.*" dump/

This will restore all db1 collections that were previously dumped. But this will not affect the data in db2, even though both the dn1 and db2 are saved in the same dump directory.

  • In the Mongostore command, you must provide the -uri flag or standard connection flags.
  • In the Mongostore command, to restore different collections, use the -nsInclude option. It allows users to select a namespace when restoring the database collections.

You can now follow these steps to efficiently backup and restore your MongoDB databases, ensuring data security and peace of mind. Whether you’re creating direct backups, backing up remote instances, or securing your backups with authentication mechanisms, Mongodump provides comprehensive support for all your MongoDB backup needs. If you need any tech assistance – Get Tech Support

To get more updates you can follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox

Latest news

WordPress Accessibility Day, October 9-10, 2024

The internet can be accessible to anyone if web developers make accessibility options a priority. For this reason, GreenGeeks...

Silvio Di Benedetto – Microsoft Copilot for Security: configuration and usage

Questo articolo è disponibile anche in lingua italiana, al seguente link – Microsoft Copilot for Security: get started ed...

The best free SEO analysis

As you continue to go on a long journey with SEO, you should do regular audits and see your...

TypeScript 5.7 improves error reporting

Also in TypeScript 5.7 is a new compiler option, --rewriteRelativeImportExtensions. When an import path is relative and ends with...

Looking into the next decade for the CIO

As we enter the next decade, I thought I would take a slightly unconventional approach to the top eight...

Must read

Top 10 CIO Trends for 2019

As we get ready to close out 2018 and...

Are the cloud wars over or just getting started?

One of the biggest opportunities for enterprises large and...

You might also likeRELATED
Recommended to you