Created
April 28, 2020 16:08
-
-
Save ThabetAmer/0fe30bc996042339ed748635ded8a21d to your computer and use it in GitHub Desktop.
Mongo backup of whole database collections using mongoexport shell tool in json format
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
#!/bin/bash | |
# tested on Mongo 3.6 | |
DB='test' | |
USR='admin' | |
PSW='admin'' | |
BACKUP_FOLDER="$DB-backup" | |
COLLECTIONS=$(mongo localhost:27017/$DB -u $USR -p $PSW --authenticationDatabase admin --quiet --eval "db.getCollectionNames()" | tr -d '\[\]\"[:space:]' | tr ',' ' ') | |
[-d $BACKUP_FOLDER ] || mkdir -p $BACKUP_FOLDER | |
for COLLECTION in $COLLECTIONS | |
do | |
echo "Exporting $DB/$COLLECTION..." | |
mongoexport -u $USR -p $PSW --authenticationDatabase admin -d $DB -c $COLLECTION --out $BACKUP_FOLDER/$COLLECTION.json | |
done | |
echo "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment