prysm-pulse/beacon-chain/db/restore.go
terencechain 9387a36b66
Refactor Exported Names to Follow Golang Best Practices (#13075)
* Fix exported names that start with a package name

* A few more renames

* Fix exported names that start with a package name

* A few more renames

* Radek's feedback

* Fix conflict

* fix keymanager test

* Fix comments

---------

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2023-10-20 16:45:33 +00:00

47 lines
1.2 KiB
Go

package db
import (
"os"
"path"
"strings"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db/kv"
"github.com/prysmaticlabs/prysm/v4/cmd"
"github.com/prysmaticlabs/prysm/v4/io/file"
"github.com/prysmaticlabs/prysm/v4/io/prompt"
"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]"
// Restore a beacon chain database.
func Restore(cliCtx *cli.Context) error {
sourceFile := cliCtx.String(cmd.RestoreSourceFileFlag.Name)
targetDir := cliCtx.String(cmd.RestoreTargetDirFlag.Name)
restoreDir := path.Join(targetDir, kv.BeaconNodeDbDirName)
if file.Exists(path.Join(restoreDir, kv.DatabaseFileName)) {
resp, err := prompt.ValidatePrompt(
os.Stdin, dbExistsYesNoPrompt, prompt.ValidateYesOrNo,
)
if err != nil {
return errors.Wrap(err, "could not validate choice")
}
if strings.EqualFold(resp, "n") {
log.Info("Restore aborted")
return nil
}
}
if err := file.MkdirAll(restoreDir); err != nil {
return err
}
if err := file.CopyFile(sourceFile, path.Join(restoreDir, kv.DatabaseFileName)); err != nil {
return err
}
log.Info("Restore completed successfully")
return nil
}