Redis Copy

1,417 views
Skip to first unread message

Kaan Çalışkan

unread,
May 8, 2012, 6:56:14 AM5/8/12
to redi...@googlegroups.com
Hi All,

I want to copy key value to another redis server via script or something like that. How can i do this? 

I tried to use redis-cli set command for remote host but my keys are dictionary pojo and i can't send them in that serialized format. I hope i can tell.

Best regards.

bugant

unread,
May 8, 2012, 7:04:56 AM5/8/12
to redi...@googlegroups.com
Hi,

On Tue, May 8, 2012 at 12:56 PM, Kaan Çalışkan <calis...@gmail.com> wrote:
> I want to copy key value to another redis server via script or something
> like that. How can i do this?

You can take use the SAVE (blocking) command (or its non-blocking
version BGSAVE) and then
taking the point in time snapshot created in the form of an RDB file.
Then, it will be possible to start another Redis server and make it
read that RDB file to load all your data set.

See the SAVE documentation for more details: http://redis.io/commands/save

ciao ciao,
matteo.

Kaan Çalışkan

unread,
May 8, 2012, 7:11:41 AM5/8/12
to redi...@googlegroups.com
I think it is not suitable for my system, because i just want to copy one key to another for every time. How can i copy directly key value pairs as is?

2012/5/8 bugant <bug...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To post to this group, send email to redi...@googlegroups.com.
To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.


Sripathi Krishnan

unread,
May 8, 2012, 7:17:26 AM5/8/12
to redi...@googlegroups.com
Look at the migrate command


--Sri

Kaan Çalışkan

unread,
May 8, 2012, 7:46:44 AM5/8/12
to redi...@googlegroups.com
I am using redis 2.4.10 version. This version of redis is not support migrate command. Maybe you can give an ezample to copy item via migrate command. 

Best regards.

2012/5/8 Sripathi Krishnan <sripathi...@gmail.com>

bugant

unread,
May 8, 2012, 7:59:06 AM5/8/12
to redi...@googlegroups.com
On Tue, May 8, 2012 at 1:11 PM, Kaan Çalışkan <calis...@gmail.com> wrote:
> I think it is not suitable for my system, because i just want to copy one
> key to another for every time. How can i copy directly key value pairs as
> is?

Sorry I misunderstood your question.
You can just code a simple script, say in Ruby:

require "redis"

server1 = Redis.new(server1_options)
server2 = Redis.new(server2_options)

foo = server1.hgetall("foo")
foo.each_pair {|k, v| server2.hset("foo", k, v)}

Hampus Wessman

unread,
May 8, 2012, 8:12:23 AM5/8/12
to redi...@googlegroups.com
For 2.6 you could use the DUMP and RESTORE commands.

An alternative way, that works for all verions, would be to simply write
a script that reads all the key's data from the first redis server and
issues commands to recreate it on the second redis server. You need to
do this differently for each data type, but it should be quite easy. You
can even use TYPE to find out the data type of the key, if you don't
know it.

As an example, lets say the key stores a list. Get the list's elements
on server 1 using LRANGE mylist 0 -1. Then run the following on server 2:

DEL mylist
RPUSH mylist "[element 1]"
RPUSH mylist "[element 2]"
...

Possibly wrap all that inside MULTI ... EXEC, if atomicity is important.
You can push several elements with each RPUSH too. Just keep in mind
that large commands will be fully buffered in memory and add latency for
other clients when executed (may not be a problem at all). Make sure to
use pipelining for the best performance.

Other types need to be handled differently, but you get the idea. I
think this is what I would have done.

Cheers,
Hampus

Josiah Carlson

unread,
May 8, 2012, 11:32:14 AM5/8/12
to redi...@googlegroups.com
For a recipe on copying data, I have used the following in the past:
https://gist.github.com/499552

Regards,
- Josiah

Salvatore Sanfilippo

unread,
May 8, 2012, 11:36:31 AM5/8/12
to redi...@googlegroups.com


On Tue, May 8, 2012 at 2:12 PM, Hampus Wessman <ham...@hampuswessman.se> wrote:

For 2.6 you could use the DUMP and RESTORE commands.

+1, this can be really really fast compared to any other method around.

Salvatore

--
Salvatore 'antirez' Sanfilippo
open source developer - VMware
http://invece.org

Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defence against complexity.
       — David Gelernter
Reply all
Reply to author
Forward
0 new messages