Created
December 8, 2013 21:35
-
-
Save systra/7864133 to your computer and use it in GitHub Desktop.
CouchDB cleanup - remove old documents (shell method)
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 | |
# source: http://bravenewmethod.com/2012/05/31/couchdb-cleanup-script-for-purging-old-docs/ | |
# but modified to get rid of node.js dependency | |
# if database needs auth: | |
# DBHOST=user:pass@hostname | |
DBHOST=localhost | |
PORT=5984 | |
# Get key for entries that are over 1 month old. | |
# This assumes that created view can be queried using timestamps as keys. | |
if uname -a | grep -i darwin > /dev/null | |
then | |
TODAY=$(date '+%Y-%m-%d') | |
MONTHSAGO=$(date -v-1m '+%Y-%m-%d') | |
MONTHSAGO_E=$(date -v-1m '+%s') | |
else | |
TODAY=$(date '+%Y-%m-%d') | |
MONTHSAGO=$(date -d '1 month ago' '+%Y-%m-%d') | |
MONTHSAGO_E=$(date -d '1 month ago' '+%s') | |
fi | |
cleanup() { | |
DATABASE=$1 | |
DESIGN=$2 | |
VIEW=$3 | |
echo "Cleaning $DATABASE/$DESIGN" | |
curl --silent -S http://$DBHOST:$PORT/$DATABASE/_design/$DESIGN/_view/$VIEW?endkey=$MONTHSAGO_E | \ | |
awk 'BEGIN { FS=OFS=","; print "{\"docs\":["; } /"id"/ { gsub(/id/,"_id"); gsub(/value/,"_rev"); if(id) print id ", \"_deleted\":true, " rev ","; id = $1; rev = $3; } END { if (id) print id ", \"_deleted\":true, " rev; print "]}"; }' | \ | |
curl --silent -S -X POST -d @- -H "Content-Type:application/json" http://$DBHOST:$PORT/$DATABASE/_bulk_docs | \ | |
sed 's/\({[^}]*}\),/\1=/g' | \ | |
tr "=" "\n" | tr -d '[]' | \ | |
wc -l | awk '{ print $1 " docs deleted" }' | |
#grep -v "\"ok\":true" | |
} | |
echo "STATS CLEANUP <= $MONTHSAGO - Start" `date` | |
# Put databases and views here | |
cleanup mydb history outdated | |
echo "STATS CLEANUP - Done" `date` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment