From d20b90ecb9348df81ba128480086b0d921ba1394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Schr=C3=B6ter?= Date: Sun, 1 May 2022 16:44:29 +0200 Subject: [PATCH] fix(backups): fix & improve ripgrep matching --- services/restic/common.nix | 35 ++++++++++++++++++++++++-------- services/restic/home-pc.nix | 40 ++++++++++++++++++------------------- services/restic/rpi4.nix | 19 +++++++++--------- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/services/restic/common.nix b/services/restic/common.nix index 6b3aa05..792d933 100644 --- a/services/restic/common.nix +++ b/services/restic/common.nix @@ -8,7 +8,8 @@ with lib; with builtins; let hasAnyAttr = flip (attrset: any (flip hasAttr attrset)); in { - resticConfig = args@{ name, extraPruneOpts ? [ ], ... }: + resticConfig = args@{ name, ripgrep ? false, paths ? [ ], ignorePatterns ? [ ] + , extraPruneOpts ? [ ], ... }: assert !hasAnyAttr [ "initialize" "repository" @@ -16,21 +17,39 @@ in { "passwordFile" "pruneOpts" ] args; - (removeAttrs args [ "name" "extraPruneOpts" ]) // { + assert (args ? paths); + assert (ripgrep || (!ripgrep && !(args ? ignorePatterns))); + { initialize = true; repository = "b2:felschr-backups:/${name}"; environmentFile = "/etc/nixos/secrets/restic/b2"; passwordFile = "/etc/nixos/secrets/restic/password"; - timerConfig = if (args ? timerConfig) then - args.timerConfig - else { - OnCalendar = "daily"; - }; + timerConfig.OnCalendar = "daily"; + paths = if ripgrep then null else paths; + dynamicFilesFrom = if ripgrep then + let + files = foldl (a: b: "${a} ${b}") "" paths; + ignoreFile = builtins.toFile "ignore" + (foldl (a: b: a + "\n" + b) "" ignorePatterns); + in '' + ${pkgs.ripgrep}/bin/rg \ + --files ${files} \ + --ignore-file ${ignoreFile} \ + | sed "s/\[/\\\[/" | sed "s/\]/\\\]/" + '' + else + null; pruneOpts = [ "--keep-daily 7" "--keep-weekly 4" "--keep-monthly 3" "--keep-yearly 1" ] ++ extraPruneOpts; - }; + } // (removeAttrs args [ + "name" + "ripgrep" + "paths" + "ignorePatterns" + "extraPruneOpts" + ]); } diff --git a/services/restic/home-pc.nix b/services/restic/home-pc.nix index 81b07fd..cfe4a6f 100644 --- a/services/restic/home-pc.nix +++ b/services/restic/home-pc.nix @@ -12,27 +12,25 @@ in { services.restic.backups.full = common.resticConfig { name = "home-pc"; - dynamicFilesFrom = let - ignore = builtins.toFile "excludes" '' - /var/lib/lxcfs - /var/lib/docker - /home/*/.local/share/Trash - /home/*/.cache - /home/*/Downloads - /home/*/.npm - /home/*/Games - /home/*/.steam - /home/*/.local/share/Steam - /home/*/.local/share/lutris - /home/felschr/media - /home/felschr/sync - /home/felschr/keybase - ''; - in '' - ${pkgs.ripgrep}/bin/rg \ - --files /etc/nixos /var/lib /home \ - --ignore-file ${ignore} - ''; + ripgrep = true; + paths = [ "/etc/nixos" "/var/lib" "/home" ]; + ignorePatterns = [ + "/var/lib/systemd" + "/var/lib/lxcfs" + "/var/lib/docker" + "/var/lib/flatpak" + "/home/*/.local/share/Trash" + "/home/*/.cache" + "/home/*/Downloads" + "/home/*/.npm" + "/home/*/Games" + "/home/*/.steam" + "/home/*/.local/share/Steam" + "/home/*/.local/share/lutris" + "/home/felschr/media" + "/home/felschr/sync" + "/home/felschr/keybase" + ]; timerConfig.OnCalendar = "0/4:00:00"; extraPruneOpts = [ "--keep-last 6" ]; }; diff --git a/services/restic/rpi4.nix b/services/restic/rpi4.nix index 8c9a1d3..f72a94e 100644 --- a/services/restic/rpi4.nix +++ b/services/restic/rpi4.nix @@ -12,17 +12,18 @@ in { services.restic.backups.full = common.resticConfig { name = "rpi4"; + ripgrep = true; paths = [ "/etc/nixos" "/var/lib" "/home" ]; + ignorePatterns = [ + "/var/lib/lxcfs" + "/var/lib/docker" + "/var/lib/flatpak" + "/var/lib/systemd" + "/home/*/.local/share/Trash" + "/home/*/.cache" + "/var/lib/jellyfin/transcodes" + ]; timerConfig.OnCalendar = "0/4:00:00"; extraPruneOpts = [ "--keep-last 6" ]; - extraOptions = let - exclude = '' - /var/lib/lxcfs - /var/lib/docker - /home/*/.local/share/Trash - /home/*/.cache - /var/lib/jellyfin/transcodes - ''; - in [ "--exclude=/var/lib/jellyfin/transcodes" ]; }; }