-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add local snapshots management commands
- `appd snapshots list` list local snapshots - `appd snapshots restore` restore from a local snapshot
- Loading branch information
Showing
11 changed files
with
146 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package snapshot | ||
|
||
import ( | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Cmd returns the snapshots group command | ||
func Cmd(appCreator servertypes.AppCreator) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "snapshots", | ||
Short: "Manage local snapshots", | ||
Long: "Manage local snapshots", | ||
} | ||
cmd.AddCommand( | ||
ListSnapshotsCmd(appCreator), | ||
RestoreSnapshotCmd(appCreator), | ||
) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package snapshot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/server" | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ListSnapshotsCmd returns the command to list local snapshots | ||
func ListSnapshotsCmd(appCreator servertypes.AppCreator) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Short: "List snapshots", | ||
Long: "List snapshots", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := server.GetServerContextFromCmd(cmd) | ||
snapshotStore, err := server.GetSnapshotStore(ctx.Viper) | ||
if err != nil { | ||
return err | ||
} | ||
snapshots, err := snapshotStore.List() | ||
if err != nil { | ||
return fmt.Errorf("failed to list snapshots: %w", err) | ||
} | ||
for _, snapshot := range snapshots { | ||
fmt.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package snapshot | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
|
||
"cosmossdk.io/log" | ||
"github.com/spf13/cobra" | ||
|
||
dbm "github.com/cosmos/cosmos-db" | ||
"github.com/cosmos/cosmos-sdk/server" | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
) | ||
|
||
// RestoreSnapshotCmd returns a command to restore a snapshot | ||
func RestoreSnapshotCmd(appCreator servertypes.AppCreator) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "restore <height> <format>", | ||
Short: "Restore app state from local snapshot", | ||
Long: "Restore app state from local snapshot", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := server.GetServerContextFromCmd(cmd) | ||
|
||
height, err := strconv.ParseUint(args[0], 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
format, err := strconv.ParseUint(args[1], 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
home := ctx.Config.RootDir | ||
db, err := openDB(home, server.GetAppDBBackend(ctx.Viper)) | ||
if err != nil { | ||
return err | ||
} | ||
logger := log.NewLogger(cmd.OutOrStdout()) | ||
app := appCreator(logger, db, nil, ctx.Viper) | ||
|
||
sm := app.SnapshotManager() | ||
return sm.RestoreLocalSnapshot(height, uint32(format)) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) { | ||
dataDir := filepath.Join(rootDir, "data") | ||
return dbm.NewDB("application", backendType, dataDir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters