feat(region,host): ignore not exist files when disk backup (#21181)

This commit is contained in:
Zexi Li
2024-09-07 22:44:04 +08:00
committed by GitHub
parent 1537276992
commit d244ebf3da
3 changed files with 28 additions and 8 deletions
+4 -3
View File
@@ -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 {
+17 -2
View File
@@ -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)
}
+7 -3
View File
@@ -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
}