-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
executable file
·166 lines (131 loc) · 4.6 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"sort"
"os"
"io"
"strings"
"github.com/hashicorp/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 api.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, token string, outfile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
client, _ := api.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)
}
}
func backupAcls(ipaddress string, token string, outfile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
client, _ := api.NewClient(config)
acl := client.ACL()
tokens, _, err := acl.List(nil)
if err != nil {
panic(err)
}
// sort.Sort(ByCreateIndex(tokens))
outstring := ""
for _, element := range tokens {
// outstring += fmt.Sprintf("%s:%s:%s:%s\n", element.ID, element.Name, element.Type, element.Rules)
outstring += fmt.Sprintf("====\nID: %s\nName: %s\nType: %s\nRules:\n%s\n", element.ID, element.Name, element.Type, element.Rules)
}
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, token string, infile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
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, _ := api.NewClient(config)
kv := client.KV()
for _, element := range strings.Split(string(data), "\n") {
kvp := strings.Split(element, ":")
if len(kvp) > 1 {
p := &api.KVPair{Key: kvp[0], Value: []byte(kvp[1])}
_, err := kv.Put(p, nil)
if err != nil {
panic(err)
}
}
}
}
func main() {
usage := `Consul KV and ACL Backup with KV Restore tool.
Usage:
consul-backup [-i IP:PORT] [-t TOKEN] [--aclbackup] [--aclbackupfile ACLBACKUPFILE] [--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].
-t, --token=TOKEN An ACL Token with proper permissions in Consul [default: ].
-a, --aclbackup Backup ACLs, does nothing in restore mode. ACL restore not available at this time.
-b, --aclbackupfile=ACLBACKUPFILE ACL Backup Filename [default: acl.bkp].
-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["--token"].(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["--token"].(string), arguments["<filename>"].(string))
if arguments["--aclbackup"] == true {
fmt.Println("ACL Tokens will be backed up to file: ", arguments["--aclbackupfile"].(string))
backupAcls(arguments["--address"].(string), arguments["--token"].(string), arguments["--aclbackupfile"].(string))
}
}
}