0

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
Simon
  • 855
  • 9
  • 24
  • Put the variable in quotes: `$RED DUMP "$key"` if it can contain spaces. – Barmar Aug 12 '23 at 05:11
  • For starters, `bash` has a `function` and `arrays` , so don't make it too [complex](https://mywiki.wooledge.org/BashFAQ/050) Also there is a reason why you [DRLWF](https://mywiki.wooledge.org/DontReadLinesWithFor) – Jetchisel Aug 12 '23 at 05:13
  • @Barmar That does not work. `"$key"` is still evaluated with single quotes and therefore still returns `(nil)` – Simon Aug 12 '23 at 21:55

0 Answers0