diff options
| author | Max Audron <audron@cocaine.farm> | 2026-01-07 13:14:18 +0100 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2026-01-07 13:14:18 +0100 |
| commit | cdae0fb511851bf703a6fce3db2062b02b0e4c05 (patch) | |
| tree | 985608c2e6943cb986ba02b4d0c30e886ff2d287 /modules/backup/snapshot.nix | |
| parent | teamspeak switcharoo (diff) | |
add kopia module
Diffstat (limited to 'modules/backup/snapshot.nix')
| -rw-r--r-- | modules/backup/snapshot.nix | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/modules/backup/snapshot.nix b/modules/backup/snapshot.nix new file mode 100644 index 0000000..91076cf --- /dev/null +++ b/modules/backup/snapshot.nix @@ -0,0 +1,76 @@ +{ + pkgs, + lib, + config, + mkInstanceServices, + ... +}: +let + instanceType = lib.types.submodule { + options = { + path = lib.mkOption { + type = lib.types.str; + default = "/persistent"; + description = "snapshoted path for kopia instance."; + }; + schedule = lib.mkOption { + type = lib.types.str; + default = "daily"; + description = "Snapshot schedule for the Kopia instance."; + }; + }; + }; +in +{ + options.services.kopia.instances = lib.mkOption { + type = lib.types.attrsOf instanceType; + }; + + config = lib.mkIf config.services.kopia.enable ( + let + mkSnapshotService = + # refactor with mkRepositoryArgs + name: instance: + lib.attrsets.nameValuePair "kopia-snapshot-${name}" { + description = "Kopia S3 snapshot service"; + wants = [ + "kopia-repository-${name}.service" + ]; + after = [ "kopia-repository-${name}.service" ]; + script = '' + ${pkgs.kopia}/bin/kopia snapshot create ${instance.path} --description "Snapshot for ${name}" + ''; + serviceConfig = { + Type = "simple"; + User = "${instance.user}"; + WorkingDirectory = "~"; + SetLoginEnvironment = true; + # retry on failure + Restart = "on-failure"; + # wait 30 seconds before restarting + RestartSec = "30"; + # limit the number of restarts to 5 in 1 day + StartLimitInterval = "1d"; + StartLimitBurst = "5"; + # lower priority + Nice = "-19"; + IOSchedulingClass = "idle"; + }; + }; + mkSnapshotTimer = + name: instance: + lib.attrsets.nameValuePair "kopia-snapshot-${name}" { + description = "Kopia S3 snapshot timer"; + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = instance.schedule; + }; + }; + in + { + # systemd service for repositories open + systemd.services = mkInstanceServices config.services.kopia.instances mkSnapshotService; + systemd.timers = mkInstanceServices config.services.kopia.instances mkSnapshotTimer; + } + ); +} |
