fix(backups): fix & improve ripgrep matching

This commit is contained in:
Felix Schröter 2022-05-01 16:44:29 +02:00
parent 68d499f5cf
commit d20b90ecb9
Signed by: felschr
GPG key ID: 671E39E6744C807D
3 changed files with 56 additions and 38 deletions

View file

@ -8,7 +8,8 @@ with lib;
with builtins; with builtins;
let hasAnyAttr = flip (attrset: any (flip hasAttr attrset)); let hasAnyAttr = flip (attrset: any (flip hasAttr attrset));
in { in {
resticConfig = args@{ name, extraPruneOpts ? [ ], ... }: resticConfig = args@{ name, ripgrep ? false, paths ? [ ], ignorePatterns ? [ ]
, extraPruneOpts ? [ ], ... }:
assert !hasAnyAttr [ assert !hasAnyAttr [
"initialize" "initialize"
"repository" "repository"
@ -16,21 +17,39 @@ in {
"passwordFile" "passwordFile"
"pruneOpts" "pruneOpts"
] args; ] args;
(removeAttrs args [ "name" "extraPruneOpts" ]) // { assert (args ? paths);
assert (ripgrep || (!ripgrep && !(args ? ignorePatterns)));
{
initialize = true; initialize = true;
repository = "b2:felschr-backups:/${name}"; repository = "b2:felschr-backups:/${name}";
environmentFile = "/etc/nixos/secrets/restic/b2"; environmentFile = "/etc/nixos/secrets/restic/b2";
passwordFile = "/etc/nixos/secrets/restic/password"; passwordFile = "/etc/nixos/secrets/restic/password";
timerConfig = if (args ? timerConfig) then timerConfig.OnCalendar = "daily";
args.timerConfig paths = if ripgrep then null else paths;
else { dynamicFilesFrom = if ripgrep then
OnCalendar = "daily"; 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 = [ pruneOpts = [
"--keep-daily 7" "--keep-daily 7"
"--keep-weekly 4" "--keep-weekly 4"
"--keep-monthly 3" "--keep-monthly 3"
"--keep-yearly 1" "--keep-yearly 1"
] ++ extraPruneOpts; ] ++ extraPruneOpts;
}; } // (removeAttrs args [
"name"
"ripgrep"
"paths"
"ignorePatterns"
"extraPruneOpts"
]);
} }

View file

@ -12,27 +12,25 @@ in {
services.restic.backups.full = common.resticConfig { services.restic.backups.full = common.resticConfig {
name = "home-pc"; name = "home-pc";
dynamicFilesFrom = let ripgrep = true;
ignore = builtins.toFile "excludes" '' paths = [ "/etc/nixos" "/var/lib" "/home" ];
/var/lib/lxcfs ignorePatterns = [
/var/lib/docker "/var/lib/systemd"
/home/*/.local/share/Trash "/var/lib/lxcfs"
/home/*/.cache "/var/lib/docker"
/home/*/Downloads "/var/lib/flatpak"
/home/*/.npm "/home/*/.local/share/Trash"
/home/*/Games "/home/*/.cache"
/home/*/.steam "/home/*/Downloads"
/home/*/.local/share/Steam "/home/*/.npm"
/home/*/.local/share/lutris "/home/*/Games"
/home/felschr/media "/home/*/.steam"
/home/felschr/sync "/home/*/.local/share/Steam"
/home/felschr/keybase "/home/*/.local/share/lutris"
''; "/home/felschr/media"
in '' "/home/felschr/sync"
${pkgs.ripgrep}/bin/rg \ "/home/felschr/keybase"
--files /etc/nixos /var/lib /home \ ];
--ignore-file ${ignore}
'';
timerConfig.OnCalendar = "0/4:00:00"; timerConfig.OnCalendar = "0/4:00:00";
extraPruneOpts = [ "--keep-last 6" ]; extraPruneOpts = [ "--keep-last 6" ];
}; };

View file

@ -12,17 +12,18 @@ in {
services.restic.backups.full = common.resticConfig { services.restic.backups.full = common.resticConfig {
name = "rpi4"; name = "rpi4";
ripgrep = true;
paths = [ "/etc/nixos" "/var/lib" "/home" ]; 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"; timerConfig.OnCalendar = "0/4:00:00";
extraPruneOpts = [ "--keep-last 6" ]; 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" ];
}; };
} }