Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created September 25, 2017 17:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucaswerkmeister/3e2f0bd56cc45a334de01e18103a2dfe to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/3e2f0bd56cc45a334de01e18103a2dfe to your computer and use it in GitHub Desktop.
pure Bash script to dump all keys in a memcached server
#!/bin/bash
# connect to memcached
exec 3<>/dev/tcp/"${1:-localhost}"/"${2:-11211}" || exit $?
# get slab numbers
printf 'stats slabs\r\n' >&3
slabnums=()
while IFS=' :' read -r stat slabnum _; do
if [[ $stat == $'END\r' ]]; then
break
fi
if ! [[ $slabnum =~ ^[0-9]*$ ]]; then
continue
fi
for index in "${!slabnums[@]}"; do
if [[ ${slabnums[index]} == "$slabnum" ]]; then
continue 2
fi
done
slabnums+=($slabnum)
done <&3
# dump each slab
for ((index=0;index<${#slabnums[@]};index++)); do
slabnum=${slabnums[index]}
printf 'stats cachedump %d 0\r\n' "$slabnum" >&3
while read -r line; do
if [[ $line == $'END\r' ]]; then
break
fi
printf '%s\n' "${line%$'\r'}"
done <&3
done
# close connection
printf 'quit\r\n' >&3
exec 3<&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment