Examples of creating and restoring MongoDB backups
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
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
Before executing the commands, you need to create a directory where MongoDB backups will be located:
- Linux — mkdir /backup/
- Windows Server — mkdir C:\backup\
Linux
Windows Server
For the example, a MongoDB user with data is used:
- Login — adminuser
- Password — password
Linux
Windows Server
Linux
Windows Server
For the example, a MongoDB user with data is used:
- Login — adminuser
- Password — password
Linux
Windows Server
Linux
Windows Server
For the example, a MongoDB user with data is used:
- Login — adminuser
- Password — password
Linux
Windows Server
Linux
Windows Server
For the example, a MongoDB user with data is used:
- Login — adminuser
- Password — password
Linux
Windows Server
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.
Linux
Windows Server
Once executed, only the Passwork database will be restored from the specified MongoDB backup file.
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.
Create an automation file with the extension .ps1 and specify the following:
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:
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.