Skip to content

Commit

Permalink
PBM-1058: fix phys backups on datadirs with tailing / (#802)
Browse files Browse the repository at this point in the history
Phys backup while trimming datadir from the file path always added
a slash to the trim prefix. In case datadir was set with a tailing
slash it led to a double slash and datadir wasn't trimmed at all
(`Trimprefix("/data/db/file1", "/data/db//")`). Therefore in the
backup's meta such files contain datadir and the restore would put them
in a subdirectory inside datadir, "/data/db/data/db/file1" instead of
"/data/db/file1".

This change fixes it.
  • Loading branch information
dAdAbird committed Mar 16, 2023
1 parent db10bb7 commit 173db8f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pbm/backup/physical.go
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"path"
"strings"
"time"

Expand Down Expand Up @@ -276,15 +277,15 @@ func (b *Backup) doPhysical(ctx context.Context, bcp *pbm.BackupCmd, opid pbm.OP
}

l.Info("uploading data")
rsMeta.Files, err = uploadFiles(ctx, bcur.Data, bcp.Name+"/"+rsMeta.Name, bcur.Meta.DBpath+"/",
rsMeta.Files, err = uploadFiles(ctx, bcur.Data, bcp.Name+"/"+rsMeta.Name, bcur.Meta.DBpath,
b.typ == pbm.IncrementalBackup, stg, bcp.Compression, bcp.CompressionLevel, l)
if err != nil {
return err
}
l.Info("uploading data done")

l.Info("uploading journals")
ju, err := uploadFiles(ctx, jrnls, bcp.Name+"/"+rsMeta.Name, bcur.Meta.DBpath+"/",
ju, err := uploadFiles(ctx, jrnls, bcp.Name+"/"+rsMeta.Name, bcur.Meta.DBpath,
false, stg, bcp.Compression, bcp.CompressionLevel, l)
if err != nil {
return err
Expand Down Expand Up @@ -339,8 +340,6 @@ func (id *UUID) IsZero() bool {
return bytes.Equal(id.UUID[:], uuid.Nil[:])
}

const journalPrefix = "journal/WiredTigerLog."

// Uploads given files to the storage. files may come as 16Mb (by default)
// blocks in that case it will concat consecutive blocks in one bigger file.
// For example: f1[0-16], f1[16-24], f1[64-16] becomes f1[0-24], f1[50-16].
Expand All @@ -353,6 +352,12 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
return data, err
}

trim := func(fname string) string {
// path.Clean to get rid of `/` at the beginning in case it's
// left after TrimPrefix. Just for consistent file names in metadata
return path.Clean("./" + strings.TrimPrefix(fname, trimPrefix))
}

wfile := files[0]
for _, file := range files[1:] {
select {
Expand All @@ -369,7 +374,7 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
if incr && (file.Len == 0 || file.Off >= file.Size) {
file.Off = -1
file.Len = -1
file.Name = strings.TrimPrefix(file.Name, trimPrefix)
file.Name = trim(file.Name)

data = append(data, file)
continue
Expand All @@ -382,11 +387,11 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
continue
}

fw, err := writeFile(ctx, wfile, subdir+"/"+strings.TrimPrefix(wfile.Name, trimPrefix), stg, comprT, comprL, l)
fw, err := writeFile(ctx, wfile, path.Join(subdir, trim(wfile.Name)), stg, comprT, comprL, l)
if err != nil {
return data, errors.Wrapf(err, "upload file `%s`", wfile.Name)
}
fw.Name = strings.TrimPrefix(wfile.Name, trimPrefix)
fw.Name = trim(wfile.Name)

data = append(data, *fw)

Expand All @@ -397,11 +402,11 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
return data, nil
}

f, err := writeFile(ctx, wfile, subdir+"/"+strings.TrimPrefix(wfile.Name, trimPrefix), stg, comprT, comprL, l)
f, err := writeFile(ctx, wfile, path.Join(subdir, trim(wfile.Name)), stg, comprT, comprL, l)
if err != nil {
return data, errors.Wrapf(err, "upload file `%s`", wfile.Name)
}
f.Name = strings.TrimPrefix(wfile.Name, trimPrefix)
f.Name = trim(wfile.Name)

data = append(data, *f)

Expand Down

0 comments on commit 173db8f

Please sign in to comment.