Dump and restore data of a specific key in redis
Asked Answered
T

4

14

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?

Transfix answered 18/2, 2014 at 7:14 Comment(2)
It could be a bit off-topic since you asked for a backup and restore (and maybe save that backup file somewhere), but it could be a nice hint too: the redis MIGRATE command takes care of dumping-restoring among 2 machines, while it's a currently limited command. With redis < 3, the key will be deleted from source instance (this could be a minor problem, since you can easily backup your source DB before migrating and then using it again after), and I wasn't able to find some option for supporting authentication on destination DB.Demetrademetre
I would go with Sergio Tulentsev approach. Write your own code to dump and restore keys. Its going to be couple lines of code.Misprint
I
35

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
Inductee answered 3/5, 2014 at 1:38 Comment(2)
If someone is interested in a batch script to grab all keys and save them to files #!/bin/bash for each in $( redis-cli KEYS \apikey* ); do redis-cli --raw dump mykey | head -c-1 > dump/$each done these can be used to import afterwards with the above script from Brendan WoodRondo
This is a nice article with more details: medium.com/@elliotchance/…Almaraz
W
7

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
Wharf answered 23/6, 2015 at 19:23 Comment(2)
If anyone who use OSX get an error " head: ilegal byte count -- -1 ", " ghead -c-1 " will be work with install coreutils. (brew install coreutils)Walz
My requirement was to DUMP the key which was set using SET command & RESTORE to a new cluster & this worked perfectly.Hundred
A
2

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.

Avron answered 18/2, 2014 at 7:40 Comment(1)
I would go with this approach.Misprint
P
0

Another follow up on this post:

It is possible that there will be just a visible new line at the end of your file. So if you open it with a text editor of your choice, just remove the last new line.

That did the trick for me.

Users before suggested to remove the last 2 chars for example, which didn't work for me since the data was represented in a different way.

Parr answered 21/7, 2022 at 6:15 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Jeannettajeannette

© 2022 - 2024 — McMap. All rights reserved.