github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/cmd/br/restore.go (about) 1 // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. 2 3 package main 4 5 import ( 6 "github.com/pingcap/errors" 7 "github.com/pingcap/log" 8 "github.com/pingcap/tidb/session" 9 "github.com/spf13/cobra" 10 "go.uber.org/zap" 11 "sourcegraph.com/sourcegraph/appdash" 12 13 "github.com/pingcap/br/pkg/gluetikv" 14 "github.com/pingcap/br/pkg/summary" 15 "github.com/pingcap/br/pkg/task" 16 "github.com/pingcap/br/pkg/trace" 17 "github.com/pingcap/br/pkg/utils" 18 "github.com/pingcap/br/pkg/version/build" 19 ) 20 21 func runRestoreCommand(command *cobra.Command, cmdName string) error { 22 cfg := task.RestoreConfig{Config: task.Config{LogProgress: HasLogFile()}} 23 if err := cfg.ParseFromFlags(command.Flags()); err != nil { 24 command.SilenceUsage = false 25 return errors.Trace(err) 26 } 27 28 ctx := GetDefaultContext() 29 if cfg.EnableOpenTracing { 30 var store *appdash.MemoryStore 31 ctx, store = trace.TracerStartSpan(ctx) 32 defer trace.TracerFinishSpan(ctx, store) 33 } 34 if err := task.RunRestore(GetDefaultContext(), tidbGlue, cmdName, &cfg); err != nil { 35 log.Error("failed to restore", zap.Error(err)) 36 return errors.Trace(err) 37 } 38 return nil 39 } 40 41 func runLogRestoreCommand(command *cobra.Command) error { 42 cfg := task.LogRestoreConfig{Config: task.Config{LogProgress: HasLogFile()}} 43 if err := cfg.ParseFromFlags(command.Flags()); err != nil { 44 command.SilenceUsage = false 45 return errors.Trace(err) 46 } 47 48 ctx := GetDefaultContext() 49 if cfg.EnableOpenTracing { 50 var store *appdash.MemoryStore 51 ctx, store = trace.TracerStartSpan(ctx) 52 defer trace.TracerFinishSpan(ctx, store) 53 } 54 if err := task.RunLogRestore(GetDefaultContext(), tidbGlue, &cfg); err != nil { 55 log.Error("failed to restore", zap.Error(err)) 56 return errors.Trace(err) 57 } 58 return nil 59 } 60 61 func runRestoreRawCommand(command *cobra.Command, cmdName string) error { 62 cfg := task.RestoreRawConfig{ 63 RawKvConfig: task.RawKvConfig{Config: task.Config{LogProgress: HasLogFile()}}, 64 } 65 if err := cfg.ParseFromFlags(command.Flags()); err != nil { 66 command.SilenceUsage = false 67 return errors.Trace(err) 68 } 69 70 ctx := GetDefaultContext() 71 if cfg.EnableOpenTracing { 72 var store *appdash.MemoryStore 73 ctx, store = trace.TracerStartSpan(ctx) 74 defer trace.TracerFinishSpan(ctx, store) 75 } 76 if err := task.RunRestoreRaw(GetDefaultContext(), gluetikv.Glue{}, cmdName, &cfg); err != nil { 77 log.Error("failed to restore raw kv", zap.Error(err)) 78 return errors.Trace(err) 79 } 80 return nil 81 } 82 83 // NewRestoreCommand returns a restore subcommand. 84 func NewRestoreCommand() *cobra.Command { 85 command := &cobra.Command{ 86 Use: "restore", 87 Short: "restore a TiDB/TiKV cluster", 88 SilenceUsage: true, 89 PersistentPreRunE: func(c *cobra.Command, args []string) error { 90 if err := Init(c); err != nil { 91 return errors.Trace(err) 92 } 93 build.LogInfo(build.BR) 94 utils.LogEnvVariables() 95 task.LogArguments(c) 96 session.DisableStats4Test() 97 98 summary.SetUnit(summary.RestoreUnit) 99 return nil 100 }, 101 } 102 command.AddCommand( 103 newFullRestoreCommand(), 104 newDBRestoreCommand(), 105 newTableRestoreCommand(), 106 newLogRestoreCommand(), 107 newRawRestoreCommand(), 108 ) 109 task.DefineRestoreFlags(command.PersistentFlags()) 110 111 return command 112 } 113 114 func newFullRestoreCommand() *cobra.Command { 115 command := &cobra.Command{ 116 Use: "full", 117 Short: "restore all tables", 118 Args: cobra.NoArgs, 119 RunE: func(cmd *cobra.Command, _ []string) error { 120 return runRestoreCommand(cmd, "Full restore") 121 }, 122 } 123 task.DefineFilterFlags(command, filterOutSysAndMemTables) 124 return command 125 } 126 127 func newDBRestoreCommand() *cobra.Command { 128 command := &cobra.Command{ 129 Use: "db", 130 Short: "restore tables in a database from the backup data", 131 Args: cobra.NoArgs, 132 RunE: func(cmd *cobra.Command, _ []string) error { 133 return runRestoreCommand(cmd, "Database restore") 134 }, 135 } 136 task.DefineDatabaseFlags(command) 137 return command 138 } 139 140 func newTableRestoreCommand() *cobra.Command { 141 command := &cobra.Command{ 142 Use: "table", 143 Short: "restore a table from the backup data", 144 Args: cobra.NoArgs, 145 RunE: func(cmd *cobra.Command, _ []string) error { 146 return runRestoreCommand(cmd, "Table restore") 147 }, 148 } 149 task.DefineTableFlags(command) 150 return command 151 } 152 153 func newLogRestoreCommand() *cobra.Command { 154 command := &cobra.Command{ 155 Use: "cdclog", 156 Short: "(experimental) restore data from cdc log backup", 157 Args: cobra.NoArgs, 158 RunE: func(cmd *cobra.Command, _ []string) error { 159 return runLogRestoreCommand(cmd) 160 }, 161 } 162 task.DefineFilterFlags(command, filterOutSysAndMemTables) 163 task.DefineLogRestoreFlags(command) 164 return command 165 } 166 167 func newRawRestoreCommand() *cobra.Command { 168 command := &cobra.Command{ 169 Use: "raw", 170 Short: "(experimental) restore a raw kv range to TiKV cluster", 171 Args: cobra.NoArgs, 172 RunE: func(cmd *cobra.Command, _ []string) error { 173 return runRestoreRawCommand(cmd, "Raw restore") 174 }, 175 } 176 177 task.DefineRawRestoreFlags(command) 178 return command 179 }