Files
opencloud/opencloud/pkg/command/backup.go
Christian Richter 3cb002977f migrate backup cli command from urfave/cli to spf13/cobra
Signed-off-by: Christian Richter <c.richter@opencloud.eu>
2025-12-15 16:40:26 +01:00

93 lines
2.7 KiB
Go

package command
import (
"errors"
"fmt"
"github.com/opencloud-eu/opencloud/opencloud/pkg/backup"
"github.com/opencloud-eu/opencloud/opencloud/pkg/register"
"github.com/opencloud-eu/opencloud/pkg/config"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/pkg/config/parser"
decomposedbs "github.com/opencloud-eu/reva/v2/pkg/storage/fs/decomposed/blobstore"
decomposeds3bs "github.com/opencloud-eu/reva/v2/pkg/storage/fs/decomposeds3/blobstore"
"github.com/spf13/cobra"
)
// BackupCommand is the entrypoint for the backup command
func BackupCommand(cfg *config.Config) *cobra.Command {
bckCmd := &cobra.Command{
Use: "backup",
Short: "OpenCloud backup functionality",
PreRunE: func(cmd *cobra.Command, args []string) error {
return configlog.ReturnError(parser.ParseConfig(cfg, true))
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Read the docs")
return nil
},
}
bckCmd.AddCommand(ConsistencyCommand(cfg))
return bckCmd
}
// ConsistencyCommand is the entrypoint for the consistency Command
func ConsistencyCommand(cfg *config.Config) *cobra.Command {
consCmd := &cobra.Command{
Use: "consistency",
Short: "check backup consistency",
RunE: func(cmd *cobra.Command, args []string) error {
basePath := cmd.Flag("basepath").Value.String()
if basePath == "" {
fmt.Println("basepath is required")
_ = cmd.Help()
return nil
}
var (
bs backup.ListBlobstore
err error
)
switch cmd.Flag("blobstore").Value.String() {
case "decomposeds3":
bs, err = decomposeds3bs.New(
cfg.StorageUsers.Drivers.DecomposedS3.Endpoint,
cfg.StorageUsers.Drivers.DecomposedS3.Region,
cfg.StorageUsers.Drivers.DecomposedS3.Bucket,
cfg.StorageUsers.Drivers.DecomposedS3.AccessKey,
cfg.StorageUsers.Drivers.DecomposedS3.SecretKey,
decomposeds3bs.Options{},
)
case "decomposed":
bs, err = decomposedbs.New(basePath)
case "none":
bs = nil
default:
err = errors.New("blobstore type not supported")
}
if err != nil {
fmt.Println(err)
return err
}
if err := backup.CheckProviderConsistency(basePath, bs, cmd.Flag("fail").Changed); err != nil {
fmt.Println(err)
return err
}
return nil
},
}
consCmd.Flags().StringP("basepath", "p", "", "the basepath of the decomposedfs (e.g. /var/tmp/opencloud/storage/users)")
err := consCmd.MarkFlagRequired("basepath")
if err != nil {
fmt.Println(err)
}
consCmd.Flags().StringP("blobstore", "b", "", "the blobstore type. Can be (none, decomposed, decomposeds3). Default decomposed")
consCmd.Flags().Bool("fail", false, "exit with non-zero status if consistency check fails")
return consCmd
}
func init() {
register.AddCommand(BackupCommand)
}