forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.go
193 lines (167 loc) · 4.85 KB
/
backup.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
package replication
import (
"context"
"io"
"os"
"path"
"sync"
"time"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/errors"
)
// StartBackup starts the backup process for the binary log and writes to the backup directory.
func (b *BinlogSyncer) StartBackup(backupDir string, p mysql.Position, timeout time.Duration) error {
err := os.MkdirAll(backupDir, 0o755)
if err != nil {
return errors.Trace(err)
}
if b.cfg.SynchronousEventHandler == nil {
return b.StartBackupWithHandler(p, timeout, func(filename string) (io.WriteCloser, error) {
return os.OpenFile(path.Join(backupDir, filename), os.O_CREATE|os.O_WRONLY, 0o644)
})
} else {
return b.StartSynchronousBackup(p, timeout)
}
}
// StartBackupWithHandler starts the backup process for the binary log using the specified position and handler.
// The process will continue until the timeout is reached or an error occurs.
// This method should not be used together with SynchronousEventHandler.
//
// Parameters:
// - p: The starting position in the binlog from which to begin the backup.
// - timeout: The maximum duration to wait for new binlog events before stopping the backup process.
// If set to 0, a default very long timeout (30 days) is used instead.
// - handler: A function that takes a binlog filename and returns an WriteCloser for writing raw events to.
func (b *BinlogSyncer) StartBackupWithHandler(p mysql.Position, timeout time.Duration,
handler func(binlogFilename string) (io.WriteCloser, error),
) (retErr error) {
if timeout == 0 {
// a very long timeout here
timeout = 30 * 3600 * 24 * time.Second
}
if b.cfg.SynchronousEventHandler != nil {
return errors.New("StartBackupWithHandler cannot be used when SynchronousEventHandler is set. Use StartSynchronousBackup instead.")
}
// Force use raw mode
b.parser.SetRawMode(true)
// Set up the backup event handler
backupHandler := &BackupEventHandler{
handler: handler,
}
s, err := b.StartSync(p)
if err != nil {
return errors.Trace(err)
}
defer func() {
if backupHandler.w != nil {
closeErr := backupHandler.w.Close()
if retErr == nil {
retErr = closeErr
}
}
}()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
select {
case <-ctx.Done():
return nil
case <-b.ctx.Done():
return nil
case err := <-s.ech:
return errors.Trace(err)
case e := <-s.ch:
err = backupHandler.HandleEvent(e)
if err != nil {
return errors.Trace(err)
}
}
}
}
// StartSynchronousBackup starts the backup process using the SynchronousEventHandler in the BinlogSyncerConfig.
func (b *BinlogSyncer) StartSynchronousBackup(p mysql.Position, timeout time.Duration) error {
if b.cfg.SynchronousEventHandler == nil {
return errors.New("SynchronousEventHandler must be set in BinlogSyncerConfig to use StartSynchronousBackup")
}
s, err := b.StartSync(p)
if err != nil {
return errors.Trace(err)
}
var ctx context.Context
var cancel context.CancelFunc
if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
} else {
ctx = context.Background()
}
select {
case <-ctx.Done():
// The timeout has been reached
return nil
case <-b.ctx.Done():
// The BinlogSyncer has been closed
return nil
case err := <-s.ech:
// An error occurred during streaming
return errors.Trace(err)
}
}
// BackupEventHandler handles writing events for backup
type BackupEventHandler struct {
handler func(binlogFilename string) (io.WriteCloser, error)
w io.WriteCloser
mutex sync.Mutex
filename string
}
func NewBackupEventHandler(handlerFunction func(filename string) (io.WriteCloser, error)) *BackupEventHandler {
return &BackupEventHandler{
handler: handlerFunction,
}
}
// HandleEvent processes a single event for the backup.
func (h *BackupEventHandler) HandleEvent(e *BinlogEvent) error {
h.mutex.Lock()
defer h.mutex.Unlock()
var err error
offset := e.Header.LogPos
if e.Header.EventType == ROTATE_EVENT {
rotateEvent := e.Event.(*RotateEvent)
h.filename = string(rotateEvent.NextLogName)
if e.Header.Timestamp == 0 || offset == 0 {
// fake rotate event
return nil
}
} else if e.Header.EventType == FORMAT_DESCRIPTION_EVENT {
if h.w != nil {
if err = h.w.Close(); err != nil {
h.w = nil
return errors.Trace(err)
}
}
if len(h.filename) == 0 {
return errors.Errorf("empty binlog filename for FormatDescriptionEvent")
}
h.w, err = h.handler(h.filename)
if err != nil {
return errors.Trace(err)
}
// Write binlog header 0xfebin
_, err = h.w.Write(BinLogFileHeader)
if err != nil {
return errors.Trace(err)
}
}
if h.w != nil {
n, err := h.w.Write(e.RawData)
if err != nil {
return errors.Trace(err)
}
if n != len(e.RawData) {
return errors.Trace(io.ErrShortWrite)
}
} else {
return errors.New("writer is not initialized")
}
return nil
}