Skip to content

Instantly share code, notes, and snippets.

@nurrony
Last active September 16, 2015 06:04
Show Gist options
  • Select an option

  • Save nurrony/cededadd12b8a0752a7e to your computer and use it in GitHub Desktop.

Select an option

Save nurrony/cededadd12b8a0752a7e to your computer and use it in GitHub Desktop.
Restore all MongoDB collections from directory contains BSON files
#!/usr/bin/env bash
#Author nmrony<[email protected]>
#you need to download it and give execution permission to run it
#handle errors
function die(){
echo $1;
exit 1;
}
function importDB() {
mongorestore -d $2 $1 2>/dev/null || die "Failed to open file";
}
read -p "Enter database Name: " dbname;
read -p "BSON directory path: " search_dir;
filelist=$(ls "$search_dir"/*.bson 2>/dev/null);
if [ -z "$filelist" ]; then
die "Directory is empty or there is no bson file";
else
importDB "$search_dir" "$dbname"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment