scan 0 MATCH * COUNT 1000 // it gets all the keys
if return is "0" as first element then count is less than 1000
if more then it will return the pointer as first element and >scan pointer_val MATCH * COUNT 1000
to get the next set of keys it continues till the first value is "0".
I refined the bash solution a bit, so that the more efficient scan is used instead of keys, and printing out array and hash values is supported. My solution also prints out the key name.
redis_print.sh:
#!/bin/bash
# Default to '*' key pattern, meaning all redis keys in the namespace
REDIS_KEY_PATTERN="${REDIS_KEY_PATTERN:-*}"
for key in $(redis-cli --scan --pattern "$REDIS_KEY_PATTERN")
do
type=$(redis-cli type $key)
if [ $type = "list" ]
then
printf "$key => \n$(redis-cli lrange $key 0 -1 | sed 's/^/ /')\n"
elif [ $type = "hash" ]
then
printf "$key => \n$(redis-cli hgetall $key | sed 's/^/ /')\n"
else
printf "$key => $(redis-cli get $key)\n"
fi
done
Note: you can formulate a one-liner of this script by removing the first line of redis_print.sh and commanding: cat redis_print.sh | tr '\n' ';' | awk '$1=$1'
For listing out all keys & values you'll probably have to use bash or something similar, but MGET can help in listing all the values when you know which keys to look for beforehand.
Below is just a little variant of the script provided by @"Juuso Ohtonen".
I have add a password variable and counter so you can can check the progression of your backup. Also I replaced simple brackets [] by double brackets[[]] to prevent an error I had on macos.
1. Get the total number of keys
$ sudo redis-cli
INFO keyspace
AUTH yourpassword
INFO keyspace
2. Edit the script
#!/bin/bash
# Default to '*' key pattern, meaning all redis keys in the namespace
REDIS_KEY_PATTERN="${REDIS_KEY_PATTERN:-*}"
PASS="yourpassword"
i=1
for key in $(redis-cli -a "$PASS" --scan --pattern "$REDIS_KEY_PATTERN")
do
echo $i.
((i=i+1))
type=$(redis-cli -a "$PASS" type $key)
if [[ $type = "list" ]]
then
printf "$key => \n$(redis-cli -a "$PASS" lrange $key 0 -1 | sed 's/^/ /')\n"
elif [[ $type = "hash" ]]
then
printf "$key => \n$(redis-cli -a "$PASS" hgetall $key | sed 's/^/ /')\n"
else
printf "$key => $(redis-cli -a "$PASS" get $key)\n"
fi
echo
done