Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. DBADDR='localhost:5984'
  4. DEBUG=0
  5. DATABASE=""
  6. LOGFILE="/dev/null"
  7. REMREV=0
  8. FIXQUOTES=0
  9.  
  10. usage () {
  11. echo ""
  12. echo "Download couchdb data from local database"
  13. echo ""
  14. echo "USAGE: `basename $0` [OPTIONS] <DB-NAME>"
  15. echo " "
  16. echo "Options:"
  17. echo " -h ...Show help"
  18. echo " -l <file> ...Specify a log file for curls output messages."
  19. echo " -q ...Clean-up newline and quoting issues, particularly for views."
  20. echo " -r ...Remove _rev entry from documents."
  21. echo " Needed if resulting data is going to populate a new database."
  22. echo " -s <url> ...Specifiy server IP/URL to download from. Default: localhost:5984"
  23. echo " -v <0-3> ...Increase print messages for more script info."
  24. echo " 0 - Print only standard optput"
  25. echo " 1 - Print full debug messages including everyting from 0"
  26. echo ""
  27. exit 1
  28. }
  29.  
  30. while getopts hl:qrs:v: OPT; do
  31. case "$OPT" in
  32. h)
  33. usage ;;
  34. l)
  35. LOGFILE=$OPTARG ;;
  36. q)
  37. FIXQUOTES=1 ;;
  38. r)
  39. REMREV=1 ;;
  40. s)
  41. DBADDR=$OPTARG ;;
  42. v)
  43. DEBUG=$OPTARG ;;
  44. *)
  45. break
  46. ;;
  47. esac
  48. done
  49.  
  50. # get rid of the just-finished flag arguments
  51. shift $(($OPTIND-1))
  52.  
  53. # Finally put together our entire options string
  54. if [ "$DEBUG" -ge "1" ]; then set -o verbose; fi
  55.  
  56. # If nothing is specified, then send us the help file
  57. if [[ $# < 1 ]]; then
  58. usage
  59. else
  60. DATABASE=$1
  61. fi
  62.  
  63. # Grab our data and see if we get anything
  64. # dos2unit fixes newline issues
  65. # sed will remove the first line of the result (a container the wraps the json data)
  66. # head removes the last line of the result (end of container wrapping json)
  67. # sed & sed pull the actual document out from the array in the container result
  68. RESULT=$(curl --fail -vX GET "${DBADDR}/${DATABASE}/_all_docs?include_docs=true" 2>> "$LOGFILE" |\
  69. dos2unix |\
  70. sed -e "1d" |\
  71. head -n -1 |\
  72. sed 's/\},$//g'|\
  73. sed 's/^.*doc\"\://g')
  74.  
  75. # If we are supposed to, clen it all up
  76. if [[ $FIXQUOTES -gt 0 ]]; then RESULT=$(echo "$RESULT" |sed "s/[\]\"/'/g" | sed "s/[\]n//g" |tr -s " "); fi
  77.  
  78. # Finally, remove our _rev number if requested and print
  79. if [[ $REMREV -lt '1' ]]; then
  80. echo "$RESULT"
  81. else
  82. echo "$RESULT" | sed 's/\"\_rev\":\"[0-9a-z\-]*\",//g'
  83. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement