Merge pull request #3474 from kitone/fix-issue-3382
Honor RESTIC_CACHE_DIR environment variable
This commit is contained in:
commit
cb81ee9396
5 changed files with 27 additions and 3 deletions
9
changelog/unreleased/issue-3382
Normal file
9
changelog/unreleased/issue-3382
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
Change: honor RESTIC_CACHE_DIR environment variable in the check command
|
||||||
|
|
||||||
|
--cache-dir option doesn't honor the RESTIC_CACHE_DIR environment variable by
|
||||||
|
default. This fixes an issue with the restic check command that doesn't obey the
|
||||||
|
RESTIC_CACHE_DIR environment variable. Extend the behavior of cache command to
|
||||||
|
manage directories created by restic check.
|
||||||
|
|
||||||
|
https://github.com/restic/restic/issues/3382
|
||||||
|
https://github.com/restic/restic/pull/3474
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/cache"
|
"github.com/restic/restic/internal/cache"
|
||||||
|
@ -140,8 +141,13 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
||||||
size = fmt.Sprintf("%11s", formatBytes(uint64(bytes)))
|
size = fmt.Sprintf("%11s", formatBytes(uint64(bytes)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name := entry.Name()
|
||||||
|
if !strings.HasPrefix(name, "restic-check-cache-") {
|
||||||
|
name = name[:10]
|
||||||
|
}
|
||||||
|
|
||||||
tab.AddRow(data{
|
tab.AddRow(data{
|
||||||
entry.Name()[:10],
|
name,
|
||||||
fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)),
|
fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)),
|
||||||
old,
|
old,
|
||||||
size,
|
size,
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/cache"
|
||||||
"github.com/restic/restic/internal/checker"
|
"github.com/restic/restic/internal/checker"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/fs"
|
"github.com/restic/restic/internal/fs"
|
||||||
|
@ -158,6 +159,9 @@ func prepareCheckCache(opts CheckOptions, gopts *GlobalOptions) (cleanup func())
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedir := gopts.CacheDir
|
cachedir := gopts.CacheDir
|
||||||
|
if cachedir == "" {
|
||||||
|
cachedir = cache.EnvDir()
|
||||||
|
}
|
||||||
|
|
||||||
// use a cache in a temporary directory
|
// use a cache in a temporary directory
|
||||||
tempdir, err := ioutil.TempDir(cachedir, "restic-check-cache-")
|
tempdir, err := ioutil.TempDir(cachedir, "restic-check-cache-")
|
||||||
|
|
2
internal/cache/cache.go
vendored
2
internal/cache/cache.go
vendored
|
@ -167,7 +167,7 @@ func updateTimestamp(d string) error {
|
||||||
const MaxCacheAge = 30 * 24 * time.Hour
|
const MaxCacheAge = 30 * 24 * time.Hour
|
||||||
|
|
||||||
func validCacheDirName(s string) bool {
|
func validCacheDirName(s string) bool {
|
||||||
r := regexp.MustCompile(`^[a-fA-F0-9]{64}$`)
|
r := regexp.MustCompile(`^[a-fA-F0-9]{64}$|^restic-check-cache-[0-9]+$`)
|
||||||
return r.MatchString(s)
|
return r.MatchString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
internal/cache/dir.go
vendored
7
internal/cache/dir.go
vendored
|
@ -6,10 +6,15 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EnvDir return $RESTIC_CACHE_DIR env
|
||||||
|
func EnvDir() string {
|
||||||
|
return os.Getenv("RESTIC_CACHE_DIR")
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultDir returns $RESTIC_CACHE_DIR, or the default cache directory
|
// DefaultDir returns $RESTIC_CACHE_DIR, or the default cache directory
|
||||||
// for the current OS if that variable is not set.
|
// for the current OS if that variable is not set.
|
||||||
func DefaultDir() (cachedir string, err error) {
|
func DefaultDir() (cachedir string, err error) {
|
||||||
cachedir = os.Getenv("RESTIC_CACHE_DIR")
|
cachedir = EnvDir()
|
||||||
if cachedir != "" {
|
if cachedir != "" {
|
||||||
return cachedir, nil
|
return cachedir, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue