lotus/cmd/lotus-storage-miner/backup.go

116 lines
2.7 KiB
Go
Raw Normal View History

2020-10-01 11:58:26 +00:00
package main
import (
"fmt"
2020-10-01 12:46:07 +00:00
"os"
2020-10-01 11:58:26 +00:00
2020-10-01 12:46:07 +00:00
logging "github.com/ipfs/go-log/v2"
"github.com/mitchellh/go-homedir"
2020-10-01 11:58:26 +00:00
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
lcli "github.com/filecoin-project/lotus/cli"
2020-10-01 12:46:07 +00:00
"github.com/filecoin-project/lotus/lib/backupds"
"github.com/filecoin-project/lotus/node/repo"
2020-10-01 11:58:26 +00:00
)
var backupCmd = &cli.Command{
Name: "backup",
Usage: "Create node metadata backup",
Description: `The backup command writes a copy of node metadata under the specified path
2020-10-01 12:46:07 +00:00
Online backups:
2020-10-01 11:58:26 +00:00
For security reasons, the daemon must be have LOTUS_BACKUP_BASE_PATH env var set
to a path where backup files are supposed to be saved, and the path specified in
this command must be within this base path`,
2020-10-01 12:46:07 +00:00
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "offline",
Usage: "create backup without the node running",
},
},
2020-10-01 11:58:26 +00:00
ArgsUsage: "[backup file path]",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 1 {
return xerrors.Errorf("expected 1 argument")
}
2020-10-01 12:46:07 +00:00
if cctx.Bool("offline") {
return offlineBackup(cctx)
2020-10-01 11:58:26 +00:00
}
2020-10-01 12:46:07 +00:00
return onlineBackup(cctx)
2020-10-01 11:58:26 +00:00
},
}
2020-10-01 12:46:07 +00:00
func offlineBackup(cctx *cli.Context) error {
logging.SetLogLevel("badger", "ERROR") // nolint:errcheck
repoPath := cctx.String(FlagMinerRepo)
r, err := repo.NewFS(repoPath)
if err != nil {
return err
}
ok, err := r.Exists()
if err != nil {
return err
}
if !ok {
return xerrors.Errorf("repo at '%s' is not initialized", cctx.String(FlagMinerRepo))
}
lr, err := r.Lock(repo.StorageMiner)
if err != nil {
return xerrors.Errorf("locking repo: %w", err)
}
defer lr.Close() // nolint:errcheck
mds, err := lr.Datastore("/metadata")
if err != nil {
return xerrors.Errorf("getting metadata datastore: %w", err)
}
bds := backupds.Wrap(mds)
fpath, err := homedir.Expand(cctx.Args().First())
if err != nil {
return xerrors.Errorf("expanding file path: %w", err)
}
out, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return xerrors.Errorf("opening backup file %s: %w", fpath, err)
}
if err := bds.Backup(out); err != nil {
if cerr := out.Close(); cerr != nil {
log.Errorw("error closing backup file while handling backup error", "closeErr", cerr, "backupErr", err)
}
return xerrors.Errorf("backup error: %w", err)
}
if err := out.Close(); err != nil {
return xerrors.Errorf("closing backup file: %w", err)
}
return nil
}
func onlineBackup(cctx *cli.Context) error {
api, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return xerrors.Errorf("getting api: %w (if the node isn't running you can use the --offline flag)", err)
}
defer closer()
err = api.CreateBackup(lcli.ReqContext(cctx), cctx.Args().First())
if err != nil {
return err
}
fmt.Println("Success")
return nil
}