Last active
February 5, 2016 19:20
-
-
Save brockers/47d0bc136cb8d3f13404 to your computer and use it in GitHub Desktop.
Download Couchdb Data from into json formatted file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| DBADDR='localhost:5984' | |
| DEBUG=0 | |
| DATABASE="" | |
| LOGFILE="/dev/null" | |
| REMREV=0 | |
| FIXQUOTES=0 | |
| REMIMG=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 " -i ...Remove image entries from documents" | |
| echo " File attachements do not get downloaded anyway." | |
| 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 hil:qrs:v: OPT; do | |
| case "$OPT" in | |
| h) | |
| usage ;; | |
| i) | |
| REMIMG=1 ;; | |
| 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 -gt '0' ]]; then | |
| REMRESULT=$( echo "$RESULT" | sed 's/\"\_rev\":\"[0-9a-z\-]*\",//g' ) | |
| RESULT=$REMRESULT | |
| fi | |
| if [[ $REMIMG -gt '0' ]]; then | |
| IMGRESULT=$( echo "$RESULT" | perl -pe 's|,\"images\":\[.*\]||' ) | |
| RESULT=$IMGRESULT | |
| fi | |
| echo "$RESULT" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The download portion of https://gist.github.com/470c039a5af4fea52aaa.git