Skip to content

Commit

Permalink
fix: cleanup old versions of restic when upgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Feb 15, 2024
1 parent 8fa90ab commit 79f529f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/resticinstaller/resticinstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ func installResticIfNotExists(resticInstallPath string) error {
})
}

func removeOldVersions(installDir string) {
files, err := os.ReadDir(installDir)
if err != nil {
zap.S().Errorf("remove old restic versions: read dir %v: %v", installDir, err)
return
}

for _, file := range files {
if !strings.HasPrefix(file.Name(), "restic-") || strings.Contains(file.Name(), RequiredResticVersion) {
continue
}

if err := os.Remove(path.Join(installDir, file.Name())); err != nil {
zap.S().Errorf("remove old restic version %v: %v", file.Name(), err)
}
}
}

// FindOrInstallResticBinary first tries to find the restic binary if provided as an environment variable. Otherwise it downloads restic if not already installed.
func FindOrInstallResticBinary() (string, error) {
findResticMu.Lock()
Expand Down Expand Up @@ -229,6 +247,7 @@ func FindOrInstallResticBinary() (string, error) {
return "", fmt.Errorf("install restic: %w", err)
}
zap.S().Infof("Installed restic %v", RequiredResticVersion)
removeOldVersions(path.Dir(resticInstallPath))
}

return resticInstallPath, nil
Expand Down

0 comments on commit 79f529f

Please sign in to comment.