From 173db8fad402815d4db6df34bdff0b2d0a289bc3 Mon Sep 17 00:00:00 2001 From: Andrew Pogrebnoi Date: Thu, 16 Mar 2023 14:27:39 +0200 Subject: [PATCH] PBM-1058: fix phys backups on datadirs with tailing / (#802) 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. --- pbm/backup/physical.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pbm/backup/physical.go b/pbm/backup/physical.go index 758d56372..25b9741f2 100644 --- a/pbm/backup/physical.go +++ b/pbm/backup/physical.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os" + "path" "strings" "time" @@ -276,7 +277,7 @@ 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 @@ -284,7 +285,7 @@ func (b *Backup) doPhysical(ctx context.Context, bcp *pbm.BackupCmd, opid pbm.OP 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 @@ -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]. @@ -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 { @@ -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 @@ -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) @@ -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)