Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB script to backup replication set locally to a Windows Server

Tags:

mongodb

I would like to make a Daily Backup of my MongoDB from a replication set running from Windows 2012 servers.

End goal would be to get a daily backup and write the backup to a remote or local share - Windows.

Can I batch the mongodump command?

Any help would be greatly appreciated!!

like image 662
user2860244 Avatar asked Nov 06 '13 21:11

user2860244


1 Answers

Sorry, it's a bit late but the following seems to work OK for me. The script dumps the database and compresses the output using 7-Zip.

1) Create backup script (backup.bat)

@echo off

REM move into the backups directory
CD C:\database_backups


REM Create a file name for the database output which contains the date and time. Replace any characters which might cause an issue.
set filename=database %date% %time%
set filename=%filename:/=-%
set filename=%filename: =__%
set filename=%filename:.=_%
set filename=%filename::=-%

REM Export the database
echo Running backup "%filename%"
C:\mongodb\mongodump --out %filename%


REM ZIP the backup directory
echo Running backup "%filename%"
"c:\Program Files\7-Zip\7z.exe" a -tzip "%filename%.zip" "%filename%"


REM Delete the backup directory (leave the ZIP file). The /q tag makes sure we don't get prompted for questions 
echo Deleting original backup directory "%filename%"
rmdir "%filename%" /s /q

echo BACKUP COMPLETE

2) Schedule the backup

  1. Open Computer Management
  2. Go to Task Scheduler and select Create Task.
  3. On the General tab, enter a description and select Run whether user is logged on or not if you want the task to run at night.
  4. On the Triggers tab, select when you would like the task to run.
  5. On the Actions tab, create a new action which points at your batch script.
like image 166
Ian Avatar answered Sep 20 '22 18:09

Ian