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
Admin utilities location
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/

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
Using the Start menu
powershellCtrl + 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
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-MongoDBRestore-MongoDB
Examples of creating MongoDB backups
Before executing commands, create a directory where MongoDB backups will be stored:
- Linux —
mkdir /backup/ - Windows Server —
mkdir C:\backup\
Standalone installation without authorization
- Linux
- Windows Server
mongodump --host localhost:27017 --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
.\mongodump --host localhost:27017 --archive="C:\backup\mongo-$timestamp.dump"
Standalone installation with authorization
The example uses a MongoDB user with the following credentials:
- Login — adminuser
- Password — password
- Linux
- Windows Server
mongodump --host localhost:27017 -u adminuser -p password --archive=/backup/mongo-$(date "+%Y-%m-%d_%H:%M").dump
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
.\mongodump --host localhost:27017 -u adminuser -p password --archive="C:\backup\mongo-$timestamp.dump"
Replica Set without authorization
- Linux
- Windows Server
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
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
.\mongodump --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 --archive="C:\backup\mongo-$timestamp.dump"
Replica Set with authorization
The example uses a MongoDB user with the following credentials:
- Login — adminuser
- Password — password
- Linux
- Windows Server
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
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm"
.\mongodump --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 -u adminuser -p password --archive="C:\backup\mongo-$timestamp.dump"
Examples of restoring MongoDB backups
Standalone installation without authorization
- Linux
- Windows Server
mongorestore --host localhost:27017 --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump
.\mongorestore --host localhost:27017 --drop --archive="C:\backup\mongo-xxxx-xx-xx_xx-xx.dump"
Standalone installation with authorization
The example uses a MongoDB user with the following credentials:
- Login — adminuser
- Password — password
- Linux
- Windows Server
mongorestore --host localhost:27017 -u adminuser -p password --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump
.\mongorestore --host localhost:27017 -u adminuser -p password --drop --archive="C:\backup\mongo-xxxx-xx-xx_xx-xx.dump"
Replica Set without authorization
- Linux
- Windows Server
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
.\mongorestore --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 --drop --archive="C:\backup\mongo-xxxx-xx-xx_xx-xx.dump"
Replica Set with authorization
The example uses a MongoDB user with the following credentials:
- Login — adminuser
- Password — password
- Linux
- Windows Server
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
.\mongorestore --host rs0/mongo.example.01:27017,mongo.example.02:27017,mongo.example.03:27017 -u adminuser -p password --drop --archive="C:\backup\mongo-xxxx-xx-xx_xx-xx.dump"
Separate restoration Passwork database
Overview
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
- Linux
- Windows Server
mongorestore --host localhost:27017 --nsInclude=pwbox.* --drop --archive=/backup/mongo-xxxx-xx-xx_xx:xx.dump
.\mongorestore --host localhost:27017 --nsInclude=pwbox.* --drop --archive="C:\backup\mongo-xxxx-xx-xx_xx-xx.dump"
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
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"
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
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.