-
-
Save NathanBaulch/a068f46ace464341b16630200869577b to your computer and use it in GitHub Desktop.
A bash script that scans Redis keys by pattern using SCAN
This file contains hidden or 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
#!/bin/bash | |
if [ "$#" -lt 1 ] | |
then | |
echo "Scan keys in Redis matching a pattern using SCAN (safe version of KEYS)" | |
echo "Usage: $0 <host> [port] [database] [pattern]" | |
exit 1 | |
fi | |
host=${1:-} | |
port=${2:-6379} | |
database=${3:-0} | |
pattern=${4:-\*} | |
cursor=-1 | |
while [[ "$cursor" -ne 0 ]]; do | |
if [[ "$cursor" -eq -1 ]] | |
then | |
cursor=0 | |
fi | |
reply=$(redis-cli -h "$host" -p "$port" -n "$database" --raw SCAN "$cursor" MATCH "$pattern" COUNT 1000) | |
cursor=${reply%%$'\n'*} | |
if [[ "$cursor" != "$reply" ]] | |
then | |
echo "${reply#*$'\n'}" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment