Skip to content

Modern Docker Management, Designed for Everyone

fix: exclude gitops scratch clones from system backups (#4519)

Kyle Mendell46742508904c
diff --git a/backend/internal/system/children/backup/children/snapshots/service.go b/backend/internal/system/children/backup/children/snapshots/service.go
index 697caac22..1c4fd25b8 100644
--- a/backend/internal/system/children/backup/children/snapshots/service.go
+++ b/backend/internal/system/children/backup/children/snapshots/service.go
@@ -32,6 +32,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/backup"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/activity"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
@@ -63,6 +64,7 @@ type Service struct {
databaseFile func() (string, error)
projectsDirectory func(ctx context.Context) string
recoveryEnvironment func(ctx context.Context) map[string]string
+ gitWorkDirectory string
}
func NewService(
@@ -74,6 +76,7 @@ func NewService(
databaseFile func() (string, error),
projectsDirectory func(ctx context.Context) string,
recoveryEnvironment func(ctx context.Context) map[string]string,
+ gitWorkDirectory string,
) *Service {
return &Service{
engine: engine,
@@ -84,6 +87,7 @@ func NewService(
databaseFile: databaseFile,
projectsDirectory: projectsDirectory,
recoveryEnvironment: recoveryEnvironment,
+ gitWorkDirectory: gitWorkDirectory,
}
}
@@ -706,6 +710,16 @@ func (s *Service) backupSourceLayout(ctx context.Context, dockerClient *client.C
excludes = append(excludes, name+sidecar)
}
}
+ gitWorkDirectory, err := filepath.Abs(s.gitWorkDirectory)
+ if err != nil {
+ return backupSourceLayout{}, fmt.Errorf("failed to resolve the git work directory: %w", err)
+ }
+ if relative, relErr := filepath.Rel(dataDirectory, gitWorkDirectory); relErr == nil && relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
+ excludes = append(excludes, path.Join(
+ strings.NewReplacer(`\`, `\\`, `*`, `\*`, `?`, `\?`, `[`, `\[`, `]`, `\]`, `{`, `\{`, `}`, `\}`).Replace(filepath.ToSlash(relative)),
+ git.CloneScratchPrefix+"*",
+ ))
+ }
projectsPath, external, err := projectsSnapshotPath(dataDirectory, projectsDirectory)
if err != nil {
return backupSourceLayout{}, err
@@ -1178,7 +1192,7 @@ func (s *Service) Create(
break
}
current, exists := after[filePath]
- if !exists || !os.SameFile(original, current) || original.Size() != current.Size() || original.Mode() != current.Mode() || !original.ModTime().Equal(current.ModTime()) {
+ if !exists || !os.SameFile(original, current) || original.Mode() != current.Mode() || (!original.IsDir() && (original.Size() != current.Size() || !original.ModTime().Equal(current.ModTime()))) {
err = fmt.Errorf("system backup source %q changed during capture; retry when file updates finish", filePath)
}
}
diff --git a/backend/internal/system/children/backup/service.go b/backend/internal/system/children/backup/service.go
index 6570013d5..d0a57b5d6 100644
--- a/backend/internal/system/children/backup/service.go
+++ b/backend/internal/system/children/backup/service.go
@@ -203,6 +203,7 @@ func NewService(deps Dependencies) *Service {
return projects.ResolveConfiguredContainerDirectory(service.projectsSetting(ctx), "/app/data/projects")
},
func(ctx context.Context) map[string]string { return service.recoveryEnvironment(ctx) },
+ kit.FromPtr(deps.Config).GitWorkDir,
),
volumes: volumes.NewService(volumes.Dependencies{
DB: deps.DB,
diff --git a/backend/pkg/gitutil/git.go b/backend/pkg/gitutil/git.go
index 6d63716ba..763b715b4 100644
--- a/backend/pkg/gitutil/git.go
+++ b/backend/pkg/gitutil/git.go
@@ -45,8 +45,8 @@ const (
// binarySniffBytes is how much of a file is inspected to classify it as binary.
binarySniffBytes = 512
- // cloneScratchPrefix names the per-run clone scratch dirs created by Clone.
- cloneScratchPrefix = "gitops-"
+ // CloneScratchPrefix names the per-run clone scratch dirs created by Clone.
+ CloneScratchPrefix = "gitops-"
errUnsupportedURL = "repository URL must use http(s)://, ssh://, git://, or git@host:path"
@@ -365,7 +365,7 @@ func (c *Client) Clone(ctx context.Context, url, branch string, auth AuthConfig,
if err = os.MkdirAll(workDir, 0o755); err != nil {
return "", fmt.Errorf("failed to create work dir: %w", err)
}
- tmpDir, err := os.MkdirTemp(workDir, cloneScratchPrefix+"*")
+ tmpDir, err := os.MkdirTemp(workDir, CloneScratchPrefix+"*")
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
}
@@ -577,7 +577,7 @@ func (c *Client) PurgeScratchDirs(ctx context.Context, maxAge time.Duration) (in
return removed, errErr
}
entryPath := filepath.Join(root, entry.Name())
- if !entry.IsDir() || !strings.HasPrefix(entry.Name(), cloneScratchPrefix) {
+ if !entry.IsDir() || !strings.HasPrefix(entry.Name(), CloneScratchPrefix) {
continue
}
if maxAge > 0 {
diff --git a/backend/pkg/gitutil/git_write.go b/backend/pkg/gitutil/git_write.go
index 49c9e4872..32e412182 100644
--- a/backend/pkg/gitutil/git_write.go
+++ b/backend/pkg/gitutil/git_write.go
@@ -248,7 +248,7 @@ func (c *Client) initEmptyCheckoutInternal(url, branch string) (*WriteCheckout,
if err := os.MkdirAll(workDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create work dir: %w", err)
}
- repoPath, err := os.MkdirTemp(workDir, cloneScratchPrefix+"*")
+ repoPath, err := os.MkdirTemp(workDir, CloneScratchPrefix+"*")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %w", err)
}