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 ac709c8 commit 5b2919a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pbm/backup/physical.go
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"path"
"strings"
"time"

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

l.Info("uploading data")
rsMeta.Journal, rsMeta.Files, err = uploadFiles(ctx, bcur.Data, bcp.Name+"/"+rsMeta.Name, bcur.Meta.DBpath+"/",
rsMeta.Journal, 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 @@ -340,6 +341,12 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
return journal, data, err
}

trim := func(fname string) string {
// path.Clean to get rid of the lading `/` 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 @@ -360,11 +367,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 journal, data, errors.Wrapf(err, "upload file `%s`", wfile.Name)
}
fw.Name = strings.TrimPrefix(wfile.Name, trimPrefix)
fw.Name = trim(wfile.Name)

if strings.HasPrefix(fw.Name, journalPrefix) {
journal = append(journal, *fw)
Expand All @@ -379,16 +386,11 @@ func uploadFiles(ctx context.Context, files []pbm.File, subdir, trimPrefix strin
return journal, 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 journal, data, errors.Wrapf(err, "upload file `%s`", wfile.Name)
}
f.Name = strings.TrimPrefix(wfile.Name, trimPrefix)
if strings.HasPrefix(f.Name, journalPrefix) {
journal = append(journal, *f)
} else {
data = append(data, *f)
}
f.Name = trim(wfile.Name)

return journal, data, nil
}
Expand Down

0 comments on commit 5b2919a

Please sign in to comment.