forked from klangrud/consul-backup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·125 lines (98 loc) · 3.01 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
package main
import (
"fmt"
"sort"
"os"
"io"
"strings"
"github.com/armon/consul-api"
"github.com/docopt/docopt-go"
)
//type KVPair struct {
// Key string
// CreateIndex uint64
// ModifyIndex uint64
// LockIndex uint64
// Flags uint64
// Value []byte
// Session string
//}
type ByCreateIndex consulapi.KVPairs
func (a ByCreateIndex) Len() int { return len(a) }
func (a ByCreateIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
//Sort the KVs by createIndex
func (a ByCreateIndex) Less(i, j int) bool { return a[i].CreateIndex < a[j].CreateIndex }
func backup(ipaddress string, outfile string) {
config := consulapi.DefaultConfig()
config.Address = ipaddress
client, _ := consulapi.NewClient(config)
kv := client.KV()
pairs, _, err := kv.List("/", nil)
if err != nil {
panic(err)
}
sort.Sort(ByCreateIndex(pairs))
outstring := ""
for _, element := range pairs {
outstring += fmt.Sprintf("%s:%s\n", element.Key, element.Value)
}
file, err := os.Create(outfile)
if err != nil {
panic(err)
}
if _, err := file.Write([]byte(outstring)[:]); err != nil {
panic(err)
}
}
/* File needs to be in the following format:
KEY1:VALUE1
KEY2:VALUE2
*/
func restore(ipaddress string, infile string) {
config := consulapi.DefaultConfig()
config.Address = ipaddress
file, err := os.Open(infile)
if err != nil {
panic(err)
}
data := make([]byte, 100)
_, err = file.Read(data)
if err != nil && err != io.EOF { panic(err) }
client, _ := consulapi.NewClient(config)
kv := client.KV()
for _, element := range strings.Split(string(data), "\n") {
kvp := strings.Split(element, ":")
if len(kvp) > 1 {
p := &consulapi.KVPair{Key: kvp[0], Value: []byte(kvp[1])}
_, err := kv.Put(p, nil)
if err != nil {
panic(err)
}
}
}
}
func main() {
usage := `Consul Backup and Restore tool.
Usage:
consul-backup [-i IP:PORT] [--restore] <filename>
consul-backup -h | --help
consul-backup --version
Options:
-h --help Show this screen.
--version Show version.
-i, --address=IP:PORT The HTTP endpoint of Consul [default: 127.0.0.1:8500].
-r, --restore Activate restore mode`
arguments, _ := docopt.Parse(usage, nil, true, "consul-backup 1.0", false)
// fmt.Println(arguments)
if arguments["--restore"] == true {
fmt.Println("Restore mode:")
fmt.Printf("Warning! This will overwrite existing kv. Press [enter] to continue; CTL-C to exit")
fmt.Scanln()
fmt.Println("Restoring KV from file: ", arguments["<filename>"].(string))
restore(arguments["--address"].(string), arguments["<filename>"].(string))
} else {
fmt.Println("Backup mode:")
fmt.Println("KV store will be backed up to file: ", arguments["<filename>"].(string))
backup(arguments["--address"].(string), arguments["<filename>"].(string))
}
}