Created
May 4, 2018 12:34
-
-
Save akkonrad/75c3d4e06c4207ae3a1cc47c6c80ff62 to your computer and use it in GitHub Desktop.
mongo database export
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
#!/bin/bash | |
# define collections that you want to export. if not defined, nothing will be exported | |
COLLECTIONS=(collection1 collection2 collection3) | |
# database access | |
DB_HOST=${1-'127.0.0.1'} | |
DB_NAME=${2-'main'} | |
DB_PORT=${3-'27017'} | |
DB_USER=${4-'root'} | |
DB_PASS=${5-''} | |
echo "DB_HOST: $DB_HOST" | |
echo "DB_NAME: $DB_NAME" | |
echo "DB_PORT: $DB_PORT" | |
echo "DB_USER: $DB_USER" | |
echo "DB_PASS: $DB_PASS" | |
# create directories where dump will be stored | |
mkdir -p db | |
mkdir -p db/$DB_NAME | |
for COLLECTION in "${COLLECTIONS[@]}" | |
do : | |
echo | |
echo "EXPORTING COLLECTION: $COLLECTION" | |
if [ -z "$DB_PASS" ] | |
then | |
# in case when no password is set we won't pass any authorization data assuming it's not required | |
mongoexport --host $DB_HOST --port $DB_PORT --db $DB_NAME --collection $COLLECTION --out db/$DB_NAME/$COLLECTION.json | |
else | |
mongoexport --host $DB_HOST --port $DB_PORT --db $DB_NAME --username $DB_USER --password $DB_PASS --collection $COLLECTION --out db/$DB_NAME/$COLLECTION.json | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment