prysm-pulse/beacon-chain/db/restore.go
Radosław Kapka 9d7052796b
Restore database CLI command (#8061)
* restore beacon node db

* revert image name

* move restore out of the kv folder

* remove files from kv folder

* go mod tidy

* Remove usage of prometheus testutil

* add yes/no to prompt text

* restore slasher db

* organize imports

* go mod tidy

* restore validator db

* close slasher db

* defer close backup db in tests

* simplify function literal
2020-12-07 21:36:43 +01:00

47 lines
1.3 KiB
Go

package db
import (
"os"
"path"
"strings"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/fileutil"
"github.com/prysmaticlabs/prysm/shared/promptutil"
"github.com/sirupsen/logrus"
"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]"
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 fileutil.FileExists(path.Join(restoreDir, kv.DatabaseFileName)) {
resp, err := promptutil.ValidatePrompt(
os.Stdin, dbExistsYesNoPrompt, promptutil.ValidateYesOrNo,
)
if err != nil {
return errors.Wrap(err, "could not validate choice")
}
if strings.ToLower(resp) == "n" {
logrus.Info("Restore aborted")
return nil
}
}
if err := fileutil.MkdirAll(restoreDir); err != nil {
return err
}
if err := fileutil.CopyFile(sourceFile, path.Join(restoreDir, kv.DatabaseFileName)); err != nil {
return err
}
logrus.Info("Restore completed successfully")
return nil
}