github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/dataprotection/restore.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package dataprotection
    21  
    22  import (
    23  	"github.com/spf13/cobra"
    24  	"k8s.io/cli-runtime/pkg/genericiooptions"
    25  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    26  	"k8s.io/kubectl/pkg/util/templates"
    27  
    28  	"github.com/1aal/kubeblocks/pkg/cli/cmd/cluster"
    29  	"github.com/1aal/kubeblocks/pkg/cli/create"
    30  	"github.com/1aal/kubeblocks/pkg/cli/printer"
    31  	"github.com/1aal/kubeblocks/pkg/cli/types"
    32  	"github.com/1aal/kubeblocks/pkg/cli/util"
    33  )
    34  
    35  var (
    36  	createRestoreExample = templates.Examples(`
    37  		# restore a new cluster from a backup
    38  		kbcli dp restore mybackup --cluster cluster-name`)
    39  )
    40  
    41  func newRestoreCommand(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
    42  	o := cluster.CreateRestoreOptions{}
    43  	o.CreateOptions = create.CreateOptions{
    44  		IOStreams: streams,
    45  		Factory:   f,
    46  		Options:   o,
    47  	}
    48  
    49  	clusterName := ""
    50  
    51  	cmd := &cobra.Command{
    52  		Use:               "restore",
    53  		Short:             "Restore a new cluster from backup",
    54  		ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.BackupGVR()),
    55  		Example:           createRestoreExample,
    56  		Run: func(cmd *cobra.Command, args []string) {
    57  			if len(args) > 0 {
    58  				o.Backup = args[0]
    59  			}
    60  			if clusterName != "" {
    61  				o.Args = []string{clusterName}
    62  			}
    63  			cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
    64  			util.CheckErr(o.Complete())
    65  			util.CheckErr(o.Validate())
    66  			util.CheckErr(o.Run())
    67  		},
    68  	}
    69  
    70  	cmd.Flags().StringVar(&clusterName, "cluster", "", "The cluster to restore")
    71  	cmd.Flags().StringVar(&o.RestoreTimeStr, "restore-to-time", "", "point in time recovery(PITR)")
    72  	cmd.Flags().StringVar(&o.VolumeRestorePolicy, "volume-restore-policy", "Parallel", "the volume claim restore policy, supported values: [Serial, Parallel]")
    73  	return cmd
    74  }