inchirags@gmail.com Chirag's MongoDB DBA Tutorial https://www.chirags.in
*****************************************************************************************
* MongoDB Schedule Backups on a Windows Server *
*****************************************************************************************
YouTube Video:
MongoDB Database server:
Server IP: 192.168.224.133
To schedule MongoDB backups on a Windows Server, you can use the Task Scheduler and a backup script. Here's a step-by-step guide:
Step 1: Create a Backup Script
Open a text editor like Notepad.
Write a backup script using the mongodump command. Below is an example script:
@echo off
:: Set the current date in YYYY-MM-DD format
for /f "tokens=2 delims==" %%i in ('wmic os get localdatetime /value') do set datetime=%%i
set datestamp=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%
:: Set backup directory
set backupDir=C:\MongoDB_Backups\%datestamp%
:: Create backup directory
if not exist "%backupDir%" mkdir "%backupDir%"
:: Perform MongoDB dump
mongodump --host localhost --port 27017 --out "%backupDir%"
:: Optional: Remove old backups older than 7 days
forfiles /p "C:\MongoDB_Backups" /m * /d -7 /c "cmd /c if @isdir==TRUE rmdir /s /q @path"
echo Backup completed successfully!
Save the script as mongo_backup.bat in a directory, e.g., C:\Scripts.
Step 2: Test the Script
Open Command Prompt.
Navigate to the script's location:
cd C:\Scripts
Run the script:
mongo_backup.bat
Check the output in C:\MongoDB_Backups. Ensure a folder with the current date is created and contains your MongoDB backup files.
Step 3: Schedule the Backup Using Task Scheduler
Open Task Scheduler on your Windows Server.
In the Actions panel, click Create Task.
Fill in the details:
Name: Enter a name like MongoDB Daily Backup.
Description: Describe the task, e.g., "Daily backup of MongoDB databases."
Set the Trigger:
Go to the Triggers tab and click New.
Choose a schedule (e.g., Daily, Weekly, or Monthly).
Set the time and recurrence pattern. Click OK.
Set the Action:
Go to the Actions tab and click New.
Action: Select Start a program.
Program/script: Browse to mongo_backup.bat.
Click OK.
Set Conditions (Optional):
Go to the Conditions tab to configure any specific conditions, such as only running when the server is idle.
Set Settings:
Go to the Settings tab.
Check "Allow task to be run on demand".
Optionally, enable "Stop the task if it runs longer than" to prevent long-running tasks.
Click OK to save the task.
Step 4: Test the Scheduled Task
In Task Scheduler, right-click the task you created and select Run.
Verify that the task runs successfully and creates a backup in the specified directory.
Step 5: Monitor Backups
Periodically check the C:\MongoDB_Backups directory to ensure backups are being created as scheduled.
Review Task Scheduler logs if backups fail.
Let me know if you need help configuring this!
For any doubts and query, please write on YouTube video comments section.
Note : Flow the Process shown in video.
Please, Subscribe and like for more videos:
https://youtube.com/@chiragstutorial
Don't forget to, Follow, Like, Share &, Comment
Thanks & Regards,
Chitt Ranjan Mahto "Chirag"
_________________________________________________________________________________________
Note: All scripts used in this demo will be available in our website.
Link will be available in description.
#mongodb
#mongodbtutorial
#mongo
#mongosh
#mongodbtutorialforbeginners
#chiragstutorial
#chiragsdatabasetutorial
#chirags
#chiragsmahto
#mongodbautobackup
Install MongoDB 8,Authentication,MongoDB Tutorial,MongoDB Chirags Tutorial,MongoDB DBA Tutorial,Chirags Tutorial,Chirags DBA Tutorial,Chirag Mahto,Chitt Ranjan Mahto,Chirag DBA Tutorial,Database Tutorial,NoSQL Database Tutorial,MongoDB NoSQL Database,MongoDB Install,MongoDB Configuration,MongoDB Access,MongoDB Compass,Mongosh,Rockmongo,Mongo,MongoDB Authorization,MongoDB Authentication,MongoDB Authorisation,Mongodb Auto Backup in Windows, MongoDB Schedule Backup in Windows