summaryrefslogtreecommitdiff
path: root/utils/redis-copy.rb
blob: d892e377f38aacd5894229026e762236516ba9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# redis-copy.rb - Copyright (C) 2009-2010 Salvatore Sanfilippo
# BSD license, See the COPYING file for more information.
#
# Copy the whole dataset from one Redis instance to another one
#
# WARNING: currently hashes and sorted sets are not supported! This
#          program should be updated.

require 'rubygems'
require 'redis'
require 'digest/sha1'

def redisCopy(opts={})
    sha1=""
    src = Redis.new(:host => opts[:srchost], :port => opts[:srcport])
    dst = Redis.new(:host => opts[:dsthost], :port => opts[:dstport])
    puts "Loading key names..."
    keys = src.keys('*')
    puts "Copying #{keys.length} keys..."
    c = 0
    keys.each{|k|
        vtype = src.type?(k)
        ttl = src.ttl(k).to_i if vtype != "none"

        if vtype == "string"
            dst[k] = src[k]
        elsif vtype == "list"
            list = src.lrange(k,0,-1)
            if list.length == 0
                # Empty list special case
                dst.lpush(k,"")
                dst.lpop(k)
            else
                list.each{|ele|
                    dst.rpush(k,ele)
                }
            end
        elsif vtype == "set"
            set = src.smembers(k)
            if set.length == 0
                # Empty set special case
                dst.sadd(k,"")
                dst.srem(k,"")
            else
                set.each{|ele|
                    dst.sadd(k,ele)
                }
            end
        elsif vtype == "none"
            puts "WARNING: key '#{k}' was removed in the meanwhile."
        end

        # Handle keys with an expire time set
        if ttl != -1 and vtype != "none"
            dst.expire(k,ttl)
        end

        c = c+1
        if (c % 1000) == 0
            puts "#{c}/#{keys.length} completed"
        end
    }
    puts "DONE!"
end

if ARGV.length != 4
    puts "Usage: redis-copy.rb <srchost> <srcport> <dsthost> <dstport>"
    exit 1
end
puts "WARNING: it's up to you to FLUSHDB the destination host before to continue, press any key when ready."
STDIN.gets
srchost = ARGV[0]
srcport = ARGV[1]
dsthost = ARGV[2]
dstport = ARGV[3]
puts "Copying #{srchost}:#{srcport} into #{dsthost}:#{dstport}"
redisCopy(:srchost => srchost, :srcport => srcport.to_i,
          :dsthost => dsthost, :dstport => dstport.to_i)