Last active
August 2, 2016 04:07
-
-
Save sambauers/8032884 to your computer and use it in GitHub Desktop.
Empties memcached completely, including flushed keys.
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
#!/usr/bin/env bash | |
# Edit these to your liking | |
IP="127.0.0.1"; | |
PORT="11211"; | |
# Warn the user | |
printf "This will flush all keys and delete them as well. This action can NOT be reversed.\\n"; | |
printf "Are you sure you want to do this?\\n"; | |
select answer in "Yes" "No" ; do | |
case $answer in | |
"Yes" ) | |
printf "Here we go then...\\n"; | |
break; | |
;; | |
"No" ) | |
printf "Stopped.\\n"; | |
exit 1; | |
;; | |
esac | |
done | |
# First flush all | |
printf "Attempting flush_all\\n"; | |
printf "======================\\n"; | |
printf "flush_all" | netcat $IP $PORT; | |
# Get the slabs | |
printf "Getting slabs and keys\\n"; | |
printf "======================\\n"; | |
# Save $IFS | |
ORIGINAL_IFS=$IFS; | |
IFS=$'\n'; | |
SLAB_LINES=$( printf "stats items" | netcat $IP $PORT ); | |
# Declare an associative array (may make this Bash 4 only - not sure) | |
declare -A SLAB_NUMBERS; | |
# Get all the slab numbers | |
for SLAB_LINE in $SLAB_LINES; | |
do | |
if [[ "${SLAB_LINE:0:3}" == "END" ]]; then continue; fi | |
SLAB_LINE=${SLAB_LINE##STAT items:}; | |
SLAB_LINE=${SLAB_LINE%%:*}; | |
SLAB_NUMBERS[$SLAB_LINE]=1; | |
done | |
# Reset $IFS | |
IFS=$ORIGINAL_IFS; | |
for SLAB_NUMBER in ${!SLAB_NUMBERS[@]}; | |
do | |
printf "Slab %s\\n" $SLAB_NUMBER; | |
printf -- "----------------------\\n"; | |
# Get all the keys in each slab | |
IFS=$'\n'; | |
KEY_LINES=$( printf "stats cachedump %s 0" $SLAB_NUMBER | netcat $IP $PORT ); | |
for KEY_LINE in $KEY_LINES; | |
do | |
if [[ "${KEY_LINE:0:3}" == "END" ]]; then continue; fi | |
# Delete the key | |
KEY_LINE=${KEY_LINE##ITEM }; | |
KEY_LINE=${KEY_LINE%% [*}; | |
printf "Deleting %s\\n" $KEY_LINE; | |
printf "delete %s" $KEY_LINE | netcat $IP $PORT; | |
done | |
IFS=$ORIGINAL_IFS; | |
done | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment