Consul KV数据修复

意外断电等情况下,raft.db文件会丢失数据,如果数据完整 但是格式异常,此时可以尝试手动修复(不保证一定可以恢复)

 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
package main

import (
	"io"
	"os"
	"regexp"
    "fmt"
)

func main() {
	file, err := os.Open("./raft.db")
	if err != nil {
		panic(err)
	}

	// Value后面有3个字符,所以需要...吃掉这三个字符
	// kv结束后会有?Op结尾
	compile, err := regexp.Compile(`(?s)Key.(.*?/.*?).LockIndex.*?Value...(.*?).Op`)

	all, err := io.ReadAll(file)
	submatch := compile.FindAllSubmatch(all, -1)
	var mp = make(map[string][]string)
	var newMap = make(map[string]string)

	for _, item := range submatch {
		mp[string(item[1])] = append(mp[string(item[1])], string(item[2]))
		newMap[string(item[1])] = string(item[2])
	}
	for s, v := range newMap {
		fmt.Println(s, v)
	}
}
updatedupdated2025-09-302025-09-30