github.com/cosmos/cosmos-sdk@v0.50.10/client/snapshot/restore.go (about) 1 package snapshot 2 3 import ( 4 "path/filepath" 5 "strconv" 6 7 dbm "github.com/cosmos/cosmos-db" 8 "github.com/spf13/cobra" 9 10 "cosmossdk.io/log" 11 12 "github.com/cosmos/cosmos-sdk/server" 13 servertypes "github.com/cosmos/cosmos-sdk/server/types" 14 ) 15 16 // RestoreSnapshotCmd returns a command to restore a snapshot 17 func RestoreSnapshotCmd(appCreator servertypes.AppCreator) *cobra.Command { 18 cmd := &cobra.Command{ 19 Use: "restore <height> <format>", 20 Short: "Restore app state from local snapshot", 21 Long: "Restore app state from local snapshot", 22 Args: cobra.ExactArgs(2), 23 RunE: func(cmd *cobra.Command, args []string) error { 24 ctx := server.GetServerContextFromCmd(cmd) 25 26 height, err := strconv.ParseUint(args[0], 10, 64) 27 if err != nil { 28 return err 29 } 30 format, err := strconv.ParseUint(args[1], 10, 32) 31 if err != nil { 32 return err 33 } 34 35 home := ctx.Config.RootDir 36 db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) 37 if err != nil { 38 return err 39 } 40 logger := log.NewLogger(cmd.OutOrStdout()) 41 app := appCreator(logger, db, nil, ctx.Viper) 42 43 sm := app.SnapshotManager() 44 return sm.RestoreLocalSnapshot(height, uint32(format)) 45 }, 46 } 47 return cmd 48 } 49 50 func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) { 51 dataDir := filepath.Join(rootDir, "data") 52 return dbm.NewDB("application", backendType, dataDir) 53 }