Skip to content

Commit

Permalink
Skip MySQL binlog start time (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Mehedi Hasan <[email protected]>
  • Loading branch information
heheh13 authored Dec 21, 2023
1 parent d322f49 commit a9c0459
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/mysql/binlog_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
)

const fetchSinceFlagShortDescr = "backup name starting from which you want to fetch binlogs"
const skipStartTimeFlagShortDescr = "skip start time starting from begin to fetch binlogs"
const fetchUntilFlagShortDescr = "time in RFC3339 for PITR"
const fetchUntilBinlogLastModifiedFlagShortDescr = "time in RFC3339 that is used to prevent wal-g from replaying" +
" binlogs that was created/modified after this time"

var fetchBackupName string
var fetchUntilTS string
var fetchUntilBinlogLastModifiedTS string
var skipStartTime bool

// binlogPushCmd represents the cron command
var binlogFetchCmd = &cobra.Command{
Expand All @@ -27,7 +29,7 @@ var binlogFetchCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
folder, err := internal.ConfigureFolder()
tracelog.ErrorLogger.FatalOnError(err)
mysql.HandleBinlogFetch(folder, fetchBackupName, fetchUntilTS, fetchUntilBinlogLastModifiedTS)
mysql.HandleBinlogFetch(folder, fetchBackupName, fetchUntilTS, fetchUntilBinlogLastModifiedTS, skipStartTime)
},
PreRun: func(cmd *cobra.Command, args []string) {
internal.RequiredSettings[internal.MysqlBinlogDstSetting] = true
Expand All @@ -46,5 +48,6 @@ func init() {
"until-binlog-last-modified-time",
"",
fetchUntilBinlogLastModifiedFlagShortDescr)
binlogFetchCmd.PersistentFlags().BoolVar(&skipStartTime, "skip-start-time", false, skipStartTimeFlagShortDescr)
cmd.AddCommand(binlogFetchCmd)
}
13 changes: 9 additions & 4 deletions internal/databases/mysql/binlog_fetch_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal"
Expand Down Expand Up @@ -41,12 +42,16 @@ func (ih *indexHandler) createIndexFile() error {
return nil
}

func HandleBinlogFetch(folder storage.Folder, backupName string, untilTS string, untilBinlogLastModifiedTS string) {
func HandleBinlogFetch(folder storage.Folder, backupName string, untilTS string, untilBinlogLastModifiedTS string, skipStartTime bool) {
dstDir, err := internal.GetLogsDstSettings(internal.MysqlBinlogDstSetting)
tracelog.ErrorLogger.FatalOnError(err)

startTS, endTS, endBinlogTS, err := getTimestamps(folder, backupName, untilTS, untilBinlogLastModifiedTS)
tracelog.ErrorLogger.FatalOnError(err)
var startTS, endTS, endBinlogTS time.Time
if skipStartTime {
startTS, endTS, endBinlogTS, err = getEndTimestamps(folder, untilTS, untilBinlogLastModifiedTS)
} else {
startTS, endTS, endBinlogTS, err = getTimestamps(folder, backupName, untilTS, untilBinlogLastModifiedTS)
tracelog.ErrorLogger.FatalOnError(err)
}

handler := newIndexHandler(dstDir)

Expand Down
13 changes: 13 additions & 0 deletions internal/databases/mysql/binlog_replay_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,16 @@ func getTimestamps(folder storage.Folder, backupName, untilTS, untilBinlogLastMo
}
return startTS, endTS, endBinlogTS, nil
}

func getEndTimestamps(folder storage.Folder, untilTS, untilBinlogLastModifiedTS string) (time.Time, time.Time, time.Time, error) {
endTS, err := utility.ParseUntilTS(untilTS)
if err != nil {
return time.Time{}, time.Time{}, time.Time{}, err
}

endBinlogTS, err := utility.ParseUntilTS(untilBinlogLastModifiedTS)
if err != nil {
return time.Time{}, time.Time{}, time.Time{}, err
}
return startTS, endTS, endBinlogTS, nil
}

0 comments on commit a9c0459

Please sign in to comment.