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