Skip to content

Commit 0bcab61

Browse files
authored
Merge pull request #1683 from stgraber/backup
Fix importing from older backups
2 parents cd19568 + b6e542f commit 0bcab61

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

cmd/incusd/instances_post.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,16 @@ func createFromBackup(s *state.State, r *http.Request, projectName string, data
694694
return response.InternalError(err)
695695
}
696696

697-
logger.Debug("Reading backup file info")
698697
bInfo, err := backup.GetInfo(backupFile, s.OS, backupFile.Name())
699698
if err != nil {
700699
return response.BadRequest(err)
701700
}
702701

702+
// Detect broken legacy backups.
703+
if bInfo.Config == nil {
704+
return response.BadRequest(fmt.Errorf("Backup file is missing required information"))
705+
}
706+
703707
// Check project permissions.
704708
var req api.InstancesPost
705709
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {

internal/server/backup/backup_info.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Info struct {
6060
func GetInfo(r io.ReadSeeker, sysOS *sys.OS, outputPath string) (*Info, error) {
6161
result := Info{}
6262
hasIndexFile := false
63+
hasOptimizedHeader := false
6364

6465
// Define some bools used to create points for OptimizedStorage field.
6566
optimizedStorageFalse := false
@@ -102,16 +103,22 @@ func GetInfo(r io.ReadSeeker, sysOS *sys.OS, outputPath string) (*Info, error) {
102103
}
103104

104105
if result.OptimizedStorage != nil {
105-
// No need to continue looking for optimized storage hint using the presence of the
106-
// container.bin file below, as the index.yaml file tells us directly.
107-
break
106+
hasOptimizedHeader = true
108107
} else {
109108
// Default to non-optimized if not specified and continue reading to see if
110109
// optimized container.bin file present.
111110
result.OptimizedStorage = &optimizedStorageFalse
112111
}
113112
}
114113

114+
// Load old backup data.
115+
if result.Config == nil && hdr.Name == "backup/container/backup.yaml" {
116+
err = yaml.NewDecoder(tr).Decode(&result.Config)
117+
if err != nil {
118+
return nil, err
119+
}
120+
}
121+
115122
// If the tarball contains a binary dump of the container, then this is an optimized backup.
116123
// This check is only for legacy backups before we introduced the Type and OptimizedStorage fields
117124
// in index.yaml, so there is no need to perform this type of check for other types of backups that
@@ -120,10 +127,12 @@ func GetInfo(r io.ReadSeeker, sysOS *sys.OS, outputPath string) (*Info, error) {
120127
optimizedStorageTrue := true
121128
result.OptimizedStorage = &optimizedStorageTrue
122129

123-
// Stop read loop if index.yaml already parsed.
124-
if hasIndexFile {
125-
break
126-
}
130+
hasOptimizedHeader = true
131+
}
132+
133+
// Check if we're done processing what we need.
134+
if hasIndexFile && hasOptimizedHeader && result.Config != nil {
135+
break
127136
}
128137
}
129138

0 commit comments

Comments
 (0)