Skip to content

Instantly share code, notes, and snippets.

@adfinlay
Last active March 23, 2017 15:46
Show Gist options
  • Save adfinlay/6ff48e0d50ce9b47852c711465aa1ff2 to your computer and use it in GitHub Desktop.
Save adfinlay/6ff48e0d50ce9b47852c711465aa1ff2 to your computer and use it in GitHub Desktop.
List non-expiring (ttl = -1) keys matchg pattern using "safe scan"
#!/bin/bash
if [ "$#" -lt 2 ]
then
echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)"
echo "Usage: $0 <host> [port] [database] [pattern] [count]"
exit 1
fi
host=${1:-}
port=${2:-6379}
database=${3:-0}
pattern=${4:-\*}
count=${5:-10}
cursor=-1
keys=""
touch keys.txt
> keys.txt
while [[ "$cursor" -ne 0 ]]; do
if [[ "$cursor" -eq -1 ]]
then
cursor=0
fi
reply=$(redis-cli -h "$host" -p "$port" -n "$database" SCAN "$cursor" MATCH "$pattern" COUNT "$count")
cursor=$(expr "$reply" : '\([0-9]*[0-9 ]\)')
keys=$(echo "$reply" | sed -e 's/^[0-9]\+//g')
echo $keys >> keys.txt
done
cat keys.txt | sed -e 's/ /\n/g' | grep -v '^$' > keys.txt
cat keys.txt | xargs -n 1 -L 1 redis-cli -h "$host" -p "$port" -n "$database" TTL > ttl.txt
paste -d " " keys.txt ttl.txt | grep .*-1$ | cut -d " " -f 1 > nonexpiring.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment