Website logo
⌘K
Technical documentation
Installation manuals
Technical requirements
Standard installation on Linux
Standard installation on Windows Server
Manual installation on Linux
Manual installation on Windows Server
Administration
Migration to Passwork 6
How to update Passwork
Configuring background tasks
Mapping LDAP security groups with roles in Passwork
Mobile applications
Configuring SMTP for Windows Server
What to do in case of errors
About config.ini
Emergency mode
Import from Bitwarden via API
API 4.0
Linux
MongoDB
Legacy
Updates
Help center
Docs powered by Archbee
Website logo
Help center
MongoDB

Backup management

16min

MongoDB Backup

Preparation

You can create database backups using the mongodump utility. And restore - using the utility mongorestore.

Both utilities come with a MongoDB database.

Windows

Find the folder where the database is installed (usually it is C:\Program Files\MongoDB\Server\4.2\bin\).

Linux

These utilities are available from the command line after the database is installed.

Use a command line to work with the utilities.

Creating backup

Create an empty directory and open it in the command line. Run:

Shell
mongodump


The utility will connect to the local database, create a dump folder and save dumps of all databases. Save the dump folder to your backup storage.

For more fine-tuning, see the detailed information about mongodump - https://docs.mongodb.com/manual/reference/program/mongodump/

Recovery

Open a command line in the folder with the dump folder dump. Execute

Shell
mongorestore --drop dump


The utility will connect to the local database and restore all databases from backups.

MongoDB is a widely used NoSQL database management system for building and deploying applications. It is an open-source, document-oriented database that provides high performance, scalability, and flexibility. One of the most important tasks when working with MongoDB is backing up and restoring your databases. This is where the utilities mongodump and mongorestore come in.

To backup your MongoDB databases, you can use mongodump. To do so, create an empty directory and open it in the command line. Then, run the following command:

Shell
mongodump



The utility will connect to the local database, create a dump folder, and save dumps of all databases. You can then save the dump folder to your backup storage.

For example, to create a backup of a specific database, you can use the following command:

Shell
mongodump --db myDatabase



This will create a backup of the myDatabase database.

To create a backup of a specific collection within a database, you can use the following command:

Shell
mongodump --db myDatabase --collection myCollection



This will create a backup of the myCollection collection within the myDatabase database.

For more advanced backup options, refer to the MongoDB documentation, which provides detailed information on how to backup a specific database, exclude certain collections or collections with certain characteristics, and compress the output.

To restore a MongoDB database from backups, you can use mongorestore. To do so, open a command line in the folder with the dump folder (named "dump" by default), and execute the following command:

Shell
mongorestore --drop dump



The utility will connect to the local database and restore all databases from backups.

For example, to restore a specific database from a backup, you can use the following command:

Shell
mongorestore --drop --db myDatabase dump/myDatabase



This will restore the myDatabase database from the backup located in the dump/myDatabase folder.

For more information about backup and recovery, refer to the MongoDB documentation, which provides detailed information on backup strategies, disaster recovery, and more. The documentation also includes frequently asked questions about backing up and restoring MongoDB databases.

In summary, backup and recovery are important aspects of database management. With tools like mongodump and mongorestore, you can easily create and restore backups of your MongoDB databases. The MongoDB documentation provides detailed information on how to use these utilities and best practices for backup and recovery.

To create a MongoDB dump and archive it, you can use the mongodump utility and then create a compressed archive of the resulting dump directory.

To create a dump, execute the following command in an empty directory:

Shell
mongodump



This will create a dump directory containing the backup data.

Next, you can create a compressed archive of the dump directory. On Linux, you can use the tar command to create a .tar.gz archive:

Shell
tar -czvf my-archive.tar.gz dump/



On Windows, you can use a tool like 7-Zip to create a .zip archive of the dump directory.

You can then save the resulting archive to your backup storage.

To restore a database from the archive, first extract the archive to a directory using the appropriate tool for your platform. Then, use the mongorestore utility to restore the database:

Shell
mongorestore --drop dump/



This will restore the database from the dump directory.

Note that the --drop option is used to drop the existing database before restoring the backup. This is necessary to ensure that the restored database is identical to the original database. If you do not use the --drop option, mongorestore will only add missing documents to existing collections and will not remove any existing documents that are not present in the backup.

To create a MongoDB dump and archive it, you can use the mongodump utility and then create a compressed archive of the resulting dump directory. To encrypt the archive, you can use a tool like openssl.

To create a dump, execute the following command in an empty directory:

Shell
mongodump



This will create a dump directory containing the backup data.

Next, you can create a compressed archive of the dump directory. On Linux, you can use the tar command to create a .tar.gz archive:

Shell
tar -czvf my-archive.tar.gz dump/



On Windows, you can use a tool like 7-Zip to create a .zip archive of the dump directory.

To encrypt the archive, you can use the openssl command. First, generate a key and an initialization vector (IV):

Shell
openssl enc -aes-256-cbc -k my-secret-key -P -md sha1



This will generate a key and an IV. Note down the values of key and iv.

Next, encrypt the archive using the key and IV:

Shell
openssl enc -aes-256-cbc -salt -in my-archive.tar.gz -out my-archive.tar.gz.enc -K <key> -iv <iv>



This will create an encrypted archive my-archive.tar.gz.enc.

To decrypt the archive, use the following command:

Shell
openssl enc -aes-256-cbc -d -in my-archive.tar.gz.enc -out my-archive.tar.gz -K <key> -iv <iv>



This will create a decrypted archive my-archive.tar.gz.

Note that you should keep your key and IV secret and secure. If you lose them, you won't be able to decrypt your backup.

Finally, you can save the resulting encrypted archive to your backup storage.

To set up recurrent backups for your MongoDB databases, you can use a cron job on Linux or a scheduled task on Windows.

First, create a script to run the mongodump command and save the resulting dump directory to your backup storage. For example, you can create a script named backup.sh with the following content:

Shell
#!/bin/bash
mongodump
tar -czvf my-archive.tar.gz dump/
cp my-archive.tar.gz /path/to/backup/storage/



This script will create a dump directory using mongodump, create a compressed archive of the dump directory using tar, and copy the resulting archive to your backup storage.

Next, set up a cron job or scheduled task to run the script at the desired interval (e.g. daily, weekly, etc.). For example, to set up a daily backup on Linux, you can create a cron job by running the following command:

Shell
crontab -e



This will open the cron table in your default text editor. Add the following line to the end of the file:

Shell
0 0 * * * /path/to/backup.sh



This will run the backup.sh script every day at midnight.

On Windows, you can set up a scheduled task using the Task Scheduler. Open the Task Scheduler and create a new task. Set the trigger to run at the desired interval and set the action to run the backup.sh script.

For more information on cron jobs and scheduled tasks, refer to the documentation for your operating system.

In summary, to set up recurrent backups for your MongoDB databases, you can create a script to run the mongodump command and save the resulting dump directory to your backup storage, and then set up a cron job or scheduled task to run the script at the desired interval.

Updated 22 Nov 2023
Did this page help you?
PREVIOUS
A guide to Cron Job on Linux
NEXT
MongoDB authorization
Docs powered by Archbee
TABLE OF CONTENTS
MongoDB Backup
Preparation
Windows
Linux
Creating backup
Recovery
Docs powered by Archbee