summary refs log tree commit diff
diff options
context:
space:
mode:
authorElias Probst <mail@eliasprobst.eu>2020-10-04 18:47:52 +0200
committerCole Helbling <cole.e.helbling@outlook.com>2021-01-30 18:24:51 -0800
commit27da11972d3fd9353f81e94a6549e8a0da40f45d (patch)
treec92e69538f44b9a06f77ae96e0e88d1effc88595
parent1fb2d04c269782c305630bb8ad151dc0f82fc802 (diff)
downloadnixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar.gz
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar.bz2
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar.lz
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar.xz
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.tar.zst
nixpkgs-27da11972d3fd9353f81e94a6549e8a0da40f45d.zip
nixos/restic: correct location of cache directory
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
-rw-r--r--nixos/doc/manual/release-notes/rl-2103.xml5
-rw-r--r--nixos/modules/services/backup/restic.nix4
2 files changed, 8 insertions, 1 deletions
diff --git a/nixos/doc/manual/release-notes/rl-2103.xml b/nixos/doc/manual/release-notes/rl-2103.xml
index 24a0281310c..41086e2220f 100644
--- a/nixos/doc/manual/release-notes/rl-2103.xml
+++ b/nixos/doc/manual/release-notes/rl-2103.xml
@@ -653,6 +653,11 @@ self: super:
      The <varname>platform</varname> grouping of these things never meant anything, and was just a historial/implementation artifact that was overdue removal.
     </para>
    </listitem>
+   <listitem>
+    <para>
+     <varname>services.restic</varname> now uses a dedicated cache directory for every backup defined in <varname>services.restic.backups</varname>. The old global cache directory, <literal>/root/.cache/restic</literal>, is now unused and can be removed to free up disk space.
+    </para>
+   </listitem>
   </itemizedlist>
  </section>
 </section>
diff --git a/nixos/modules/services/backup/restic.nix b/nixos/modules/services/backup/restic.nix
index d869835bf07..573f0efa9da 100644
--- a/nixos/modules/services/backup/restic.nix
+++ b/nixos/modules/services/backup/restic.nix
@@ -243,9 +243,11 @@ in
           restartIfChanged = false;
           serviceConfig = {
             Type = "oneshot";
-            ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd;
+            ExecStart = [ "${resticCmd} backup --cache-dir=%C/restic-backups-${name} ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd;
             User = backup.user;
             RuntimeDirectory = "restic-backups-${name}";
+            CacheDirectory = "restic-backups-${name}";
+            CacheDirectoryMode = "0700";
           } // optionalAttrs (backup.s3CredentialsFile != null) {
             EnvironmentFile = backup.s3CredentialsFile;
           };