Skip to content

Instantly share code, notes, and snippets.

@ThabetAmer
Created April 28, 2020 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThabetAmer/0fe30bc996042339ed748635ded8a21d to your computer and use it in GitHub Desktop.
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
#!/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