From 610b6764443fe37d703147e42cbef4fa230f0364 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 11 Sep 2017 21:37:10 +0200 Subject: [PATCH] Automatically exclude current restic cache --- cmd/restic/cmd_backup.go | 10 ++++++++++ cmd/restic/exclude.go | 25 +++++++++++++++++++++++++ internal/cache/cache.go | 5 +++++ internal/restic/cache.go | 3 +++ 4 files changed, 43 insertions(+) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index c8b9154fb..0c254738a 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -387,6 +387,16 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { return err } + // exclude restic cache + if repo.Cache != nil { + f, err := rejectResticCache(repo) + if err != nil { + return err + } + + rejectFuncs = append(rejectFuncs, f) + } + err = repo.LoadIndex(context.TODO()) if err != nil { return err diff --git a/cmd/restic/exclude.go b/cmd/restic/exclude.go index 369c4df9a..bb14bfafb 100644 --- a/cmd/restic/exclude.go +++ b/cmd/restic/exclude.go @@ -12,6 +12,7 @@ import ( "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/filter" "github.com/restic/restic/internal/fs" + "github.com/restic/restic/internal/repository" ) // RejectFunc is a function that takes a filename and os.FileInfo of a @@ -177,3 +178,27 @@ func rejectByDevice(samples []string) (RejectFunc, error) { panic(fmt.Sprintf("item %v, device id %v not found, allowedDevs: %v", item, id, allowed)) }, nil } + +// rejectResticCache returns a RejectFunc that rejects the restic cache +// directory (if set). +func rejectResticCache(repo *repository.Repository) (RejectFunc, error) { + if repo.Cache == nil { + return func(string, os.FileInfo) bool { + return false + }, nil + } + cacheBase := repo.Cache.BaseDir() + + if cacheBase == "" { + return nil, errors.New("cacheBase is empty string") + } + + return func(item string, _ os.FileInfo) bool { + if fs.HasPathPrefix(cacheBase, item) { + debug.Log("rejecting restic cache directory %v", item) + return true + } + + return false + }, nil +} diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 0f610127e..6e75b8743 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -152,3 +152,8 @@ func (c *Cache) Wrap(be restic.Backend) restic.Backend { Cache: c, } } + +// BaseDir returns the base directory. +func (c *Cache) BaseDir() string { + return c.Base +} diff --git a/internal/restic/cache.go b/internal/restic/cache.go index ab9d4fb16..56ed060ac 100644 --- a/internal/restic/cache.go +++ b/internal/restic/cache.go @@ -4,6 +4,9 @@ import "io" // Cache manages a local cache. type Cache interface { + // BaseDir returns the base directory of the cache. + BaseDir() string + // Wrap returns a backend with a cache. Wrap(Backend) Backend