Database
MongoDB

Examples of creating and restoring MongoDB backups

27min

Description

Practical examples of creating backups in MongoDB. Backups are necessary to ensure data security and recovery in case of system failure or data loss. It is recommended to perform daily backups of MongoDB databases and keep copies for the last week. This will allow you to restore data from Passwork if necessary. The administration utilities that are installed with MongoDB are used to create and restore:

  • Create backups — mongodump
  • Restore backups — mongorestore

Location of the administration utilities

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

Open PowerShell as — "Administrator":

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

Perform a search for administration utilities:

PowerShell


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

Once executed, the current working directory will be changed to the one in which the utilities are located, to test execute:

PowerShell


Docker builds use MongoDB backup and restore scripts, which are located in the root directory of the Passwork installation:

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

Examples of creating backups in MongoDB

Before executing the commands, you need to create a directory where MongoDB backups will be located:

  • Linux — mkdir /backup/
  • Windows Server — mkdir C:\backup\

Single installation without authorisation

Linux

Shell


Windows Server

PowerShell


Single installation with authorisation

For the example, a MongoDB user with data is used:

  • Login — adminuser
  • Password — password

Linux

Shell


Windows Server

PowerShell


Repica set (replica set) without authorisation

Linux

Shell


Windows Server

PowerShell


Replication set (replica set) with authorisation

For the example, a MongoDB user with data is used:

  • Login — adminuser
  • Password — password

Linux

Shell


Windows Server

PowerShell


Examples of restoring backups in MongoDB

Single installation without authorisation

Linux

Shell


Windows Server

PowerShell


Single installation with authorisation

For the example, a MongoDB user with data is used:

  • Login — adminuser
  • Password — password

Linux

Shell


Windows Server

PowerShell


Repica set (replica set) without authorisation

Linux

Shell


Windows Server

PowerShell


Replication set (replica set) with authorisation

For the example, a MongoDB user with data is used:

  • Login — adminuser
  • Password — password

Linux

Shell


Windows Server

PowerShell


Separate recovery of the Passwork database

Description

The option is described on the example of basic installation of MongoDB components. It is possible to use any of the previously described options by specifying an attribute to the restore command.

Example of recovery

Linux

Shell


Windows Server

PowerShell


Once executed, only the Passwork database will be restored from the specified MongoDB backup file.

Configuring scheduled backups

Linux

To configure the frequency of database backups, it is recommended to use the built-in event scheduler — crontab.

More details about crontab and examples of its use — Crontab basics

Configuring crontab for backups should be done on the server where the administration utility, mongodump, is available.

Windows Server

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

PowerShell


Note to self:

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

Open PowerShell as — "Administrator":

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

Create a task in the Event Scheduler to create MongoDB backups:

PowerShell


Note to self:

  • You need to change the names and location of the automated file you run — .ps1;
  • It is recommended to use the "Administrator" account to perform the background task. If another user is used, make sure that the user has sufficient rights to perform all actions.