-
Notifications
You must be signed in to change notification settings - Fork 3
/
ldb-repair.go
46 lines (41 loc) · 1.14 KB
/
ldb-repair.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
package main
import (
"fmt"
"os"
flags "github.com/jessevdk/go-flags"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
ldbopt "github.com/syndtr/goleveldb/leveldb/opt"
)
type options struct {
LevelDBPath string `short:"d" long:"datastore-path" description:"full or relative path to the root of the levelDB datastore"`
}
func main() {
var (
opts = &options{}
argParser = flags.NewParser(opts, flags.Default)
)
if _, err := argParser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Printf("parsing arguments: %s\n", err)
os.Exit(1)
}
}
fmt.Printf("opening %s...", opts.LevelDBPath)
db, err := leveldb.OpenFile(opts.LevelDBPath, &ldbopt.Options{})
if err != nil && errors.IsCorrupted(err) {
db, recoverErr := leveldb.RecoverFile(opts.LevelDBPath, &ldbopt.Options{})
if recoverErr != nil {
fmt.Printf("recovering: %s\n", recoverErr)
os.Exit(2)
}
defer db.Close()
fmt.Println("recovery complete")
os.Exit(0)
}
defer db.Close()
fmt.Println("opened but not corrupted: recovery skipped")
os.Exit(0)
}