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 linux administration utilities are located in a directory that is added to the system path path, making them available for use from the cli command line typically, 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, 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 $utilitypath = get childitem path "c \\" filter "mongodump exe" recurse erroraction silentlycontinue | select object first 1; $utilitydirectory = $utilitypath directoryname; cd $utilitydirectory 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 dir standard passwork installation (docker, powershell) docker builds use the mongodb database creation and recovery scripts, which are located in the root directory of the passwork installation /\<passwork>/db backup sh /\<passwork>/db restore sh powershell module use the mongodb database creation and recovery functions, run in powershell as — "administrator" backup mongodb restore mongodb 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 mongodump host localhost 27017 archive=/backup/mongo $(date "+%y %m %d %h %m") dump windows server $timestamp = get date format "yyyy mm dd hh mm" \mongodump host localhost 27017 archive="c \backup\mongo $timestamp dump" single installation with authorisation for the example, a mongodb user with data is used login — adminuser password — password linux mongodump host localhost 27017 u adminuser p password archive=/backup/mongo $(date "+%y %m %d %h %m") dump windows server $timestamp = get date format "yyyy mm dd hh mm" \mongodump host localhost 27017 u adminuser p password archive="c \backup\mongo $timestamp dump" repica set (replica set) without authorisation linux 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 windows server $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" replication set (replica set) with authorisation for the example, a mongodb user with data is used login — adminuser password — password linux 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 windows server $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 backups in mongodb single installation without authorisation linux mongorestore host localhost 27017 drop archive=/backup/mongo xxxx xx xx xx\ xx dump windows server \mongorestore host localhost 27017 drop archive="c \backup\mongo xxxx xx xx xx xx dump" single installation with authorisation for the example, a mongodb user with data is used login — adminuser password — password linux mongorestore host localhost 27017 u adminuser p password drop archive=/backup/mongo xxxx xx xx xx\ xx dump windows server \mongorestore host localhost 27017 u adminuser p password drop archive="c \backup\mongo xxxx xx xx xx xx dump" repica set (replica set) without authorisation linux 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 windows server \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" replication set (replica set) with authorisation for the example, a mongodb user with data is used login — adminuser password — password linux 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 windows server \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 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 mongorestore host localhost 27017 nsinclude=pwbox drop archive=/backup/mongo xxxx xx xx xx\ xx dump windows server \mongorestore host localhost 27017 nsinclude=pwbox drop archive="c \backup\mongo xxxx xx xx xx xx dump" 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 docid\ ee0puoige2x2gyuzxbqml 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 \# getting the current time $timestamp = get date format "yyyy mm dd hh mm" \# creating a mongodb backup mongodump host localhost 27017 archive="c \backup\mongo $timestamp dump" 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 \# action to run the backup example ps1 script using powershell $action = new scheduledtaskaction execute "powershell exe" argument " file c \backup example ps1" \# to be done every day at 02 00 a m $trigger = new scheduledtasktrigger daily at "02 00am" \# parameters of the task to be created $settings = new scheduledtasksettingsset allowstartifonbatteries startwhenavailable hidden priority 5 \# creating a task register scheduledtask action $action taskname "mongodb example backups" settings $settings trigger $trigger runlevel highest user "username" password "password" force 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