Created
July 20, 2012 23:33
-
-
Save J-O-N/3153837 to your computer and use it in GitHub Desktop.
Tools to dump/restore a couch database - mysql style!
This file contains 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 | |
# An awesome tool to dump a couch database - mysqldump style! | |
# 2600hz - The Future of Cloud Telecom | |
COUCH_URL=$1 | |
DB=$2 | |
DOC_IDS=`curl -s -X "GET" "$COUCH_URL/$DB/_all_docs" | sed -n -e 's/^.*"id":"\([^"]\+\)".*$/\1/p'` | |
DOC_COUNT=`echo "$DOC_IDS" | wc -l` | |
DOC_NUMBER=0 | |
echo "{\"db\":\"$DB\",\"doc_count\":$DOC_COUNT}" | |
for DOC_ID in $DOC_IDS | |
do | |
let "DOC_NUMBER+=1" | |
# Redirect the output of the echo to STDERR so it's not captured by the redirect | |
echo "($DOC_NUMBER/$DOC_COUNT) Dumping document $DOC_ID..." 1>&2 | |
curl -s -X "GET" "$COUCH_URL/$DB/$DOC_ID?attachments=true" | |
done | |
echo "Done!" 1>&2 |
This file contains 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 | |
# An awesome tool for restoring couch dumps - myql style! | |
# 2600hz - The Future of Cloud Telecom | |
COUCH_URL=$1 | |
WITHREVS=0 | |
if [ "$2" == "revs" ]; then | |
WITHREVS=1 | |
fi | |
read DUMP_INFO | |
DB=`echo "$DUMP_INFO" | sed -n -e 's/^.*"db":"\([^"]\+\)".*$/\1/p'` | |
DOC_COUNT=`echo "$DUMP_INFO" | sed -n -e 's/^.*"doc_count":\([^,}]\+\).*$/\1/p'` | |
DOC_NUMBER=0 | |
while read DOC | |
do | |
let "DOC_NUMBER+=1" | |
DOC_ID=`echo "$DOC" | sed -n -e 's/^.*"_id":"\([^"]\+\)".*$/\1/p'` | |
if [ "$WITHREVS" == "0" ]; then | |
DOC=`echo "$DOC" | sed -e 's/"_rev":"[0-9a-zA-Z\-]*",//'` | |
fi | |
RESULT=`curl -s -X "PUT" -H "Content-Type: application/json" -d "$DOC" "$COUCH_URL/$DB/$DOC_ID" | sed -n -e 's/^.*"reason":"\([^"]\+\).*$/\1/p'` | |
if [ -z "$RESULT" ]; then | |
echo "($DOC_NUMBER/$DOC_COUNT) Restored document $DOC_ID." | |
else | |
echo "($DOC_NUMBER/$DOC_COUNT) Error uploading document $DOC_ID: $RESULT" | |
fi | |
done | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment