nixos-config/config/services/backup.nix

76 lines
1.9 KiB
Nix
Raw Normal View History

2022-06-12 15:39:15 +00:00
{
config,
pkgs,
...
}: let
2022-05-06 09:54:54 +00:00
resticPrune = pkgs.writeScript "resticPrune" ''
2022-08-26 18:27:47 +00:00
#!/bin/sh
2022-05-06 09:54:54 +00:00
export RESTIC_REPOSITORY="$RESTIC_REPOSITORY-prune"
2022-06-09 11:44:19 +00:00
${pkgs.restic}/bin/restic prune --no-cache --max-unused 0
${pkgs.restic}/bin/restic check --read-data-subset 10%
2022-05-06 09:54:54 +00:00
'';
2022-06-12 15:39:15 +00:00
in {
2022-04-24 12:11:41 +00:00
users.users.backup = {
description = "Backup user";
home = "/backup";
isSystemUser = true;
openssh.authorizedKeys.keys = [
2023-04-18 15:19:55 +00:00
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINN5Q/L2FyB3DIgdJRYnTGHW3naw5VQ9coOdwHYmv0aZ darkkirb@thinkrac"
2022-04-24 12:11:41 +00:00
];
2022-04-24 12:14:47 +00:00
group = "backup";
2022-05-07 09:12:32 +00:00
useDefaultShell = true;
2022-04-24 12:11:41 +00:00
};
2022-06-12 15:39:15 +00:00
users.groups.backup = {};
2022-05-06 09:54:54 +00:00
systemd.services.restic-prune = {
enable = true;
description = "Cleaning up restic backups";
serviceConfig = {
ExecStart = "${resticPrune}";
User = "backup";
Group = "backup";
Type = "oneshot";
2022-08-26 19:37:20 +00:00
EnvironmentFile = config.sops.secrets."services/restic/env".path;
2022-05-06 09:54:54 +00:00
};
};
2022-09-12 10:38:55 +00:00
systemd.timers.restic-prune = {
enable = true;
description = "Prune restic backups";
requires = ["restic-prune.service"];
wantedBy = ["multi-user.target"];
timerConfig = {
OnCalendar = "weekly";
RandomizedDelaySec = 604800;
};
};
2022-05-06 09:54:54 +00:00
sops.secrets."services/restic/env".owner = "backup";
2022-05-06 13:53:22 +00:00
sops.secrets."services/restic/rclone.conf" = {
owner = "backup";
path = "/backup/.config/rclone/rclone.conf";
};
systemd.services.backup-rclone = {
enable = true;
description = "Upload backup to remote";
script = ''
2024-02-02 16:32:44 +00:00
${pkgs.rclone}/bin/rclone sync /backup backup:backup-chir-rs/backup
2022-05-06 13:53:22 +00:00
'';
serviceConfig = {
User = "backup";
Group = "backup";
Type = "oneshot";
};
};
systemd.timers.backup-rclone = {
enable = true;
description = "Upload backup to remote";
2022-06-12 15:39:15 +00:00
requires = ["backup-rclone.service"];
wantedBy = ["network-online.target"];
2022-05-06 13:53:22 +00:00
timerConfig = {
2022-09-12 10:38:55 +00:00
OnCalendar = "weekly";
RandomizedDelaySec = 604800;
2022-05-06 13:53:22 +00:00
};
};
2022-04-24 12:11:41 +00:00
}