From 79f529f8edfb9bf893e74f7b1355bd7f2d7bdc3f Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Wed, 14 Feb 2024 20:31:42 -0800 Subject: [PATCH] fix: cleanup old versions of restic when upgrading --- internal/resticinstaller/resticinstaller.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/resticinstaller/resticinstaller.go b/internal/resticinstaller/resticinstaller.go index 5dbc6e0a7..5177127f4 100644 --- a/internal/resticinstaller/resticinstaller.go +++ b/internal/resticinstaller/resticinstaller.go @@ -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() @@ -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