Why does redis-cli DUMP (rdcli) return (nil) when the value passed is a loop control variable?

99 Views Asked by At

My goal is to loop over a list of keys and generate a DUMP for each key

I am using redis-cli installed via npm which is why I call it as rdcli

# For reading comprehension:
export RED="rdcli -h $redis_endpoint -p $redis_port -a $redis_pass"

# This returns a list of my redis keys
$RED KEYS "*"
# The returned lines are numbered so I strip them off like so
$RED KEYS "*" | cut -d' ' -f2-

# This generates a file of serialized data which I can later pass to my new redis instance
$RED DUMP $key >> redis.dmp

This works fine when $key is a single key. However, if I attempt to loop over the list of redis keys

for key in $($RED KEYS "*" | cut -d' ' -f2-); do 
    $RED DUMP $key; 
done

or

$RED KEYS "*" | cut -d' ' -f2- | xargs -I key $RED DUMP key

or

# runfile.file contains fully expanded, hardcoded commands
# Ex:
# rdcli -h xxx -p yyy -a zzz DUMP :1:aaa
# rdcli -h xxx -p yyy -a zzz DUMP :1:bbb
# rdcli -h xxx -p yyy -a zzz DUMP :1:ccc
chmod +x runfile.file
./runfile.file
# this may be a separate issue but the symptoms are the same

Output:

(nil)
(nil)
(nil)
(nil)
...

My only hint is that enabling debug mode with set -x reveals that

$RED DUMP $key
# where $key is set during runtime

is being evaluated as

rdcli -h xxx -p yyy -a zzz DUMP ':1:aaa'
# and subsequently spitting out (nil)

If I copy and paste rdcli -h xxx -p yyy -a zzz DUMP ':1:aaa' back into the shell it correctly returns serialized code but I am unable to do this programatically.

Additional info:

  • I have thousands of keys so I cannot do this by hand
  • I am pulling the keys from a heroku redis instance and have no way to download a backup or save
  • I am migrating to an Elasticache redis which prevents me from setting it up as a follower of the heroku instance as these commands are restricted
  • The two redis servers cannot communicate directly which is why I am using the DUMP and restore method
0

There are 0 best solutions below