Dump and restore data of a specific key in redis

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?

Answers


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

Write a script that will DUMP needed keys, save the output to files and then later you can read those files and shove their content to RESTORE command.


Following up on this post:

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

If this doesn't work for you and you get an error like: head: illegal byte count -- -1

Then modify the dump command without the head command:

redis-cli --raw dump mykey > myfile

Now, open the dump file with sublime on the mac or textpad on the pc and remove the last two chars and save and then do the restore.

These were my two last lines:

 0561 7074 7572 6520 fa00 5be0 0526 015d
 7d06 00a7 afed c100 323d 400a 

I removed "0a" and saved and the restore worked, e.g.

0561 7074 7572 6520 fa00 5be0 0526 015d
7d06 00a7 afed c100 323d 40

Need Your Help

Is there java.util.concurrent equivalent for WeakHashMap?

java collections guava java.util.concurrent weakhashmap

Can the following piece of code be rewritten w/o using Collections.synchronizedMap() yet maintaining correctness at concurrency?

How do I use regular expressions in bash scripts?

regex bash conditional

I want to check if a variable has a valid year using a regular expression. Reading the bash manual I understand I could use the operator =~