Skip to main content
Version: 7.0

Backups creation and restoration examples

This article describes examples of creating backups in MongoDB. Backup ensures data security and the ability to restore it in case of system failure or loss.

We recommend performing daily backups of MongoDB databases and keeping copies for the last week.

The following administration utilities, installed together with MongoDB, are used for creation and restoration:

  • Backup creation — mongodump
  • Backup restoration — mongorestore

Location of administration utilities

Linux

Administration utilities are located in a directory that is added to the system PATH, making them accessible for use from the CLI command line.

Usually, the utilities are located in the following directories:

  • /usr/bin/
  • /usr/local/bin/
Location of MongoDB utilities on Linux
Windows Server

By default, MongoDB administration utilities are not located in the $PATH environment variable, so you need to search for the utilities using PowerShell.

Open PowerShell as Administrator:

Using the Start context menu
  1. Right-click the Start icon in the lower-left corner of the screen.
  2. Select "Windows PowerShell (Administrator)" from the context menu.
Using the Start menu
  1. Left-click the Start icon in the lower-left corner of the screen.
  2. Type powershell
  3. Open PowerShell as "Administrator" using the combination — Ctrl + Shift + Enter

Perform a search for the administration utilities:

$utilityPath = Get-ChildItem -Path "C:\" -Filter "mongodump.exe" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1; $utilityDirectory = $utilityPath.DirectoryName; cd $utilityDirectory
danger

If MongoDB and administration utilities were installed on a non-standard drive, you need to change the search path in the -Path parameter.

After execution, the current working directory will change to the new one where the utilities are located.

Standard Passwork Installation (Docker, PowerShell)

For the Docker build, scripts for creating and restoring MongoDB databases are used, located in the root directory of the Passwork installation:

  • /<passwork>/db-backup.sh
  • /<passwork>/db-restore.sh

For the PowerShell module, functions for creating and restoring MongoDB databases are used, executed in PowerShell as Administrator:

  • Backup-MongoDB
  • Restore-MongoDB

Examples of creating MongoDB backups

Before executing commands, create a directory where MongoDB backups will be stored:

  • Linuxmkdir /backup/
  • Windows Servermkdir C:\backup\

Standalone installation without authorization

mongodump --host localhost:27017 --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump

Standalone installation with authorization

info

The example uses a MongoDB user with the following credentials:

  • Login — adminuser
  • Password — password
mongodump --host localhost:27017 -u adminuser -p password --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump

Replica Set without authorization

mongodump --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump

Replica Set with authorization

info

The example uses a MongoDB user with the following credentials:

  • Login — adminuser
  • Password — password
mongodump --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 -u adminuser -p password --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump

Examples of restoring MongoDB backups

Standalone installation without authorization

mongorestore --host localhost:27017 --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump

Standalone installation with authorization

info

The example uses a MongoDB user with the following credentials:

  • Login — adminuser
  • Password — password
mongorestore --host localhost:27017 -u adminuser -p password --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump

Replica Set without authorization

mongorestore --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump

Replica Set with authorization

info

The example uses a MongoDB user with the following credentials:

  • Login — adminuser
  • Password — password
mongorestore --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 -u adminuser -p password --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump

Separate restoration of the Passwork database

Description

This option is described using the example of a basic MongoDB component installation. Any previously described option can be used by specifying the attribute to the restore command.

Restoration example

mongorestore --host localhost:27017 --nsInclude=pwbox.* --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump
info

After execution, only the Passwork database will be restored from the specified MongoDB backup file.

Scheduled backup configuration

Linux

To configure the frequency of database backups, it is recommended to use the Crontab event scheduler. More about Crontab and examples of its use — Basic Cron Information

danger

Crontab configuration for creating backups should be performed on the server where the mongodump administration utility is available.

Windows Server

Create an automation file with the extension .ps1 and specify the following:

# Get current time
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"

# Create MongoDB backup
mongodump --host localhost:27017 --archive="C:\backup\mongo-$timestamp.dump"
danger

Note:

  • If MongoDB and administration utilities were installed on a non-standard drive, you need to change the search path in the -Path parameter;
  • Before running the task in the Task Scheduler, you need to create the directory where MongoDB backups will be stored.

Open PowerShell as Administrator:

  • Right-click the Start icon in the lower-left corner of the screen;
  • Select Windows PowerShell (Administrator) from the context menu.

Create a task in Task Scheduler to create MongoDB backups:

# Action to run the backup.example.ps1 script using PowerShell
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\backup.example.ps1"

# Run daily at 2:00 AM
$trigger = New-ScheduledTaskTrigger -Daily -At "02:00AM"

# Task settings
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -StartWhenAvailable -Hidden -Priority 5

# Create the task
Register-ScheduledTask -Action $action -TaskName "mongodb-example-backups" -Settings $settings -Trigger $trigger -RunLevel Highest -User "username" -Password "password" -Force
danger

Note:

  • You need to change the names and locations of the automated .ps1 file to be executed;
  • It is recommended to use an administrator account to run the background task. If another user is used, make sure they have sufficient rights to perform all actions.