diff options
| author | Max Audron <audron@cocaine.farm> | 2026-01-07 15:28:01 +0100 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2026-01-07 15:28:01 +0100 |
| commit | 84739ac2345265e518a50bc2e9a239eb442e6e22 (patch) | |
| tree | e289c856e5465f0c713e97a0ba86e1f734c3484e /modules/backup/maintenance.nix | |
| parent | add kopia module (diff) | |
setup backups for mail
Diffstat (limited to 'modules/backup/maintenance.nix')
| -rw-r--r-- | modules/backup/maintenance.nix | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/modules/backup/maintenance.nix b/modules/backup/maintenance.nix new file mode 100644 index 0000000..5f6c971 --- /dev/null +++ b/modules/backup/maintenance.nix @@ -0,0 +1,75 @@ +{ + config, + lib, + pkgs, + mkInstanceServices, + ... +}: +let + instanceType = lib.types.submodule { + options = { + maintenance = { + schedule = lib.mkOption { + type = lib.types.str; + default = "*-*-* 6:00:00"; + description = "kopia full maintenance schedule"; + }; + }; + }; + }; +in +{ + options.services.kopia.instances = lib.mkOption { + type = lib.types.attrsOf instanceType; + }; + + config = lib.mkIf config.services.kopia.enable ( + let + mkMaintenanceService = + name: instance: + lib.attrsets.nameValuePair "kopia-maintenance-${name}" { + description = "Kopia ${name} maintenance service"; + wants = [ + "kopia-repository-${name}.service" + ]; + after = [ "kopia-repository-${name}.service" ]; + conflicts = [ "kopia-snapshot-${name}.service" ]; + script = '' + ${pkgs.kopia}/bin/kopia maintenance run --full + ''; + serviceConfig = { + Type = "simple"; + User = "${instance.user}"; + WorkingDirectory = "~"; + SetLoginEnvironment = true; + # retry on failure + Restart = "on-failure"; + # wait 30 seconds before restarting + RestartSec = "30"; + # lower priority + Nice = "-19"; + IOSchedulingClass = "idle"; + }; + unitConfig = { + # limit the number of restarts to 5 in 1 day + StartLimitInterval = "1d"; + StartLimitBurst = "5"; + }; + }; + mkMaintenanceTimer = + name: instance: + lib.attrsets.nameValuePair "kopia-maintenance-${name}" { + description = "Kopia ${name} maintenance timer"; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = instance.maintenance.schedule; + }; + }; + in + { + # systemd service for repositories open + systemd.services = mkInstanceServices config.services.kopia.instances mkMaintenanceService; + systemd.timers = mkInstanceServices config.services.kopia.instances mkMaintenanceTimer; + } + ); +} |
