Skip to content

🗳️ A simple script to make a backup of a MongoDB database and create a scheduled task for it on Windows

License

Notifications You must be signed in to change notification settings

rilopez/powerShell-backupMongoDB

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🗳️ Dump MongoDB Database

It's a simple script made with PowerShell to create a backup of a MongoDB database and save all collections in a .zip folder.

🔃 Creating a simple backup flow

1. Verify the version of MongoDB, running:

mongod --version

2. If the version of your MongoDB is 4.4 or above, you will need install de mongodump, mongorestore and other tools separately. For this, follow this guide: Installing the Database Tools on Windows

"Starting with MongoDB 4.4, the MongoDB Database Tools are now released separately from the MongoDB Server..."

3. After the installation or if your version already has the database tools (like in the version 4.2), run the following commands to guarantee that the tools are accessable by the command line:

mongodump --help
mongorestore --help

If they are not accessable, follow this guide: Make the DB Tools available in your PATH

4. After all the process of installation/verification we will create the flow properly

4.1 Edit the backupMongoDatabase.ps1 script, modifing the variables:

$databaseName = "databaseName"
$mongoDbHost = "localhost:27017"
$backupPath = "C:\Path\To\Back\Folder"

OBS: If the database requires authorization remember to use the command with -u and -p flags

4.2. Edit the createSystemScheduledTask.ps1 script, modifing the variable with the path of the dumpMongoDatabase.ps1 script and the time that the task will run

$scriptToExecutePath = "C:\Path\To\dumpMongoDatabase.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 1am

OBS: Change the 'databaseName' in the name and description too, for more control of which database that task will backup

4.3. Finally, open the PowerShell as admin (for precaution) and enter in the folder that the createSystemScheduledTask.ps1 is

cd "C:\Path\To\WhereScriptIs"

4.4. And execute it

./createSystemScheduledTask.ps1

4.5. The following output will be showed

5. ✅ Success! We created a basic backup task. You can check the task in Task Scheduler too:

The task:

6. Test if the task is running correctly executing it manually:

Start-ScheduledTask -TaskName 'MongoDB Backup - <databaseName>'

♻️ Need restore a database using the backup?

Take a look at this discussion at DBA Stack Exchange: How restore a specific database from backup using mongorestore command

📃 References

About

🗳️ A simple script to make a backup of a MongoDB database and create a scheduled task for it on Windows

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 100.0%