Skip to content

Commit

Permalink
Merge pull request #565 from cybozu-go/fix-563
Browse files Browse the repository at this point in the history
backup: PITR did not work sometimes
  • Loading branch information
masa213f authored Sep 8, 2023
2 parents 00cce35 + b0de37d commit bdd73cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 13 additions & 6 deletions backup/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (rm *RestoreManager) Restore(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to list object keys: %w", err)
}
sort.Strings(keys)

dumpKey, binlogKey, backupTime := rm.FindNearestDump(keys)
if dumpKey == "" {
Expand Down Expand Up @@ -195,16 +194,19 @@ func (rm *RestoreManager) Restore(ctx context.Context) error {
return nil
}

// FindNearestDump finds the nearest dump file and binlog file to the restore point.
// `keys` are object keys for the restoring instance. They need not be sorted.
func (rm *RestoreManager) FindNearestDump(keys []string) (string, string, time.Time) {
sort.Strings(keys)

var nearest time.Time
var nearestDump, nearestBinlog string

for _, key := range keys {
if strings.HasSuffix(key, constants.BinlogFilename) {
nearestBinlog = key
continue
}
if !strings.HasSuffix(key, constants.DumpFilename) {
isBinlog := strings.HasSuffix(key, constants.BinlogFilename)
isDump := strings.HasSuffix(key, constants.DumpFilename)
if !isBinlog && !isDump {
rm.log.Info("skipping garbage", "key", key)
continue
}

Expand All @@ -217,6 +219,11 @@ func (rm *RestoreManager) FindNearestDump(keys []string) (string, string, time.T
break
}

if isBinlog {
nearestBinlog = key
continue
}

nearestDump = key
nearest = bkt
if path.Dir(nearestDump) != path.Dir(nearestBinlog) {
Expand Down
10 changes: 9 additions & 1 deletion backup/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func TestFindNearestDump(t *testing.T) {
"moco/test/test/garbage",
"moco/test/test/20210526000000/dump.tar", // invalid
"moco/test/test/20210526-000000/dump.tar",
"moco/test/test/20210527-000000/dump.tar",
"moco/test/test/20210527-000000/binlog.tar.zst",
"moco/test/test/20210528-000000/dump.tar",
"moco/test/test/20210528-000000/binlog.tar.zst",
}

testCases := []struct {
Expand All @@ -25,7 +29,7 @@ func TestFindNearestDump(t *testing.T) {
expectBinlog string
expectTime time.Time
}{
{"latest", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC),
{"exact", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC),
"moco/test/test/20210526-000000/dump.tar", "", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC)},
{"up-to-date", time.Date(2021, time.May, 26, 1, 0, 0, 0, time.UTC),
"moco/test/test/20210526-000000/dump.tar", "", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC)},
Expand All @@ -35,6 +39,10 @@ func TestFindNearestDump(t *testing.T) {
"moco/test/test/20210525-112233/dump.tar", "moco/test/test/20210525-112233/binlog.tar.zst",
time.Date(2021, time.May, 25, 11, 22, 33, 0, time.UTC)},
{"not-found", time.Date(2021, time.May, 24, 0, 0, 0, 0, time.UTC), "", "", time.Time{}},
{"#563", time.Date(2021, time.May, 27, 12, 0, 0, 0, time.UTC),
"moco/test/test/20210527-000000/dump.tar", "moco/test/test/20210527-000000/binlog.tar.zst",
time.Date(2021, time.May, 27, 0, 0, 0, 0, time.UTC),
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit bdd73cc

Please sign in to comment.