Skip to content

Modern Docker Management, Designed for Everyone

fix: prune local backup repositories daily to free deleted backup data (#4235)

Giulio Savini2ecc76332295
diff --git a/backend/internal/backup/service.go b/backend/internal/backup/service.go
index 9b1abf31d..023a35a88 100644
--- a/backend/internal/backup/service.go
+++ b/backend/internal/backup/service.go
@@ -55,6 +55,9 @@ const (
VolumeAdmissionScope = "volume-backup"
SystemAdmissionScope = "system-backup"
RecoveryKeyConfigID = "system-recovery"
+
+ // rusticRepositoryMissingMessage is how Rustic reports an uninitialized repository.
+ rusticRepositoryMissingMessage = "No repository config file found"
)
type Repository struct {
@@ -491,12 +494,9 @@ func fullSnapshotIDInternal(id string) bool {
return err == nil
}
-// ForgetSnapshots removes the snapshots from the repository and prunes their
-// data in a single pass.
+// ForgetSnapshots removes the snapshots and prunes their data in a single pass;
+// with no IDs it only prunes, skipping an uninitialized repository.
func (e *Engine) ForgetSnapshots(ctx context.Context, dockerClient *client.Client, repository Repository, password string, snapshotIDs []string) error {
- if len(snapshotIDs) == 0 {
- return errors.New("at least one snapshot ID is required")
- }
requested := make([]string, 0, len(snapshotIDs))
for _, id := range snapshotIDs {
if !fullSnapshotIDInternal(id) {
@@ -516,6 +516,9 @@ func (e *Engine) ForgetSnapshots(ctx context.Context, dockerClient *client.Clien
return err
}
output, err := e.runContainerInternal(ctx, dockerClient, repository, password, []string{"snapshots", "--json"})
+ if len(snapshotIDs) == 0 && err != nil && strings.Contains(err.Error(), rusticRepositoryMissingMessage) {
+ return nil
+ }
if err != nil {
return err
}
diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.go
index 6af06284f..b7148f540 100644
--- a/backend/internal/bootstrap/jobs_bootstrap.go
+++ b/backend/internal/bootstrap/jobs_bootstrap.go
@@ -279,6 +279,7 @@ type registerJobsParams struct {
ActivitySweep *scheduler.ActivitySweepJob
UploadSessionsCleanup *scheduler.UploadSessionsCleanupJob
GitCloneCleanup *scheduler.GitCloneCleanupJob
+ BackupRepositoryPrune *scheduler.BackupRepositoryPruneJob
ApnsOutbox *scheduler.ApnsOutboxJob
}
@@ -300,6 +301,7 @@ func registerJobs(params registerJobsParams) error {
params.ActivitySweep,
params.UploadSessionsCleanup,
params.GitCloneCleanup,
+ params.BackupRepositoryPrune,
params.UpgradeLogCleanup,
params.ApnsOutbox,
} {
diff --git a/backend/internal/di/di.go b/backend/internal/di/di.go
index 96643b317..1631ac484 100644
--- a/backend/internal/di/di.go
+++ b/backend/internal/di/di.go
@@ -179,6 +179,7 @@ var JobOptions = fx.Options(
scheduler.NewActivitySweepJob,
scheduler.NewUploadSessionsCleanupJob,
scheduler.NewGitCloneCleanupJob,
+ scheduler.NewBackupRepositoryPruneJob,
fx.Annotate(scheduler.NewUpgradeLogCleanupJob, fx.ResultTags(`name:"upgrade-log-cleanup"`)),
scheduler.NewApnsOutboxJob,
),
diff --git a/backend/internal/system/children/backup/service.go b/backend/internal/system/children/backup/service.go
index 01ad644d8..46047cefb 100644
--- a/backend/internal/system/children/backup/service.go
+++ b/backend/internal/system/children/backup/service.go
@@ -763,6 +763,26 @@ func (s *Service) deleteRunsInternal(ctx context.Context, localRuns2 []*backupty
return deleteErr
}
+// PruneLocalRepository frees the data of deleted local system backups once Rustic's keep-delete window passes.
+func (s *Service) PruneLocalRepository(ctx context.Context) error {
+ key, err := s.recoveryKeys.Get(ctx)
+ if errors.Is(err, backup.ErrRecoveryKeyNotConfigured) {
+ return nil
+ }
+ if err != nil {
+ return err
+ }
+ dockerClient, err := s.dockerService.GetClient(ctx)
+ if err != nil {
+ return err
+ }
+ repository, err := s.localRepositoryInternal(ctx, dockerClient, false)
+ if err != nil {
+ return err
+ }
+ return s.engine.ForgetSnapshots(ctx, dockerClient, repository, key, nil)
+}
+
func (s *Service) forgetLocalSnapshotsInternal(ctx context.Context, dockerClient *client.Client, key string, localRuns []*backuptypes.SystemBackupRun) error {
if len(localRuns) == 0 {
return nil
diff --git a/backend/internal/system/service.go b/backend/internal/system/service.go
index 66ca6fca3..d8cea128a 100644
--- a/backend/internal/system/service.go
+++ b/backend/internal/system/service.go
@@ -141,6 +141,11 @@ func (s *SystemService) SetBackupScheduler(ctx context.Context, dynamicScheduler
return s.backup.SetScheduler(ctx, dynamicScheduler, admissionGate)
}
+// PruneLocalBackupRepository frees the space of deleted local system backups.
+func (s *SystemService) PruneLocalBackupRepository(ctx context.Context) error {
+ return s.backup.PruneLocalRepository(ctx)
+}
+
// RegisterBackupJobOnStartup schedules every saved system and system-managed volume backup policy.
func (s *SystemService) RegisterBackupJobOnStartup(ctx context.Context) {
s.backup.RegisterBackupJobOnStartup(ctx)
diff --git a/backend/internal/volume/children/backup/service.go b/backend/internal/volume/children/backup/service.go
index 69847cabd..4f080b64e 100644
--- a/backend/internal/volume/children/backup/service.go
+++ b/backend/internal/volume/children/backup/service.go
@@ -766,6 +766,19 @@ func (s *Service) deleteRusticBackupsInternal(ctx context.Context, entries []*vo
return deleteErr
}
+// PruneLocalRepository frees the data of deleted local volume backups once Rustic's keep-delete window passes.
+func (s *Service) PruneLocalRepository(ctx context.Context) error {
+ dockerClient, err := s.deps.Docker.GetClient(ctx)
+ if err != nil {
+ return err
+ }
+ repository, err := s.localRusticRepositoryInternal(ctx, dockerClient, false)
+ if err != nil {
+ return err
+ }
+ return s.forgetSnapshotsInternal(ctx, dockerClient, repository, nil)
+}
+
func (s *Service) forgetLocalSnapshotsInternal(ctx context.Context, dockerClient *client.Client, entries []*volume.Backup) error {
if len(entries) == 0 {
return nil
diff --git a/backend/internal/volume/service.go b/backend/internal/volume/service.go
index 096467dfd..bc41c12e4 100644
--- a/backend/internal/volume/service.go
+++ b/backend/internal/volume/service.go
@@ -202,6 +202,11 @@ func (s *VolumeService) ReconcileInterruptedBackups(ctx context.Context, protect
return query.Updates(map[string]any{"status": VolumeBackupStatusFailed, "error": "Backup interrupted by Arcane restart"}).Error
}
+// PruneLocalBackupRepository frees the space of deleted local volume backups.
+func (s *VolumeService) PruneLocalBackupRepository(ctx context.Context) error {
+ return s.backup.PruneLocalRepository(ctx)
+}
+
// MigrateRepositoryPasswords re-keys this instance's own volume backup repositories to the recovery key; other instances re-key their own roots.
func (s *VolumeService) MigrateRepositoryPasswords(ctx context.Context) error {
return s.backup.MigrateRepositoryPasswords(ctx)
diff --git a/backend/pkg/scheduler/backup_repository_prune_job.go b/backend/pkg/scheduler/backup_repository_prune_job.go
new file mode 100644
index 000000000..dc9dfa735
--- /dev/null
+++ b/backend/pkg/scheduler/backup_repository_prune_job.go
@@ -0,0 +1,61 @@
+package scheduler
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "log/slog"
+
+ schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
+
+ "github.com/getarcaneapp/arcane/backend/v2/internal/system"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/volume"
+)
+
+// BackupRepositoryPruneJobName identifies the periodic prune of the local backup repositories.
+const BackupRepositoryPruneJobName = "backup-repository-prune"
+
+// BackupRepositoryPruneJob removes packs that deleted backups left marked for deletion.
+// Internal job: no job_metadata entry, invisible in the Jobs UI.
+type BackupRepositoryPruneJob struct {
+ systemService *system.SystemService
+ volumeService *volume.VolumeService
+}
+
+// NewBackupRepositoryPruneJob builds the prune job for the scheduler.
+func NewBackupRepositoryPruneJob(systemService *system.SystemService, volumeService *volume.VolumeService) *BackupRepositoryPruneJob {
+ return &BackupRepositoryPruneJob{systemService: systemService, volumeService: volumeService}
+}
+
+// Name returns the job name.
+func (j *BackupRepositoryPruneJob) Name() string {
+ return BackupRepositoryPruneJobName
+}
+
+// Schedule runs the prune twice a day, staggered after the default 03:00
+// system backup schedule. Rustic keeps marked packs for 23 hours, so a daily
+// run could leave deleted data on disk for almost two days.
+func (j *BackupRepositoryPruneJob) Schedule(_ context.Context) string {
+ return "0 15 4,16 * * *"
+}
+
+// Run prunes the local system and volume backup repositories.
+func (j *BackupRepositoryPruneJob) Run(ctx context.Context) (schedulertypes.Outcome, error) {
+ var pruneErr error
+ if err := j.systemService.PruneLocalBackupRepository(ctx); err != nil {
+ pruneErr = fmt.Errorf("prune system backup repository: %w", err)
+ }
+ if err := j.volumeService.PruneLocalBackupRepository(ctx); err != nil {
+ pruneErr = errors.Join(pruneErr, fmt.Errorf("prune volume backup repository: %w", err))
+ }
+ if pruneErr != nil {
+ slog.ErrorContext(ctx, "Failed to prune local backup repositories", "jobName", BackupRepositoryPruneJobName, "error", pruneErr)
+ return schedulertypes.Outcome{}, pruneErr
+ }
+ return schedulertypes.Outcome{Status: schedulertypes.Succeeded}, nil
+}
+
+// Reschedule is a no-op: the schedule is fixed.
+func (j *BackupRepositoryPruneJob) Reschedule(_ context.Context) error {
+ return nil
+}