github.com/pingcap/tiup@v1.15.1/components/dm/command/meta.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package command
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  func newMetaCmd() *cobra.Command {
    24  	cmd := &cobra.Command{
    25  		Use:   "meta",
    26  		Short: "backup/restore meta information",
    27  	}
    28  
    29  	var filePath string
    30  
    31  	var metaBackupCmd = &cobra.Command{
    32  		Use:   "backup <cluster-name>",
    33  		Short: "backup topology and other information of a cluster",
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			if len(args) != 1 {
    36  				return fmt.Errorf("please input cluster name")
    37  			}
    38  			if filePath == "" {
    39  				filePath = "tiup-cluster_" + args[0] + "_metabackup_" + time.Now().Format(time.RFC3339) + ".tar.gz"
    40  			}
    41  			err := cm.BackupClusterMeta(args[0], filePath)
    42  			if err == nil {
    43  				log.Infof("successfully backup meta of cluster %s on %s", args[0], filePath)
    44  			}
    45  			return err
    46  		},
    47  	}
    48  	metaBackupCmd.Flags().StringVar(&filePath, "file", "", "filepath of output tarball")
    49  
    50  	var metaRestoreCmd = &cobra.Command{
    51  		Use:   "restore <cluster-name> <backup-file>",
    52  		Short: "restore topology and other information of a cluster",
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			if len(args) != 2 {
    55  				return fmt.Errorf("please input cluster name and path to the backup file")
    56  			}
    57  			return cm.RestoreClusterMeta(args[0], args[1], skipConfirm)
    58  		},
    59  	}
    60  
    61  	cmd.AddCommand(metaBackupCmd)
    62  	cmd.AddCommand(metaRestoreCmd)
    63  
    64  	return cmd
    65  }