From d244ebf3da49588bd44a9f444df34a965e77e25f Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Sat, 7 Sep 2024 22:44:04 +0800 Subject: [PATCH] feat(region,host): ignore not exist files when disk backup (#21181) --- pkg/apis/compute/backup.go | 7 ++++--- pkg/hostman/guestman/pod.go | 19 +++++++++++++++++-- pkg/mcclient/options/compute/backup.go | 10 +++++++--- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/pkg/apis/compute/backup.go b/pkg/apis/compute/backup.go index 624fa269b6..e67ecfdbaf 100644 --- a/pkg/apis/compute/backup.go +++ b/pkg/apis/compute/backup.go @@ -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 { diff --git a/pkg/hostman/guestman/pod.go b/pkg/hostman/guestman/pod.go index 9ea661080d..bdb7d25b26 100644 --- a/pkg/hostman/guestman/pod.go +++ b/pkg/hostman/guestman/pod.go @@ -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) } diff --git a/pkg/mcclient/options/compute/backup.go b/pkg/mcclient/options/compute/backup.go index bbabc0bfcf..312874304e 100644 --- a/pkg/mcclient/options/compute/backup.go +++ b/pkg/mcclient/options/compute/backup.go @@ -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"` @@ -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 }