Duplicate/Copy a redis database

Recently I needed to copy a Redis database. After a bit of confusion it’s actually quite easy:

redis-cli --raw KEYS '*' | xargs redis-cli MIGRATE 127.0.0.1 6379 "" 1 5000 KEYS

Explanation:

“Get * (all) KEYS and pipe them as argument for the redis MIGRATE command onto the server on localhost listening on port 6379 using “” (empty string) as ‘keys’ param into database 1 with a timeout of 5000 seconds.

The gist of it: Using “” as ‘key’ argument allows usage of the “KEYS” clause at the end of the command. The “KEYS” clause allows to list all keys that should be migrated, which happens via the piping of all keys in the first part of the command.

Some documentation on the MIGRATE command: Redis – Commands: MIGRATE

And the origin of this great command: Stackoverflow: Copying all keys in Redis database using MIGRATE

Leave a Reply