Last active
October 8, 2020 20:39
-
-
Save andretapxure/1df64c05ff31a6912c297a59ab55784b to your computer and use it in GitHub Desktop.
This snippet is a windows and linux version for MongoDB Backup. Just need to replace the MongoDB Binary for it to work. I had some dificulty doing "if (condition) && (condition) so I nested it.
This file contains hidden or 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 | |
| echo ### Cleaning up | |
| rmdir /s /q e:\mongotemp | |
| mkdir e:\mongotemp | |
| echo ### Listing and backing up each database | |
| rem the following line must be uncommented in case you want to backup ALL databases. if not, create a input.txt file as follows comment the line that lists the databases | |
| rem [ "admin", "config", "local", "database1", "database2" ] | |
| rem "C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe" --quiet --eval "printjson(db.getMongo().getDBNames())" > input.txt | |
| set /p mongodbs=<input.txt | |
| FOR %%G IN (%mongodbs%) DO ( | |
| if not %%G==[ ( | |
| if not %%G==] ( | |
| echo Backing Up %%G | |
| "C:\Program Files\MongoDB\Server\4.0\bin\mongodump.exe" --out e:\mongotemp --db=%%G | |
| ) | |
| ) | |
| ) | |
| echo ### Compactando | |
| "c:\Program Files\7-Zip\7z.exe" a -tzip e:\mongobackup\mongo_%date:~-4,4%%date:~-7,2%%date:~-10,2%.7zip e:\mongotemp | |
| rem [ "admin", "config", "local", "database1", "database2" ] | |
| echo ### Syncing with AWS S3 | |
| "c:\Program Files\Amazon\AWSCLIV2\aws.exe" configure set AWS_ACCESS_KEY_ID AKIAIOSFODNN7EXAMPLE | |
| "c:\Program Files\Amazon\AWSCLIV2\aws.exe" configure set AWS_SECRET_ACCESS_KEY wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY | |
| "c:\Program Files\Amazon\AWSCLIV2\aws.exe" configure set default.region us-east-1 | |
| "c:\Program Files\Amazon\AWSCLIV2\aws.exe" configure set default.s3.max_concurrent_requests 50 | |
| "c:\Program Files\Amazon\AWSCLIV2\aws.exe" s3 sync e:\mongobackup s3://mybucket |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet is a windows and linux version for MongoDB Backup. Just need to replace the MongoDB Binary for it to work. I had some dificulty doing "if (condition) && (condition) so I nested it.