Dump and restore data of a specific key in redis

Joy picture Joy · Feb 18, 2014 · Viewed 18.4k times · Source

I want to take backup of a particular key in my redis which have multiple keys. My redis has many keys and I don't want to take full backup of my redis data. I have been going through http://redis.io/commands. There I found that there is a command dump by which I can take the dump of a specific key as follows:

 redis> dump "myKey"

But is giving output in hexadecimal format in redis console only. Is it possible to store the data for a specific key in a file and later import it to that key?

Answer

Brendan Wood picture Brendan Wood · May 3, 2014

In case you are trying to dump/restore a key from the command line (which is what I needed to do when I found this question), Redis has some non-obvious quirks. Please see this answer for a more detailed explanation.

The short answer is to dump/restore as follows:

bwood@mybox:~$ redis-cli --raw dump mykey | head -c-1 > myfile
bwood@mybox:~$ cat myfile | redis-cli -x restore mynewkey 0