forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.go
177 lines (163 loc) · 4.76 KB
/
restore.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
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/0xPolygonHermez/zkevm-node/config"
"github.com/0xPolygonHermez/zkevm-node/db"
"github.com/0xPolygonHermez/zkevm-node/log"
pg "github.com/habx/pg-commands"
"github.com/urfave/cli/v2"
)
const (
restorestateDbFlag = "inputfilestate"
restoreHashDbFlag = "inputfileHash"
)
var restoreFlags = []cli.Flag{
&cli.StringFlag{
Name: restorestateDbFlag,
Aliases: []string{"is"},
Usage: "Input file stateDB",
Required: true,
},
&cli.StringFlag{
Name: restoreHashDbFlag,
Aliases: []string{"ih"},
Usage: "Input file hashDB",
Required: true,
},
&configFileFlag,
}
func restore(ctx *cli.Context) error {
// Load config
c, err := config.Load(ctx, false)
if err != nil {
return err
}
setupLog(c.Log)
inputFileStateDB := ctx.String(restorestateDbFlag)
if !strings.Contains(inputFileStateDB, ".sql.tar.gz") {
return errors.New("stateDB input file must end in .sql.tar.gz")
}
d, err := db.NewSQLDB(c.State.DB)
if err != nil {
log.Error("error conecting to stateDB. Error: ", err)
return err
}
_, err = d.Exec(ctx.Context, "DROP SCHEMA IF EXISTS state CASCADE; DROP TABLE IF EXISTS gorp_migrations;")
if err != nil {
log.Error("error dropping state schema or migration table. Error: ", err)
return err
}
port, err := strconv.Atoi(c.State.DB.Port)
if err != nil {
log.Error("error converting port to int. Error: ", err)
return err
}
restore, err := pg.NewRestore(&pg.Postgres{
Host: c.State.DB.Host,
Port: port,
DB: c.State.DB.Name,
Username: c.State.DB.User,
Password: c.State.DB.Password,
})
if err != nil {
log.Error("error: ", err)
return err
}
params := []string{"--no-owner", "--no-acl", "--format=c"}
log.Info("Restore stateDB snapshot started, please wait...")
restoreExec := execCommand(restore, inputFileStateDB, pg.ExecOptions{StreamPrint: false}, params)
if restoreExec.Error != nil {
log.Error("error restoring stateDB snapshot. Error: ", restoreExec.Error.Err)
log.Debug("restoreExec.Output: ", restoreExec.Output)
return err
}
log.Info("Restore stateDB snapshot success")
inputFileHashDB := ctx.String(restoreHashDbFlag)
if !strings.Contains(inputFileHashDB, ".sql.tar.gz") {
return errors.New("hashDb input file must end in .sql.tar.gz")
}
port, err = strconv.Atoi(c.HashDB.Port)
if err != nil {
log.Error("error converting port to int. Error: ", err)
return err
}
d, err = db.NewSQLDB(c.HashDB)
if err != nil {
log.Error("error conecting to hashdb. Error: ", err)
return err
}
_, err = d.Exec(ctx.Context, "DROP SCHEMA IF EXISTS state CASCADE;")
if err != nil {
log.Error("error dropping and creating state schema. Error: ", err)
return err
}
restore, err = pg.NewRestore(&pg.Postgres{
Host: c.HashDB.Host,
Port: port,
DB: c.HashDB.Name,
Username: c.HashDB.User,
Password: c.HashDB.Password,
})
if err != nil {
log.Error("error: ", err)
return err
}
log.Info("Restore HashDB snapshot started, please wait...")
restoreExec = execCommand(restore, inputFileHashDB, pg.ExecOptions{StreamPrint: false}, params)
if restoreExec.Error != nil {
log.Error("error restoring hashDB snapshot. Error: ", restoreExec.Error.Err)
log.Debug("restoreExec.Output: ", restoreExec.Output)
return err
}
log.Info("Restore HashDB snapshot success")
return nil
}
func execCommand(x *pg.Restore, filename string, opts pg.ExecOptions, params []string) pg.Result {
result := pg.Result{}
options := append(params, x.Postgres.Parse()...)
options = append(options, fmt.Sprintf("%s%s", x.Path, filename))
log.Debug("Options: ", options)
result.FullCommand = strings.Join(options, " ")
cmd := exec.Command(pg.PGRestoreCmd, options...) //nolint:gosec
cmd.Env = append(os.Environ(), x.EnvPassword)
stderrIn, _ := cmd.StderrPipe()
go func(stderrIn io.ReadCloser, opts pg.ExecOptions, result *pg.Result) {
output := ""
reader := bufio.NewReader(stderrIn)
for {
line, err := reader.ReadString('\n')
if err != nil {
if errors.Is(err, io.EOF) {
result.Output = output
break
}
result.Error = &pg.ResultError{Err: fmt.Errorf("error reading output: %w", err), CmdOutput: output}
break
}
if opts.StreamPrint {
_, err = fmt.Fprint(opts.StreamDestination, line)
if err != nil {
result.Error = &pg.ResultError{Err: fmt.Errorf("error writing output: %w", err), CmdOutput: output}
break
}
}
output += line
}
}(stderrIn, opts, &result)
err := cmd.Start()
if err != nil {
result.Error = &pg.ResultError{Err: err, CmdOutput: result.Output}
}
err = cmd.Wait()
if exitError, ok := err.(*exec.ExitError); ok {
result.Error = &pg.ResultError{Err: err, ExitCode: exitError.ExitCode(), CmdOutput: result.Output}
}
return result
}