Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- DBADDR='localhost:5984'
- DEBUG=0
- DATABASE=""
- LOGFILE="/dev/null"
- REMREV=0
- FIXQUOTES=0
- usage () {
- echo ""
- echo "Download couchdb data from local database"
- echo ""
- echo "USAGE: `basename $0` [OPTIONS] <DB-NAME>"
- echo " "
- echo "Options:"
- echo " -h ...Show help"
- echo " -l <file> ...Specify a log file for curls output messages."
- echo " -q ...Clean-up newline and quoting issues, particularly for views."
- echo " -r ...Remove _rev entry from documents."
- echo " Needed if resulting data is going to populate a new database."
- echo " -s <url> ...Specifiy server IP/URL to download from. Default: localhost:5984"
- echo " -v <0-3> ...Increase print messages for more script info."
- echo " 0 - Print only standard optput"
- echo " 1 - Print full debug messages including everyting from 0"
- echo ""
- exit 1
- }
- while getopts hl:qrs:v: OPT; do
- case "$OPT" in
- h)
- usage ;;
- l)
- LOGFILE=$OPTARG ;;
- q)
- FIXQUOTES=1 ;;
- r)
- REMREV=1 ;;
- s)
- DBADDR=$OPTARG ;;
- v)
- DEBUG=$OPTARG ;;
- *)
- break
- ;;
- esac
- done
- # get rid of the just-finished flag arguments
- shift $(($OPTIND-1))
- # Finally put together our entire options string
- if [ "$DEBUG" -ge "1" ]; then set -o verbose; fi
- # If nothing is specified, then send us the help file
- if [[ $# < 1 ]]; then
- usage
- else
- DATABASE=$1
- fi
- # Grab our data and see if we get anything
- # dos2unit fixes newline issues
- # sed will remove the first line of the result (a container the wraps the json data)
- # head removes the last line of the result (end of container wrapping json)
- # sed & sed pull the actual document out from the array in the container result
- RESULT=$(curl --fail -vX GET "${DBADDR}/${DATABASE}/_all_docs?include_docs=true" 2>> "$LOGFILE" |\
- dos2unix |\
- sed -e "1d" |\
- head -n -1 |\
- sed 's/\},$//g'|\
- sed 's/^.*doc\"\://g')
- # If we are supposed to, clen it all up
- if [[ $FIXQUOTES -gt 0 ]]; then RESULT=$(echo "$RESULT" |sed "s/[\]\"/'/g" | sed "s/[\]n//g" |tr -s " "); fi
- # Finally, remove our _rev number if requested and print
- if [[ $REMREV -lt '1' ]]; then
- echo "$RESULT"
- else
- echo "$RESULT" | sed 's/\"\_rev\":\"[0-9a-z\-]*\",//g'
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement