|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package backupdest |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/backup/backupbase" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/backup/backuppb" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/backup/backuputils" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/cloud" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/security/username" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/protoutil" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/util/tracing" |
| 23 | + "github.com/cockroachdb/errors" |
| 24 | +) |
| 25 | + |
| 26 | +// WriteBackupIndexMetadata writes an index file for the backup described by the |
| 27 | +// job details. The provided ExternalStorage needs to be rooted at the specific |
| 28 | +// directory that the index file should be written to. |
| 29 | +// |
| 30 | +// Note: This file is not encrypted, so it should not contain any sensitive |
| 31 | +// information. |
| 32 | +func WriteBackupIndexMetadata( |
| 33 | + ctx context.Context, |
| 34 | + user username.SQLUsername, |
| 35 | + makeExternalStorageFromURI cloud.ExternalStorageFromURIFactory, |
| 36 | + details jobspb.BackupDetails, |
| 37 | +) error { |
| 38 | + ctx, sp := tracing.ChildSpan(ctx, "backupdest.WriteBackupIndexMetadata") |
| 39 | + defer sp.Finish() |
| 40 | + |
| 41 | + if details.EndTime.IsEmpty() { |
| 42 | + return errors.AssertionFailedf("end time must be set in backup details") |
| 43 | + } |
| 44 | + if details.Destination.Exists && details.StartTime.IsEmpty() { |
| 45 | + return errors.AssertionFailedf("incremental backup details missing a start time") |
| 46 | + } |
| 47 | + |
| 48 | + var backupCollectionURI string |
| 49 | + // Find the root of the collection URI that the backup is being written to so |
| 50 | + // that we can determine the relative path of the backup. |
| 51 | + if details.StartTime.IsEmpty() { |
| 52 | + backupCollectionURI = details.CollectionURI |
| 53 | + } else { |
| 54 | + var err error |
| 55 | + backupCollectionURI, err = ResolveDefaultBaseIncrementalStorageLocation( |
| 56 | + details.Destination.To, details.Destination.IncrementalStorage, |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + return errors.Wrapf(err, "get incremental backup collection URI") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + path, err := backuputils.RelativeBackupPathInCollectionURI(backupCollectionURI, details.URI) |
| 64 | + if err != nil { |
| 65 | + return errors.Wrapf(err, "get relative backup path") |
| 66 | + } |
| 67 | + metadata := &backuppb.BackupIndexMetadata{ |
| 68 | + StartTime: details.StartTime, |
| 69 | + EndTime: details.EndTime, |
| 70 | + Path: path, |
| 71 | + } |
| 72 | + metadataBytes, err := protoutil.Marshal(metadata) |
| 73 | + if err != nil { |
| 74 | + return errors.Wrapf(err, "marshal backup index metadata") |
| 75 | + } |
| 76 | + |
| 77 | + indexStore, err := makeExternalStorageFromURI( |
| 78 | + ctx, details.CollectionURI, user, |
| 79 | + ) |
| 80 | + if err != nil { |
| 81 | + return errors.Wrapf(err, "creating external storage") |
| 82 | + } |
| 83 | + |
| 84 | + indexFilePath, err := getBackupIndexFilePath( |
| 85 | + details.Destination.Subdir, |
| 86 | + details.StartTime, |
| 87 | + details.EndTime, |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return errors.Wrapf(err, "getting index file path") |
| 91 | + } |
| 92 | + |
| 93 | + return cloud.WriteFile( |
| 94 | + ctx, indexStore, indexFilePath, bytes.NewReader(metadataBytes), |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +// getBackupIndexFilePath returns the path to the backup index file representing |
| 99 | +// a backup that starts and ends at the given timestamps, including |
| 100 | +// the filename and extension. The path is relative to the collection URI. |
| 101 | +func getBackupIndexFilePath(subdir string, startTime, endTime hlc.Timestamp) (string, error) { |
| 102 | + if strings.EqualFold(subdir, backupbase.LatestFileName) { |
| 103 | + return "", errors.AssertionFailedf("expected subdir to be resolved and not be 'LATEST'") |
| 104 | + } |
| 105 | + // We flatten the subdir so that when listing from the index, we can list with |
| 106 | + // the `index/` prefix and delimit on `/`. |
| 107 | + flattenedSubdir := strings.ReplaceAll( |
| 108 | + strings.TrimPrefix(subdir, "/"), |
| 109 | + "/", "-", |
| 110 | + ) |
| 111 | + return backuputils.JoinURLPath( |
| 112 | + backupbase.BackupIndexDirectoryPath, |
| 113 | + flattenedSubdir, |
| 114 | + getBackupIndexFileName(startTime, endTime), |
| 115 | + ), nil |
| 116 | +} |
| 117 | + |
| 118 | +// getBackupIndexFilename generates the filename (including the extension) for a |
| 119 | +// backup index file that represents a backup that starts ad ends at the given |
| 120 | +// timestamps. |
| 121 | +func getBackupIndexFileName(startTime, endTime hlc.Timestamp) string { |
| 122 | + descEndTs := backuputils.EncodeDescendingTS(endTime.GoTime()) |
| 123 | + formattedStartTime := startTime.GoTime().Format(backupbase.BackupIndexFilenameTimestampFormat) |
| 124 | + if startTime.IsEmpty() { |
| 125 | + formattedStartTime = "0" // Use a placeholder for empty start time. |
| 126 | + } |
| 127 | + formattedEndTime := endTime.GoTime().Format(backupbase.BackupIndexFilenameTimestampFormat) |
| 128 | + return fmt.Sprintf( |
| 129 | + "%s_%s_%s_metadata.pb", |
| 130 | + descEndTs, formattedStartTime, formattedEndTime, |
| 131 | + ) |
| 132 | +} |
0 commit comments