]> git.saurik.com Git - redis.git/blame - utils/redis-copy.rb
Microsecond latency resolution in redis-benchmark
[redis.git] / utils / redis-copy.rb
CommitLineData
e1938730 1# redis-copy.rb - Copyright (C) 2009-2010 Salvatore Sanfilippo
8034b0f9 2# BSD license, See the COPYING file for more information.
3#
e1938730 4# Copy the whole dataset from one Redis instance to another one
8034b0f9 5#
e1938730 6# WARNING: currently hashes and sorted sets are not supported! This
7# program should be updated.
8034b0f9 8
9require 'rubygems'
10require 'redis'
11require 'digest/sha1'
12
13def redisCopy(opts={})
14 sha1=""
15 src = Redis.new(:host => opts[:srchost], :port => opts[:srcport])
16 dst = Redis.new(:host => opts[:dsthost], :port => opts[:dstport])
17 puts "Loading key names..."
18 keys = src.keys('*')
19 puts "Copying #{keys.length} keys..."
20 c = 0
21 keys.each{|k|
22 vtype = src.type?(k)
23 ttl = src.ttl(k).to_i if vtype != "none"
24
25 if vtype == "string"
26 dst[k] = src[k]
27 elsif vtype == "list"
28 list = src.lrange(k,0,-1)
29 if list.length == 0
30 # Empty list special case
31 dst.lpush(k,"")
32 dst.lpop(k)
33 else
34 list.each{|ele|
35 dst.rpush(k,ele)
36 }
37 end
38 elsif vtype == "set"
39 set = src.smembers(k)
40 if set.length == 0
41 # Empty set special case
42 dst.sadd(k,"")
43 dst.srem(k,"")
44 else
45 set.each{|ele|
46 dst.sadd(k,ele)
47 }
48 end
49 elsif vtype == "none"
50 puts "WARNING: key '#{k}' was removed in the meanwhile."
51 end
52
53 # Handle keys with an expire time set
54 if ttl != -1 and vtype != "none"
55 dst.expire(k,ttl)
56 end
57
58 c = c+1
59 if (c % 1000) == 0
60 puts "#{c}/#{keys.length} completed"
61 end
62 }
63 puts "DONE!"
64end
65
66if ARGV.length != 4
67 puts "Usage: redis-copy.rb <srchost> <srcport> <dsthost> <dstport>"
68 exit 1
69end
70puts "WARNING: it's up to you to FLUSHDB the destination host before to continue, press any key when ready."
71STDIN.gets
72srchost = ARGV[0]
73srcport = ARGV[1]
74dsthost = ARGV[2]
75dstport = ARGV[3]
76puts "Copying #{srchost}:#{srcport} into #{dsthost}:#{dstport}"
77redisCopy(:srchost => srchost, :srcport => srcport.to_i,
78 :dsthost => dsthost, :dstport => dstport.to_i)