Skip to content

Instantly share code, notes, and snippets.

@wiratama
Forked from malkitsingh/backup.bat
Created October 31, 2018 15:43
Show Gist options
  • Save wiratama/67bbd0bbbe1ee2501e4454ed6814ea8d to your computer and use it in GitHub Desktop.
Save wiratama/67bbd0bbbe1ee2501e4454ed6814ea8d 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