Script to restore all collections from mongodump
Feed this script the path to the dump directory created from mongodump to restore all collections. Handy to copy all mongo data to a new server.
#!/bin/bash DUMPPATH=$1 if [ $# -ne 1 ] then echo "$0 " exit 1 fi if [ ! -d $DUMPPATH ] then echo "$DUMPPATH is not a valid directory" exit 2 fi # Preflight confirm echo "************************************************************************************" echo echo "WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING" echo echo "This script will destroy ALL mongo data and refresh from the bson dump in $DUMPPATH" echo echo "Server: `hostname`" echo "************************************************************************************" echo read -p "Are you sure you want to continue? (y/n)" [ "$REPLY" == "y" ] || exit 0 # Iterate databases for DBNAME in `ls "$DUMPPATH"` do if [ -d "$DUMPPATH/$DBNAME" ]; then # Now iterate collections. We are only interested in .bson files echo "Restoring collections from $DBNAME db..." for FILENAME in `ls "$DUMPPATH/$DBNAME"` do EXT="${FILENAME##*.}" if [ $EXT == "bson" ]; then COLLECTION="${FILENAME%.*}" echo "Restoring ${COLLECTION}..." mongorestore --drop --db $DBNAME --collection $COLLECTION ${DUMPPATH}/${DBNAME}/${FILENAME} fi done fi done echo echo "Restore complete" exit