Skip to content

Instantly share code, notes, and snippets.

@mabn
Created March 15, 2015 15:50
Show Gist options
  • Save mabn/de4d9260ffc84efa8697 to your computer and use it in GitHub Desktop.
Save mabn/de4d9260ffc84efa8697 to your computer and use it in GitHub Desktop.
eraseredis.sh - small script to erase redis without making it unresponsive and causing failover
#!/bin/bash
CNT=10
SLEEP=10
function show_help {
echo "Usage: $0 -a auth_token -h host [-c count_per_iteration] [-s sleep_after_iteration]"
}
while getopts "h:c:s:a:?" opt; do
case "$opt" in
\?)
show_help
exit 0
;;
h) HOST=$OPTARG
;;
c) CNT=$OPTARG
;;
s) SLEEP=$OPTARG
;;
a) AUTH=$OPTARG
;;
esac
done
if [ "zzz$HOST" == "zzz" ] || [ "zzz$AUTH" == "zzz" ]
then
show_help
exit 1
fi
echo "Erasing host $HOST, $CNT keys per iteration with $SLEEP second pauses"
REDIS_CMD="redis-cli -h $HOST -a $AUTH"
remaining=`$REDIS_CMD DBSIZE`
#prev_remaining=$remaining
while [[ "$remaining" != "$prev_remaining" ]]; do
echo "keys remaining... $remaining"
$REDIS_CMD scan 0 count $CNT | tail -n +2 | xargs -n 100 -d "\n" $REDIS_CMD DEL > /dev/null
prev_remaining=$remaining
remaining=`$REDIS_CMD DBSIZE`
if [[ "$remaining" != "$prev_remaining" ]]; then echo "...";sleep $SLEEP; fi
done
echo "finished. keys remaining: $remaining"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment