Skip to content

Instantly share code, notes, and snippets.

@toanz
Forked from systra/dbcleanup.sh
Created October 27, 2021 01:28
Show Gist options
  • Save toanz/626a8255214ec702c482a94b91b9ee92 to your computer and use it in GitHub Desktop.
Save toanz/626a8255214ec702c482a94b91b9ee92 to your computer and use it in GitHub Desktop.

Revisions

  1. @systra systra created this gist Dec 8, 2013.
    44 changes: 44 additions & 0 deletions dbcleanup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/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`