Guest User

Untitled

a guest
Jan 10th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage() {
  4. echo "usage: ${0} database target_directory"
  5. exit 127
  6. }
  7.  
  8. database="${1}"
  9. if [ -z "${database}" ]; then
  10. usage
  11. fi
  12.  
  13. target_directory="${2}"
  14. if [ -z "${target_directory}" ] || [ ! -d "${target_directory}" ]; then
  15. echo "target directory does not exist."
  16. exit 1
  17. fi
  18.  
  19. jsfile="$( mktemp )"
  20.  
  21. cat << EOF > $jsfile
  22. use ${database};
  23. show collections;
  24.  
  25. EOF
  26.  
  27. collections=$( mongo --quiet "${database}" < $jsfile | tail -n+2 )
  28. if [ "$?" != "0" ]; then
  29. echo "failed to get collections: $!"
  30. exit 1
  31. fi
  32.  
  33. for collection in $collections; do
  34. mongoexport --db "${database}" --collection "${collection}" --out "${target_directory}/${collection}.json"
  35. done
Add Comment
Please, Sign In to add comment