Skip to content

Commit

Permalink
Merge pull request restic#2425 from thiell/restic_cache_dir_env
Browse files Browse the repository at this point in the history
Add support for $RESTIC_CACHE_DIR
  • Loading branch information
rawtaz committed Nov 20, 2019
2 parents 929d2b8 + 0e897ef commit 41fe931
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions doc/manual_rest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ OS-specific cache folder:
* macOS: ``~/Library/Caches/restic``
* Windows: ``%LOCALAPPDATA%/restic``

The command line parameter ``--cache-dir`` can each be used to override the
default cache location. The parameter ``--no-cache`` disables the cache
entirely. In this case, all data is loaded from the repo.
The command line parameter ``--cache-dir`` or the environment variable
``$RESTIC_CACHE_DIR`` can be used to override the default cache location. The
parameter ``--no-cache`` disables the cache entirely. In this case, all data
is loaded from the repo.

The cache is ephemeral: When a file cannot be read from the cache, it is loaded
from the repository.
Expand Down
8 changes: 6 additions & 2 deletions internal/cache/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import (

// xdgCacheDir returns the cache directory according to XDG basedir spec, see
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
// unless RESTIC_CACHE_DIR is defined
func xdgCacheDir() (string, error) {
cachedir := os.Getenv("RESTIC_CACHE_DIR")
xdgcache := os.Getenv("XDG_CACHE_HOME")
home := os.Getenv("HOME")

if xdgcache != "" {
if cachedir != "" {
return cachedir, nil
} else if xdgcache != "" {
return filepath.Join(xdgcache, "restic"), nil
} else if home != "" {
return filepath.Join(home, ".cache", "restic"), nil
}

return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
return "", errors.New("unable to locate cache directory (RESTIC_CACHE_DIR, XDG_CACHE_HOME and HOME unset)")
}

// windowsCacheDir returns the cache directory for Windows.
Expand Down

0 comments on commit 41fe931

Please sign in to comment.