Skip to content

Commit 39976f7

Browse files
authored
Refactor StartBackup to Support Custom Backup Handlers (#849)
* Refactor StartBackup to Support Custom Backup Handlers * fix: TestStartBackupEndInGivenTime test failure * Refactor StartBackupToFile to create backup directory before initiating backup * fix: Add error handling for w.Close() * style:Rename filename to binlogFilename for clarity in StartBackup * docs: Simplify the comments of the StartBackup method * refactor: Rename the StartBackup method to StartBackupWithHandler so that the StartBackup method retains its original behavior
1 parent a057d23 commit 39976f7

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

replication/backup.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ import (
1414
// StartBackup: Like mysqlbinlog remote raw backup
1515
// Backup remote binlog from position (filename, offset) and write in backupDir
1616
func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Duration) error {
17+
err := os.MkdirAll(backupDir, 0755)
18+
if err != nil {
19+
return errors.Trace(err)
20+
}
21+
return b.StartBackupWithHandler(p, timeout, func(filename string) (io.WriteCloser, error) {
22+
return os.OpenFile(path.Join(backupDir, filename), os.O_CREATE|os.O_WRONLY, 0644)
23+
})
24+
}
25+
26+
// StartBackupWithHandler starts the backup process for the binary log using the specified position and handler.
27+
// The process will continue until the timeout is reached or an error occurs.
28+
//
29+
// Parameters:
30+
// - p: The starting position in the binlog from which to begin the backup.
31+
// - timeout: The maximum duration to wait for new binlog events before stopping the backup process.
32+
// If set to 0, a default very long timeout (30 days) is used instead.
33+
// - handler: A function that takes a binlog filename and returns an WriteCloser for writing raw events to.
34+
func (b *BinlogSyncer) StartBackupWithHandler(p Position, timeout time.Duration,
35+
handler func(binlogFilename string) (io.WriteCloser, error)) (retErr error) {
1736
if timeout == 0 {
1837
// a very long timeout here
1938
timeout = 30 * 3600 * 24 * time.Second
@@ -22,10 +41,6 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
2241
// Force use raw mode
2342
b.parser.SetRawMode(true)
2443

25-
if err := os.MkdirAll(backupDir, 0755); err != nil {
26-
return errors.Trace(err)
27-
}
28-
2944
s, err := b.StartSync(p)
3045
if err != nil {
3146
return errors.Trace(err)
@@ -34,10 +49,14 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
3449
var filename string
3550
var offset uint32
3651

37-
var f *os.File
52+
var w io.WriteCloser
3853
defer func() {
39-
if f != nil {
40-
f.Close()
54+
var closeErr error
55+
if w != nil {
56+
closeErr = w.Close()
57+
}
58+
if retErr == nil {
59+
retErr = closeErr
4160
}
4261
}()
4362

@@ -67,26 +86,29 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
6786
} else if e.Header.EventType == FORMAT_DESCRIPTION_EVENT {
6887
// FormateDescriptionEvent is the first event in binlog, we will close old one and create a new
6988

70-
if f != nil {
71-
f.Close()
89+
if w != nil {
90+
if err = w.Close(); err != nil {
91+
w = nil
92+
return errors.Trace(err)
93+
}
7294
}
7395

7496
if len(filename) == 0 {
7597
return errors.Errorf("empty binlog filename for FormateDescriptionEvent")
7698
}
7799

78-
f, err = os.OpenFile(path.Join(backupDir, filename), os.O_CREATE|os.O_WRONLY, 0644)
100+
w, err = handler(filename)
79101
if err != nil {
80102
return errors.Trace(err)
81103
}
82104

83105
// write binlog header fe'bin'
84-
if _, err = f.Write(BinLogFileHeader); err != nil {
106+
if _, err = w.Write(BinLogFileHeader); err != nil {
85107
return errors.Trace(err)
86108
}
87109
}
88110

89-
if n, err := f.Write(e.RawData); err != nil {
111+
if n, err := w.Write(e.RawData); err != nil {
90112
return errors.Trace(err)
91113
} else if n != len(e.RawData) {
92114
return errors.Trace(io.ErrShortWrite)

0 commit comments

Comments
 (0)