2020-12-07 20:36:43 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
|
2021-09-17 21:55:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/io/file"
|
|
|
|
"github.com/prysmaticlabs/prysm/io/prompt"
|
2020-12-07 20:36:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const dbExistsYesNoPrompt = "A database file already exists in the target directory. " +
|
|
|
|
"Are you sure that you want to overwrite it? [y/n]"
|
|
|
|
|
2021-03-03 17:05:37 +00:00
|
|
|
// Restore a beacon chain database.
|
|
|
|
func Restore(cliCtx *cli.Context) error {
|
2020-12-07 20:36:43 +00:00
|
|
|
sourceFile := cliCtx.String(cmd.RestoreSourceFileFlag.Name)
|
|
|
|
targetDir := cliCtx.String(cmd.RestoreTargetDirFlag.Name)
|
|
|
|
|
|
|
|
restoreDir := path.Join(targetDir, kv.BeaconNodeDbDirName)
|
2021-09-17 21:55:24 +00:00
|
|
|
if file.FileExists(path.Join(restoreDir, kv.DatabaseFileName)) {
|
|
|
|
resp, err := prompt.ValidatePrompt(
|
|
|
|
os.Stdin, dbExistsYesNoPrompt, prompt.ValidateYesOrNo,
|
2020-12-07 20:36:43 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not validate choice")
|
|
|
|
}
|
2021-01-04 16:46:22 +00:00
|
|
|
if strings.EqualFold(resp, "n") {
|
2021-01-11 20:03:28 +00:00
|
|
|
log.Info("Restore aborted")
|
2020-12-07 20:36:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
if err := file.MkdirAll(restoreDir); err != nil {
|
2020-12-07 20:36:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-17 21:55:24 +00:00
|
|
|
if err := file.CopyFile(sourceFile, path.Join(restoreDir, kv.DatabaseFileName)); err != nil {
|
2020-12-07 20:36:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:03:28 +00:00
|
|
|
log.Info("Restore completed successfully")
|
2020-12-07 20:36:43 +00:00
|
|
|
return nil
|
|
|
|
}
|