Skip to content

Commit

Permalink
feat(region,host): ignore not exist files when disk backup (#21181)
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi authored Sep 7, 2024
1 parent 1537276 commit d244ebf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 4 additions & 3 deletions pkg/apis/compute/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ type DiskBackupDetails struct {
}

type DiskBackupAsTarInput struct {
IncludeFiles []string `json:"include_files"`
ExcludeFiles []string `json:"exclude_files"`
ContainerId string `json:"container_id"`
IncludeFiles []string `json:"include_files"`
ExcludeFiles []string `json:"exclude_files"`
ContainerId string `json:"container_id"`
IgnoreNotExistFile bool `json:"ignore_not_exist_file"`
}

type DiskBackupCreateInput struct {
Expand Down
19 changes: 17 additions & 2 deletions pkg/hostman/guestman/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1902,14 +1902,29 @@ func (s *sPodGuestInstance) createSnapshot(params *SDiskSnapshot, hostPath strin
snapshotPath := s.getSnapshotPath(d, params.SnapshotId)
// tar hostPath to snapshotPath
input := params.BackupDiskConfig.BackupAsTar
if err := s.tarHostDir(hostPath, snapshotPath, input.IncludeFiles, input.ExcludeFiles); err != nil {
if err := s.tarHostDir(hostPath, snapshotPath, input.IncludeFiles, input.ExcludeFiles, input.IgnoreNotExistFile); err != nil {
return "", errors.Wrapf(err, "tar host dir %s to %s", hostPath, snapshotPath)
}
return snapshotPath, nil
}

func (s *sPodGuestInstance) tarHostDir(srcDir, targetPath string, includeFiles, excludeFiles []string) error {
func (s *sPodGuestInstance) tarHostDir(srcDir, targetPath string,
includeFiles, excludeFiles []string,
ignoreNotExistFile bool) error {
baseCmd := "tar"
filterNotExistFiles := func(files []string) []string {
result := []string{}
for i := range files {
if fileutils2.Exists(filepath.Join(srcDir, files[i])) {
result = append(result, files[i])
}
}
return result
}
if ignoreNotExistFile {
includeFiles = filterNotExistFiles(includeFiles)
excludeFiles = filterNotExistFiles(excludeFiles)
}
for _, exclude := range excludeFiles {
baseCmd = fmt.Sprintf("%s --exclude='%s'", baseCmd, exclude)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/mcclient/options/compute/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func (opts *DiskBackupDeleteOptions) QueryParams() (jsonutils.JSONObject, error)

type DiskBackupCreateOptions struct {
options.BaseCreateOptions
AsTarContainerId string `help:"container id of tar process"`
AsTarIncludeFile []string `help:"include file path of tar process"`
AsTarExcludeFile []string `help:"exclude file path of tar process"`
AsTarContainerId string `help:"container id of tar process"`
AsTarIncludeFile []string `help:"include file path of tar process"`
AsTarExcludeFile []string `help:"exclude file path of tar process"`
AsTarIgnoreNotExistFile bool `help:"ignore not exist file when using tar"`

DISKID string `help:"disk id" json:"disk_id"`
BACKUPSTORAGEID string `help:"back storage id" json:"backup_storage_id"`
Expand All @@ -81,6 +82,9 @@ func (opts *DiskBackupCreateOptions) Params() (jsonutils.JSONObject, error) {
if len(opts.AsTarExcludeFile) > 0 {
input.BackupAsTar.ExcludeFiles = opts.AsTarExcludeFile
}
if opts.AsTarIgnoreNotExistFile {
input.BackupAsTar.IgnoreNotExistFile = opts.AsTarIgnoreNotExistFile
}
return jsonutils.Marshal(input), nil
}

Expand Down

0 comments on commit d244ebf

Please sign in to comment.