-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (99 loc) · 2.76 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
"gopkg.in/redis.v3"
)
var (
version string
redisDB int64
redisHost, redisPort, redisPassword string
documentation = `Redis Dumper
This script dumps all the entries from one Redis DB into a file in the redis protocol format.
See here (http://redis.io/topics/protocol) and here (http://redis.io/topics/mass-insert).
This allows use to pipe the resulting file directly into redis with pipe command like this
> cat redis_db_0_dump.rdb | redis-cli --pipe
This script is especially created to get contents from AWS Elasticache but works with all Redis instances
`
)
const restoreCommand = "*4\r\n$7\r\nRESTORE\r\n"
func init() {
flag.Int64Var(&redisDB, "n", 0, "Database number")
flag.StringVar(&redisHost, "h", "127.0.0.1", "Server hostname")
flag.StringVar(&redisPort, "p", "6379", "Server port")
flag.StringVar(&redisPassword, "a", "", "Password to use when connecting to the server")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, documentation)
fmt.Fprintf(os.Stderr, "Usage of Redis Dumper:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nCurrent Version: %s\n", version)
}
}
func main() {
flag.Parse()
log.Println("Start processing")
options := &redis.Options{
DB: redisDB,
Addr: fmt.Sprintf("%v:%v", redisHost, redisPort),
}
if len(redisPassword) != 0 {
options.Password = redisPassword
}
client := redis.NewClient(options)
file, writer := createFile()
defer file.Close()
var cursor int64
for {
var keys []string
var err error
cursor, keys, err = client.Scan(cursor, "", 1000).Result()
if err != nil {
log.Fatalf("Couldn't iterate through set: %v", err)
}
for _, key := range keys {
processKey(client, writer, key)
}
writer.Flush()
if cursor == 0 {
break
}
}
log.Println("End processing")
}
func processKey(client *redis.Client, writer *bufio.Writer, key string) {
dump, err := client.Dump(key).Result()
if err != nil {
log.Printf("ERROR: couldn't dump key %s: %v", key, err)
return
}
ttl, err := client.TTL(key).Result()
if err != nil {
log.Printf("ERROR: couldn't dump key %s: %v", key, err)
return
}
writer.WriteString(createRestoreCommand(key, dump, &ttl))
}
func createRestoreCommand(key, dump string, ttl *time.Duration) string {
seconds := int(ttl.Seconds() * 1000)
if seconds < 0 {
seconds = 0
}
ttlString := strconv.Itoa(seconds)
result := restoreCommand
for _, val := range [3]string{key, ttlString, dump} {
result += "$" + strconv.Itoa(len(val)) + "\r\n" + val + "\r\n"
}
return result
}
func createFile() (*os.File, *bufio.Writer) {
file, err := os.Create(fmt.Sprintf("redis_db_%d_dump.rdb", redisDB))
if err != nil {
log.Fatalf("Couldn't create file: %v", err)
}
return file, bufio.NewWriter(file)
}