-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
backupengine.go
420 lines (371 loc) · 13.9 KB
/
backupengine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysqlctl
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/mysqlctl/backupstorage"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vterrors"
)
var (
// BackupEngineImplementation is the implementation to use for BackupEngine
backupEngineImplementation = flag.String("backup_engine_implementation", builtinBackupEngineName, "Specifies which implementation to use for creating new backups (builtin or xtrabackup). Restores will always be done with whichever engine created a given backup.")
)
// BackupEngine is the interface to take a backup with a given engine.
type BackupEngine interface {
ExecuteBackup(ctx context.Context, params BackupParams, bh backupstorage.BackupHandle) (bool, error)
ShouldDrainForBackup() bool
}
// BackupParams is the struct that holds all params passed to ExecuteBackup
type BackupParams struct {
Cnf *Mycnf
Mysqld MysqlDaemon
Logger logutil.Logger
// Concurrency is the value of -concurrency flag given to Backup command
// It determines how many files are processed in parallel
Concurrency int
// Extra env variables for pre-backup and post-backup transform hooks
HookExtraEnv map[string]string
// TopoServer, Keyspace and Shard are used to discover primary tablet
TopoServer *topo.Server
// Keyspace and Shard are used to infer the directory where backups should be stored
Keyspace string
Shard string
// TabletAlias is used along with backupTime to construct the backup name
TabletAlias string
// BackupTime is the time at which the backup is being started
BackupTime time.Time
}
// RestoreParams is the struct that holds all params passed to ExecuteRestore
type RestoreParams struct {
Cnf *Mycnf
Mysqld MysqlDaemon
Logger logutil.Logger
// Concurrency is the value of -restore_concurrency flag (init restore parameter)
// It determines how many files are processed in parallel
Concurrency int
// Extra env variables for pre-restore and post-restore transform hooks
HookExtraEnv map[string]string
// Metadata to write into database after restore. See PopulateMetadataTables
LocalMetadata map[string]string
// DeleteBeforeRestore tells us whether existing data should be deleted before
// restoring. This is always set to false when starting a tablet with -restore_from_backup,
// but is set to true when executing a RestoreFromBackup command on an already running vttablet
DeleteBeforeRestore bool
// DbName is the name of the managed database / schema
DbName string
// Keyspace and Shard are used to infer the directory where backups are stored
Keyspace string
Shard string
// StartTime: if non-zero, look for a backup that was taken at or before this time
// Otherwise, find the most recent backup
StartTime time.Time
}
// RestoreEngine is the interface to restore a backup with a given engine.
// Returns the manifest of a backup if successful, otherwise returns an error
type RestoreEngine interface {
ExecuteRestore(ctx context.Context, params RestoreParams, bh backupstorage.BackupHandle) (*BackupManifest, error)
}
// BackupRestoreEngine is a combination of BackupEngine and RestoreEngine.
type BackupRestoreEngine interface {
BackupEngine
RestoreEngine
}
// BackupRestoreEngineMap contains the registered implementations for
// BackupEngine and RestoreEngine.
var BackupRestoreEngineMap = make(map[string]BackupRestoreEngine)
// GetBackupEngine returns the BackupEngine implementation that should be used
// to create new backups.
//
// To restore a backup, you should instead get the appropriate RestoreEngine for
// a particular backup by calling GetRestoreEngine().
//
// This must only be called after flags have been parsed.
func GetBackupEngine() (BackupEngine, error) {
name := *backupEngineImplementation
be, ok := BackupRestoreEngineMap[name]
if !ok {
return nil, vterrors.Errorf(vtrpc.Code_NOT_FOUND, "unknown BackupEngine implementation %q", name)
}
return be, nil
}
// GetRestoreEngine returns the RestoreEngine implementation to restore a given backup.
// It reads the MANIFEST file from the backup to check which engine was used to create it.
func GetRestoreEngine(ctx context.Context, backup backupstorage.BackupHandle) (RestoreEngine, error) {
manifest, err := GetBackupManifest(ctx, backup)
if err != nil {
return nil, vterrors.Wrap(err, "can't get backup MANIFEST")
}
engine := manifest.BackupMethod
if engine == "" {
// The builtin engine is the only one that ever left BackupMethod unset.
engine = builtinBackupEngineName
}
re, ok := BackupRestoreEngineMap[engine]
if !ok {
return nil, vterrors.Errorf(vtrpc.Code_NOT_FOUND, "can't restore backup created with %q engine; no such BackupEngine implementation is registered", manifest.BackupMethod)
}
return re, nil
}
// GetBackupManifest returns the common fields of the MANIFEST file for a given backup.
func GetBackupManifest(ctx context.Context, backup backupstorage.BackupHandle) (*BackupManifest, error) {
manifest := &BackupManifest{}
if err := getBackupManifestInto(ctx, backup, manifest); err != nil {
return nil, err
}
return manifest, nil
}
// getBackupManifestInto fetches and decodes a MANIFEST file into the specified object.
func getBackupManifestInto(ctx context.Context, backup backupstorage.BackupHandle, outManifest interface{}) error {
file, err := backup.ReadFile(ctx, backupManifestFileName)
if err != nil {
return vterrors.Wrap(err, "can't read MANIFEST")
}
defer file.Close()
if err := json.NewDecoder(file).Decode(outManifest); err != nil {
return vterrors.Wrap(err, "can't decode MANIFEST")
}
return nil
}
// BackupManifest defines the common fields in the MANIFEST file.
// All backup engines must include at least these fields. They are free to add
// their own custom fields by embedding this struct anonymously into their own
// custom struct, as long as their custom fields don't have conflicting names.
type BackupManifest struct {
// BackupMethod is the name of the backup engine that created this backup.
// If this is empty, the backup engine is assumed to be "builtin" since that
// was the only engine that ever left this field empty. All new backup
// engines are required to set this field to the backup engine name.
BackupMethod string
// Position is the replication position at which the backup was taken.
Position mysql.Position
// BackupTime is when the backup was taken in UTC time (RFC 3339 format)
BackupTime string
// FinishedTime is the time (in RFC 3339 format, UTC) at which the backup finished, if known.
// Some backups may not set this field if they were created before the field was added.
FinishedTime string
}
// FindBackupToRestore returns a selected candidate backup to be restored.
// It returns the most recent backup that is complete, meaning it has a valid
// MANIFEST file.
func FindBackupToRestore(ctx context.Context, params RestoreParams, bhs []backupstorage.BackupHandle) (backupstorage.BackupHandle, error) {
var bh backupstorage.BackupHandle
var index int
// if a StartTime is provided in params, then find a backup that was taken at or before that time
checkBackupTime := !params.StartTime.IsZero()
backupDir := GetBackupDir(params.Keyspace, params.Shard)
for index = len(bhs) - 1; index >= 0; index-- {
bh = bhs[index]
// Check that the backup MANIFEST exists and can be successfully decoded.
bm, err := GetBackupManifest(ctx, bh)
if err != nil {
params.Logger.Warningf("Possibly incomplete backup %v in directory %v on BackupStorage: can't read MANIFEST: %v)", bh.Name(), backupDir, err)
continue
}
var backupTime time.Time
if checkBackupTime {
backupTime, err = time.Parse(time.RFC3339, bm.BackupTime)
if err != nil {
params.Logger.Warningf("Restore: skipping backup %v/%v with invalid time %v: %v", backupDir, bh.Name(), bm.BackupTime, err)
continue
}
}
if !checkBackupTime || backupTime.Equal(params.StartTime) || backupTime.Before(params.StartTime) {
if !checkBackupTime {
params.Logger.Infof("Restore: found latest backup %v %v to restore", bh.Directory(), bh.Name())
} else {
params.Logger.Infof("Restore: found backup %v %v to restore using the specified timestamp of '%v'", bh.Directory(), bh.Name(), params.StartTime.Format(BackupTimestampFormat))
}
break
}
}
if index < 0 {
if checkBackupTime {
params.Logger.Errorf("No valid backup found before time %v", params.StartTime.Format(BackupTimestampFormat))
}
// There is at least one attempted backup, but none could be read.
// This implies there is data we ought to have, so it's not safe to start
// up empty.
return nil, ErrNoCompleteBackup
}
return bh, nil
}
func prepareToRestore(ctx context.Context, cnf *Mycnf, mysqld MysqlDaemon, logger logutil.Logger) error {
// shutdown mysqld if it is running
logger.Infof("Restore: shutdown mysqld")
if err := mysqld.Shutdown(ctx, cnf, true); err != nil {
return err
}
logger.Infof("Restore: deleting existing files")
if err := removeExistingFiles(cnf); err != nil {
return err
}
logger.Infof("Restore: reinit config file")
if err := mysqld.ReinitConfig(ctx, cnf); err != nil {
return err
}
return nil
}
// create restore state file
func createStateFile(cnf *Mycnf) error {
// if we start writing content to this file:
// change RD_ONLY to RDWR
// change Create to Open
// rename func to openStateFile
// change to return a *File
fname := filepath.Join(cnf.TabletDir(), RestoreState)
fd, err := os.Create(fname)
if err != nil {
return fmt.Errorf("unable to create file: %v", err)
}
if err = fd.Close(); err != nil {
return fmt.Errorf("unable to close file: %v", err)
}
return nil
}
// delete restore state file
func removeStateFile(cnf *Mycnf) error {
fname := filepath.Join(cnf.TabletDir(), RestoreState)
if err := os.Remove(fname); err != nil {
return fmt.Errorf("unable to delete file: %v", err)
}
return nil
}
// RestoreWasInterrupted tells us whether a previous restore
// was interrupted and we are now retrying it
func RestoreWasInterrupted(cnf *Mycnf) bool {
name := filepath.Join(cnf.TabletDir(), RestoreState)
_, err := os.Stat(name)
return err == nil
}
// GetBackupDir returns the directory where backups for the
// given keyspace/shard are (or will be) stored
func GetBackupDir(keyspace, shard string) string {
return fmt.Sprintf("%v/%v", keyspace, shard)
}
// isDbDir returns true if the given directory contains a DB
func isDbDir(p string) bool {
// db.opt is there
if _, err := os.Stat(path.Join(p, "db.opt")); err == nil {
return true
}
// Look for at least one database file
fis, err := os.ReadDir(p)
if err != nil {
return false
}
for _, fi := range fis {
if strings.HasSuffix(fi.Name(), ".frm") {
return true
}
// the MyRocks engine stores data in RocksDB .sst files
// https://github.com/facebook/rocksdb/wiki/Rocksdb-BlockBasedTable-Format
if strings.HasSuffix(fi.Name(), ".sst") {
return true
}
// .frm files were removed in MySQL 8, so we need to check for two other file types
// https://dev.mysql.com/doc/refman/8.0/en/data-dictionary-file-removal.html
if strings.HasSuffix(fi.Name(), ".ibd") {
return true
}
// https://dev.mysql.com/doc/refman/8.0/en/serialized-dictionary-information.html
if strings.HasSuffix(fi.Name(), ".sdi") {
return true
}
}
return false
}
func addDirectory(fes []FileEntry, base string, baseDir string, subDir string) ([]FileEntry, int64, error) {
p := path.Join(baseDir, subDir)
var size int64
entries, err := os.ReadDir(p)
if err != nil {
return nil, 0, err
}
for _, entry := range entries {
fi, err := entry.Info()
if err != nil {
return nil, 0, err
}
fes = append(fes, FileEntry{
Base: base,
Name: path.Join(subDir, fi.Name()),
})
size = size + fi.Size()
}
return fes, size, nil
}
// addMySQL8DataDictionary checks to see if the new data dictionary introduced in MySQL 8 exists
// and adds it to the backup manifest if it does
// https://dev.mysql.com/doc/refman/8.0/en/data-dictionary-transactional-storage.html
func addMySQL8DataDictionary(fes []FileEntry, base string, baseDir string) ([]FileEntry, int64, error) {
filePath := path.Join(baseDir, dataDictionaryFile)
// no-op if this file doesn't exist
fi, err := os.Stat(filePath)
if os.IsNotExist(err) {
return fes, 0, nil
}
fes = append(fes, FileEntry{
Base: base,
Name: dataDictionaryFile,
})
return fes, fi.Size(), nil
}
func findFilesToBackup(cnf *Mycnf) ([]FileEntry, int64, error) {
var err error
var result []FileEntry
var totalSize int64
// first add inno db files
result, totalSize, err = addDirectory(result, backupInnodbDataHomeDir, cnf.InnodbDataHomeDir, "")
if err != nil {
return nil, 0, err
}
result, size, err := addDirectory(result, backupInnodbLogGroupHomeDir, cnf.InnodbLogGroupHomeDir, "")
if err != nil {
return nil, 0, err
}
totalSize = totalSize + size
// then add the transactional data dictionary if it exists
result, size, err = addMySQL8DataDictionary(result, backupData, cnf.DataDir)
if err != nil {
return nil, 0, err
}
totalSize = totalSize + size
// then add DB directories
fis, err := os.ReadDir(cnf.DataDir)
if err != nil {
return nil, 0, err
}
for _, fi := range fis {
p := path.Join(cnf.DataDir, fi.Name())
if isDbDir(p) {
result, size, err = addDirectory(result, backupData, cnf.DataDir, fi.Name())
if err != nil {
return nil, 0, err
}
totalSize = totalSize + size
}
}
return result, totalSize, nil
}