Set cache via environment variable


#1

Is there a way to set the cache directory via an environment variable? When working at the command line it is quite annoying to manually type --cache-dir on every command.

Is TMPDIR == --cache-dir? mentions XDG_CACHE_HOME, I set this variable but restic seems to be using the existing variable instead.


#2

Uhm, the variable you’ve mentioned (XDG_CACHE_HOME) is correct, if it is set and the flag --cache-dir is not present, restic uses the directory $XDG_CACHE_HOME/restic for its cache.

The priority for selecting the cache directory is as follows:

  • Command-line flag --cache-dir
  • Environment variable $XDG_CACHE_HOME
  • Default (~/.cache/restic)

Can you paste your complete command line?


#3

Sure. Earlier in the script I set:

SET B2_ACCOUNT_ID=<removed>
SET B2_ACCOUNT_KEY=<removed>
SET RESTIC_PASSWORD_FILE=%@full[Utils:restic.txt]
SET RESTIC_REPOSITORY=b2:%[projectname]:restic-%[computername]
SET XDG_CACHE_HOME=D:\restic-cache

Then later I call restic multiple times. Here’s one:
restic --tag IIS backup C:\inetpub D:\HTTP "C:\Windows\System32\inetsrv\config"

Since the B2_ and RESTIC_ variables are required for restic to run, I am confident that all the variables have been set correctly. I also tried pasting each command at the command line, running restic, and then verifying the environment variables are still set correctly.

I deleted the current cache (in case restic checks in the default location for a cache first), then ran restic and this is the output:

repository fee88442 opened successfully, password is correct
created new cache in C:\Users\daveadmin\AppData\Local\restic
Files: 0 new, 1 changed, 287 unmodified
Dirs: 0 new, 1 changed, 4 unmodified
Added to the repo: 486.756 KiB
processed 288 files, 26.162 MiB in 1:13
snapshot b1a2475c saved

D:\restic-cache does exist, permissions are good, but it remains empty.


#4

XDG_CACHE_HOME is only used on platforms that aren’t Darwin (OS X) or Windows.

On Windows, it uses %LOCALAPPDATA%\restic as the default.

Another option is to put common arguments in a variable, for example:

set RESTIC_CMD=restic --cache-dir=...
%RESTIC_CMD% backup ...

#5

I don’t know how to do this in Windows, but if you’re working with a script then in bash you could to something like this:

restic "$@" --cache-dir=/path/to/your/cache/dir

Then, every time you call the script, you can use any command and it will use the cache directory specified in your script.


#6

True, it just adds complexity when all the other “include it on every single command” things can be defined in the environment.

Would it be possible to adds a environment variable for this one? I’d suggest it should be checked between the command line and XDG variables.


#7

Yes, it’d be possible. Can you please report this as a feature request on GitHub, so we can track it? Thanks!