github.com/pingcap/tiup@v1.15.1/components/cluster/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 "strings" 19 "time" 20 21 "github.com/spf13/cobra" 22 ) 23 24 func newMetaCmd() *cobra.Command { 25 cmd := &cobra.Command{ 26 Use: "meta", 27 Short: "backup/restore meta information", 28 } 29 30 var filePath string 31 32 var metaBackupCmd = &cobra.Command{ 33 Use: "backup <cluster-name>", 34 Short: "backup topology and other information of cluster", 35 RunE: func(cmd *cobra.Command, args []string) error { 36 if len(args) != 1 { 37 return fmt.Errorf("please input cluster-name") 38 } 39 if filePath == "" { 40 filePath = "tiup-cluster_" + args[0] + "_metabackup_" + strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "-") + ".tar.gz" 41 } 42 err := cm.BackupClusterMeta(args[0], filePath) 43 if err == nil { 44 log.Infof("successfully backup meta of cluster %s on %s", args[0], filePath) 45 } 46 return err 47 }, 48 } 49 metaBackupCmd.Flags().StringVar(&filePath, "file", "", "filepath of output tarball") 50 51 var metaRestoreCmd = &cobra.Command{ 52 Use: "restore <cluster-name> <backup-file>", 53 Short: "restore topology and other information of cluster", 54 RunE: func(cmd *cobra.Command, args []string) error { 55 if len(args) != 2 { 56 return fmt.Errorf("please input cluster-name and backup-file") 57 } 58 return cm.RestoreClusterMeta(args[0], args[1], skipConfirm) 59 }, 60 } 61 62 cmd.AddCommand(metaBackupCmd) 63 cmd.AddCommand(metaRestoreCmd) 64 65 return cmd 66 }