Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying all keys in Redis database using MIGRATE

Tags:

redis

Is it possible to copy all keys from one Redis instance to another remote instance using MIGRATE? I've tried COPY, REPLACE and KEYS without any luck. Each time I get a NOKEY response. If I use any of the MIGRATE commands with a single key it works.

Examples:

MIGRATE my.redis 6379 "*" 0 5000 REPLACE // NOKEY
MIGRATE my.redis 6379 "*" 0 5000 COPY // NOKEY
MIGRATE my.redis 6379 "" 0 5000 KEYS * // NOKEY

MIGRATE my.redis 6379 "" 0 5000 KEYS test // OK
like image 343
anthonator Avatar asked May 11 '16 15:05

anthonator


2 Answers

This is an improvement on the answer provided by @ezain since I am unable to post comments. The command uses the correct redis syntax for processing batches of keys, but the arguments to xargs result in the command being called once for every key instead of just once with all the keys included (which means it'll take much more time to complete than is necessary). The following will be much faster in all cases:

redis-cli --raw KEYS '*' | xargs redis-cli MIGRATE my.redis 6379 "" 0 5000 KEYS

If the destination is password protected:

  • redis-cli --raw KEYS '*' | xargs redis-cli MIGRATE my.redis 6379 "" 0 5000 AUTH password-here KEYS
like image 146
lots0logs Avatar answered Oct 22 '22 18:10

lots0logs


try run in your shell

redis-cli keys '*' | xargs -I '{}' redis-cli migrate my.redis 6379 "" 0 5000 KEYS '{}'
like image 6
ezain Avatar answered Oct 22 '22 17:10

ezain