diff options
Diffstat (limited to 'internal/cli/gitalybackup/restore.go')
-rw-r--r-- | internal/cli/gitalybackup/restore.go | 93 |
1 files changed, 80 insertions, 13 deletions
diff --git a/internal/cli/gitalybackup/restore.go b/internal/cli/gitalybackup/restore.go index 9bff6fe4f..628227341 100644 --- a/internal/cli/gitalybackup/restore.go +++ b/internal/cli/gitalybackup/restore.go @@ -4,12 +4,12 @@ import ( "context" "encoding/json" "errors" - "flag" "fmt" "io" + "os" "runtime" - "strings" + cli "github.com/urfave/cli/v2" "gitlab.com/gitlab-org/gitaly/v16/internal/backup" "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage" "gitlab.com/gitlab-org/gitaly/v16/internal/grpc/client" @@ -35,17 +35,84 @@ type restoreSubcommand struct { serverSide bool } -func (cmd *restoreSubcommand) Flags(fs *flag.FlagSet) { - fs.StringVar(&cmd.backupPath, "path", "", "repository backup path") - fs.IntVar(&cmd.parallel, "parallel", runtime.NumCPU(), "maximum number of parallel restores") - fs.IntVar(&cmd.parallelStorage, "parallel-storage", 2, "maximum number of parallel restores per storage. Note: actual parallelism when combined with `-parallel` depends on the order the repositories are received.") - fs.StringVar(&cmd.layout, "layout", "pointer", "how backup files are located. Either pointer or legacy.") - fs.Func("remove-all-repositories", "comma-separated list of storage names to have all repositories removed from before restoring.", func(removeAll string) error { - cmd.removeAllRepositories = strings.Split(removeAll, ",") - return nil - }) - fs.StringVar(&cmd.backupID, "id", "", "ID of full backup to restore. If not specified, the latest backup is restored.") - fs.BoolVar(&cmd.serverSide, "server-side", false, "use server-side backups. Note: The feature is not ready for production use.") +func (cmd *restoreSubcommand) Flags(ctx *cli.Context) { + cmd.backupPath = ctx.String("path") + cmd.parallel = ctx.Int("parallel") + cmd.parallelStorage = ctx.Int("parallel-storage") + cmd.layout = ctx.String("layout") + cmd.removeAllRepositories = ctx.StringSlice("remove-all-repositories") + cmd.backupID = ctx.String("id") + cmd.serverSide = ctx.Bool("server-side") +} + +func restoreFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "path", + Usage: "repository backup path", + }, + &cli.IntFlag{ + Name: "parallel", + Usage: "maximum number of parallel backups", + Value: runtime.NumCPU(), + }, + &cli.IntFlag{ + Name: "parallel-storage", + Usage: "maximum number of parallel backups per storage. Note: actual parallelism when combined with `-parallel` depends on the order the repositories are received.", + Value: 2, + }, + &cli.StringFlag{ + Name: "layout", + Usage: "how backup files are located. Either pointer or legacy.", + Value: "pointer", + }, + &cli.StringSliceFlag{ + Name: "remove-all-repositories", + Usage: "comma-separated list of storage names to have all repositories removed from before restoring.", + }, + &cli.StringFlag{ + Name: "id", + Usage: "ID of full backup to restore. If not specified, the latest backup is restored.", + }, + &cli.BoolFlag{ + Name: "server-side", + Usage: "use server-side backups. Note: The feature is not ready for production use.", + Value: false, + }, + } +} + +func newRestoreCommand() *cli.Command { + return &cli.Command{ + Name: "restore", + Usage: "Restore backup file", + Action: restoreAction, + Flags: restoreFlags(), + } +} + +func restoreAction(cctx *cli.Context) error { + logger, err := log.Configure(os.Stdout, "json", "") + if err != nil { + fmt.Printf("configuring logger failed: %v", err) + os.Exit(1) + } + + ctx, err := storage.InjectGitalyServersEnv(context.Background()) + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + + subcmd := restoreSubcommand{} + subcmd.Flags(cctx) + fmt.Print(cctx) + + if err := subcmd.Run(ctx, logger, os.Stdin, os.Stdout); err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + return nil } func (cmd *restoreSubcommand) Run(ctx context.Context, logger log.Logger, stdin io.Reader, stdout io.Writer) error { |