-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (68 loc) · 1.54 KB
/
main.go
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
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"os"
redis "github.com/gomodule/redigo/redis"
)
func main() {
sourceURL := flag.String("source", "", "source redis connection URL")
destURL := flag.String("destination", "", "destination redis connection URL")
count := flag.Int("count", 100, "redis scan 'count'")
flag.Parse()
source, err := redis.DialURL(*sourceURL)
if err != nil {
panic("error connecting to source redis")
}
dest, err := redis.DialURL(*destURL)
if err != nil {
panic("error connecting to destination redis")
}
size, err := redis.Int(source.Do("DBSIZE"))
if err != nil {
panic(err.Error())
}
var cursor int
var progress int
var reply interface{}
for {
reply, err = source.Do("SCAN", cursor, "COUNT", *count)
if err != nil {
panic(err.Error())
}
for _, k := range reply.([]interface{})[1].([]interface{}) {
progress++
key, err := redis.String(k, nil)
if err != nil {
panic(err.Error())
}
val, err := source.Do("GET", key)
if err != nil {
panic(err.Error())
}
fmt.Printf("processing '%s', progress %d\n", key, progress)
_, err = dest.Do("SET", key, val)
if err != nil {
panic(err.Error())
}
}
cursor, err = redis.Int(reply.([]interface{})[0], nil)
if err != nil {
panic(err.Error())
}
fmt.Printf("cursor: %d\n", cursor)
if cursor == 0 {
break
}
}
destSize, err := redis.Int(dest.Do("DBSIZE"))
if err != nil {
panic(err.Error())
}
if destSize >= size {
fmt.Println("completed.")
os.Exit(0)
}
fmt.Println("something went wrong")
os.Exit(1)
}