MongoDB backup script

You can run this on a scheduled task and it will take a backup of all databases, zip it and copy it to a backup location, and automatically remove older backups after 7 days.

REM Create filename for output
set filename=mongodb-backup-%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%

REM Export the databases
@echo Dumping databases to "%filename%"
"c:\Program Files\MongoDB\Server\3.4\bin\mongodump.exe" --username <username> --password <password> --out %filename%

REM Create backup zip file
@echo Creating backup zip file from "%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

REM Move zip file to backup location
@echo Moving zip to backup location
move "%filename%.zip" E:\mongodb-backups

REM Delete files older than 7 days
@echo Deleting older backups
forfiles /P "E:\mongodb-backups" /S /M *.zip /D -7 /C "cmd /c del @PATH"

@echo BACKUP COMPLETE!

Something to add?

This site uses Akismet to reduce spam. Learn how your comment data is processed.