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?

reallynice
  • 1,289
  • 2
  • 21
  • 41
Joy
  • 4,197
  • 14
  • 61
  • 131
  • 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](http://redis.io/commands/migrate) 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. – reallynice Nov 28 '14 at 08:06
  • I would go with Sergio Tulentsev approach. Write your own code to dump and restore keys. Its going to be couple lines of code. – Sahib Khan Jul 28 '22 at 14:46

4 Answers4

33

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
Community
  • 1
  • 1
Brendan Wood
  • 6,220
  • 3
  • 30
  • 28
  • 1
    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 Wood – Timothy Dalton Jul 01 '18 at 09:02
  • This is a nice article with more details: https://medium.com/@elliotchance/migrating-data-between-redis-servers-without-downtime-429e4c8048e6 – J. Allen May 08 '20 at 17:38
5

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
Community
  • 1
  • 1
user1178516
  • 651
  • 1
  • 6
  • 9
  • 4
    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) – alexmoon Jan 20 '22 at 07:34
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.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
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.

  • 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](/help/how-to-answer). – Community Jul 29 '22 at 11:52