Created
December 7, 2016 18:06
-
-
Save malkitsingh/25e62244bc8648f37422b79717100e10 to your computer and use it in GitHub Desktop.
batch file to run mongodb daily tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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