func newRunCommand()

in banyand/backup/restore.go [56:134]


func newRunCommand() *cobra.Command {
	var (
		source       string
		streamRoot   string
		measureRoot  string
		propertyRoot string
		fsConfig     remote.FsConfig
	)
	cmd := &cobra.Command{
		Use:   "run",
		Short: "Restore BanyanDB data from remote storage",
		RunE: func(_ *cobra.Command, _ []string) error {
			if streamRoot == "" && measureRoot == "" && propertyRoot == "" {
				return errors.New("at least one of stream-root-path, measure-root-path, or property-root-path is required")
			}
			if source == "" {
				return errors.New("source is required")
			}
			fs, err := newFS(source, &fsConfig)
			if err != nil {
				return err
			}
			defer fs.Close()

			var errs error

			if streamRoot != "" {
				timeDirPath := filepath.Join(streamRoot, "stream", "time-dir")
				if data, err := os.ReadFile(timeDirPath); err == nil {
					timeDir := strings.TrimSpace(string(data))
					if err = restoreCatalog(fs, timeDir, streamRoot, commonv1.Catalog_CATALOG_STREAM); err != nil {
						errs = multierr.Append(errs, fmt.Errorf("stream restore failed: %w", err))
					} else {
						_ = os.Remove(timeDirPath)
					}
				} else if !errors.Is(err, os.ErrNotExist) {
					return err
				}
			}
			if measureRoot != "" {
				timeDirPath := filepath.Join(measureRoot, "measure", "time-dir")
				if data, err := os.ReadFile(timeDirPath); err == nil {
					timeDir := strings.TrimSpace(string(data))
					if err = restoreCatalog(fs, timeDir, measureRoot, commonv1.Catalog_CATALOG_MEASURE); err != nil {
						errs = multierr.Append(errs, fmt.Errorf("measure restore failed: %w", err))
					} else {
						_ = os.Remove(timeDirPath)
					}
				} else if !errors.Is(err, os.ErrNotExist) {
					return err
				}
			}
			if propertyRoot != "" {
				timeDirPath := filepath.Join(propertyRoot, "property", "time-dir")
				if data, err := os.ReadFile(timeDirPath); err == nil {
					timeDir := strings.TrimSpace(string(data))
					if err = restoreCatalog(fs, timeDir, propertyRoot, commonv1.Catalog_CATALOG_PROPERTY); err != nil {
						errs = multierr.Append(errs, fmt.Errorf("property restore failed: %w", err))
					} else {
						_ = os.Remove(timeDirPath)
					}
				} else if !errors.Is(err, os.ErrNotExist) {
					return err
				}
			}

			return errs
		},
	}
	cmd.Flags().StringVar(&source, "source", "", "Source URL (e.g., file:///backups)")
	cmd.Flags().StringVar(&streamRoot, "stream-root-path", "/tmp", "Root directory for stream catalog")
	cmd.Flags().StringVar(&measureRoot, "measure-root-path", "/tmp", "Root directory for measure catalog")
	cmd.Flags().StringVar(&propertyRoot, "property-root-path", "/tmp", "Root directory for property catalog")
	cmd.Flags().StringVar(&fsConfig.S3ConfigFilePath, "s3-config-file", "", "Path to the s3 configuration file")
	cmd.Flags().StringVar(&fsConfig.S3CredentialFilePath, "s3-credential-file", "", "Path to the s3 credential file")
	cmd.Flags().StringVar(&fsConfig.S3ProfileName, "s3-profile", "", "S3 profile name")

	return cmd
}