Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/restic: correct location of cache directory #99579

Merged
merged 1 commit into from Jan 31, 2021

Conversation

@eliasp
Copy link
Member

@eliasp eliasp commented Oct 4, 2020

Motivation for this change

By default, restic determines the location of the cache based on the XDG
base dir specification, which is ~/.cache/restic when the environment
variable $XDG_CACHE_HOME isn't set.
As restic is executed as root by default, this resulted in the cache being
written to /root/.cache/restic, which is not quite right for a system
service and also meant, multiple backup services would use the same cache
directory - potentially causing issues with locking, data corruption,
etc.

The goal was to ensure, restic uses the correct cache location for a
system service - one cache per backup specification, using /var/cache
as the base directory for it.

systemd sets the environment variable $CACHE_DIRECTORY once
CacheDirectory= is defined, but restic doesn't change its behavior
based on the presence of this environment variable.
Instead, the specifier [1] %C can be used to point restic explicitly
towards the correct cache location using the --cache-dir argument.

Furthermore, the CacheDirectoryMode= was set to 0700, as the default
of 0755 is far too open in this case, as the cache might contain
sensitive data.

[1] https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
@jerith666
Copy link
Contributor

@jerith666 jerith666 commented Jan 25, 2021

Looks quite reasonable. I merged it locally and will report back after I see whether a couple of nightly backups work okay.

Copy link
Contributor

@jerith666 jerith666 left a comment

Nightly backups seem to be working just fine; approving.

@eliasp
Copy link
Member Author

@eliasp eliasp commented Jan 28, 2021

Do we have to take care of the now stale /root/.cache/restic? After this change, this is basically left to rot and consume unnecessarily diskspace…

And if so, what approach could be used to handle this?

@jerith666
Copy link
Contributor

@jerith666 jerith666 commented Jan 28, 2021

Good point. We could add a release note, not sure what else to do beyond that.

@cole-h
Copy link
Member

@cole-h cole-h commented Jan 28, 2021

+1 to a release note.

@eliasp eliasp force-pushed the restic-cache-directory branch from 0415014 to f46cf9e Jan 29, 2021
@eliasp
Copy link
Member Author

@eliasp eliasp commented Jan 29, 2021

I added a release note and amended it into the existing commit.
Please have a brief look, it's close to 10 years that I wrote my last docbook XML 😄

By default, restic determines the location of the cache based on the XDG
base dir specification, which is `~/.cache/restic` when the environment
variable `$XDG_CACHE_HOME` isn't set.
As restic is executed as root by default, this resulted in the cache being
written to `/root/.cache/restic`, which is not quite right for a system
service and also meant, multiple backup services would use the same cache
directory - potentially causing issues with locking, data corruption,
etc.

The goal was to ensure, restic uses the correct cache location for a
system service - one cache per backup specification, using `/var/cache`
as the base directory for it.

systemd sets the environment variable `$CACHE_DIRECTORY` once
`CacheDirectory=` is defined, but restic doesn't change its behavior
based on the presence of this environment variable.
Instead, the specifier [1] `%C` can be used to point restic explicitly
towards the correct cache location using the `--cache-dir` argument.

Furthermore, the `CacheDirectoryMode=` was set to `0700`, as the default
of `0755` is far too open in this case, as the cache might contain
sensitive data.

[1] https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers
@eliasp eliasp force-pushed the restic-cache-directory branch from ca45de4 to 3b1de09 Jan 29, 2021
cole-h
cole-h approved these changes Jan 31, 2021
Copy link
Member

@cole-h cole-h left a comment

Diff LGTM. Thanks!

@cole-h cole-h merged commit 27da119 into NixOS:master Jan 31, 2021
18 checks passed
18 checks passed
@github-actions[bot]
nixos
Details
@github-actions[bot]
tests
Details
@github-actions[bot]
action
Details
@ofborg[bot]
Evaluation Performance Report Evaluator Performance Report
Details
@github-actions[bot]
Wait for ofborg
Details
@ofborg[bot]
grahamcofborg-eval ^.^!
Details
@ofborg[bot]
grahamcofborg-eval-check-maintainers matching changed paths to changed attrs...
Details
@ofborg[bot]
grahamcofborg-eval-check-meta config.nix: checkMeta = true
Details
@ofborg[bot]
grahamcofborg-eval-darwin nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./pkgs/t
Details
@ofborg[bot]
grahamcofborg-eval-lib-tests nix-build --arg pkgs import ./. {} ./lib/tests/release.nix
Details
@ofborg[bot]
grahamcofborg-eval-nixos nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./nixos/
Details
@ofborg[bot]
grahamcofborg-eval-nixos-manual nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./nixos/
Details
@ofborg[bot]
grahamcofborg-eval-nixos-options nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./nixos/
Details
@ofborg[bot]
grahamcofborg-eval-nixpkgs-manual nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./pkgs/t
Details
@ofborg[bot]
grahamcofborg-eval-nixpkgs-tarball nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./pkgs/t
Details
@ofborg[bot]
grahamcofborg-eval-nixpkgs-unstable-jobset nix-instantiate --arg nixpkgs { outPath=./.; revCount=999999; shortRev="3b1de09"; rev="3b1de0949789cf607e4b55ad773e47759f96bb3c"; } ./pkgs/t
Details
@ofborg[bot]
grahamcofborg-eval-package-list nix-env -qa --json --file .
Details
@ofborg[bot]
grahamcofborg-eval-package-list-no-aliases nix-env -qa --json --file . --arg config { allowAliases = false; }
Details
@cole-h
Copy link
Member

@cole-h cole-h commented Jan 31, 2021

Also, thanks for such a great commit message. We should all strive to be as detailed as you were :)

@eliasp eliasp deleted the restic-cache-directory branch Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants