From 6341c7d72c8f773d5f14321150207c8b48a68f09 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 20 Nov 2017 22:08:53 +0100 Subject: [PATCH] cache: Add option to remove old cache dirs --- cmd/restic/global.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index e1c745104..df36df9b8 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "runtime" "strings" "syscall" @@ -23,6 +24,7 @@ import ( "github.com/restic/restic/internal/backend/swift" "github.com/restic/restic/internal/cache" "github.com/restic/restic/internal/debug" + "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/limiter" "github.com/restic/restic/internal/options" "github.com/restic/restic/internal/repository" @@ -45,6 +47,7 @@ type GlobalOptions struct { CacheDir string NoCache bool CACerts []string + CleanupCache bool LimitUploadKb int LimitDownloadKb int @@ -81,6 +84,7 @@ func init() { f.StringVar(&globalOptions.CacheDir, "cache-dir", "", "set the cache directory") f.BoolVar(&globalOptions.NoCache, "no-cache", false, "do not use a local cache") f.StringSliceVar(&globalOptions.CACerts, "cacert", nil, "path to load root certificates from (default: use system certificates)") + f.BoolVar(&globalOptions.CleanupCache, "cleanup-cache", false, "auto remove old cache directories") f.IntVar(&globalOptions.LimitUploadKb, "limit-upload", 0, "limits uploads to a maximum rate in KiB/s. (default: unlimited)") f.IntVar(&globalOptions.LimitDownloadKb, "limit-download", 0, "limits downloads to a maximum rate in KiB/s. (default: unlimited)") f.StringSliceVarP(&globalOptions.Options, "option", "o", []string{}, "set extended option (`key=value`, can be specified multiple times)") @@ -362,8 +366,26 @@ func OpenRepository(opts GlobalOptions) (*repository.Repository, error) { oldCacheDirs, err := cache.Old(c.Base) if err != nil { Warnf("unable to find old cache directories: %v", err) + } + + // nothing more to do if no old cache dirs could be found + if len(oldCacheDirs) == 0 { + return s, nil + } + + // cleanup old cache dirs if instructed to do so + if opts.CleanupCache { + Printf("removing %d old cache dirs from %v\n", len(oldCacheDirs), c.Base) + + for _, item := range oldCacheDirs { + dir := filepath.Join(c.Base, item) + err = fs.RemoveAll(dir) + if err != nil { + Warnf("unable to remove %v: %v\n", dir, err) + } + } } else { - Verbosef("found %d old cache directories in %v remove them with 'restic cache --cleanup'\n", + Verbosef("found %d old cache directories in %v, pass --cleanup-cache to remove them\n", len(oldCacheDirs), c.Base) }