Skip to content

Instantly share code, notes, and snippets.

@malkitsingh
Created December 7, 2016 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save malkitsingh/25e62244bc8648f37422b79717100e10 to your computer and use it in GitHub Desktop.
Save malkitsingh/25e62244bc8648f37422b79717100e10 to your computer and use it in GitHub Desktop.
batch file to run mongodb daily tasks
@echo off
REM move into the backups directory
CD C:\backup
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:\Program Files\MongoDB\Server\3.2\bin\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
REM Delete files older than 7 days
forfiles -p "C:\backup" -s -m *. -d 0 -c "cmd /c del @path"
echo BACKUP COMPLETE
REM taken from http://stackoverflow.com/questions/19822855/mongodb-script-to-backup-replication-set-locally-to-a-windows-server and https://blog.rudolphk.co.za/mongodb-backup-script-for-windows-using-a-batch-file-4d0964e696ff#.uvxxn879c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment