2021-05-20 23:08:51 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.duplicity;
|
|
|
|
|
|
|
|
stateDirectory = "/var/lib/duplicity";
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
localTarget =
|
|
|
|
if hasPrefix "file://" cfg.targetUrl
|
2020-04-24 23:36:52 +00:00
|
|
|
then removePrefix "file://" cfg.targetUrl else null;
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
in
|
|
|
|
{
|
2020-04-24 23:36:52 +00:00
|
|
|
options.services.duplicity = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "backups with duplicity";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
root = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Root directory to backup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
include = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-05-20 23:08:51 +00:00
|
|
|
default = [ ];
|
2020-04-24 23:36:52 +00:00
|
|
|
example = [ "/home" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
List of paths to include into the backups. See the FILE SELECTION
|
2022-08-21 13:32:41 +00:00
|
|
|
section in {manpage}`duplicity(1)` for details on the syntax.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exclude = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-05-20 23:08:51 +00:00
|
|
|
default = [ ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
List of paths to exclude from backups. See the FILE SELECTION section in
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`duplicity(1)` for details on the syntax.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
targetUrl = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "s3://host:port/prefix";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Target url to backup to. See the URL FORMAT section in
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`duplicity(1)` for supported urls.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
secretFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Path of a file containing secrets (gpg passphrase, access key...) in
|
|
|
|
the format of EnvironmentFile as described by
|
2022-09-09 14:08:57 +00:00
|
|
|
{manpage}`systemd.exec(5)`. For example:
|
|
|
|
```
|
2022-08-12 12:06:08 +00:00
|
|
|
PASSPHRASE=«...»
|
|
|
|
AWS_ACCESS_KEY_ID=«...»
|
|
|
|
AWS_SECRET_ACCESS_KEY=«...»
|
2022-09-09 14:08:57 +00:00
|
|
|
```
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
frequency = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "daily";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Run duplicity with the given frequency (see
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`systemd.time(7)` for the format).
|
2020-04-24 23:36:52 +00:00
|
|
|
If null, do not run automatically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-05-20 23:08:51 +00:00
|
|
|
default = [ ];
|
|
|
|
example = [ "--backend-retry-delay" "100" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Extra command-line flags passed to duplicity. See
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`duplicity(1)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2021-05-20 23:08:51 +00:00
|
|
|
|
|
|
|
fullIfOlderThan = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "never";
|
|
|
|
example = "1M";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
If `"never"` (the default) always do incremental
|
2021-05-20 23:08:51 +00:00
|
|
|
backups (the first backup will be a full backup, of course). If
|
2022-08-12 12:06:08 +00:00
|
|
|
`"always"` always do full backups. Otherwise, this
|
2021-05-20 23:08:51 +00:00
|
|
|
must be a string representing a duration. Full backups will be made
|
|
|
|
when the latest full backup is older than this duration. If this is not
|
|
|
|
the case, an incremental backup is performed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cleanup = {
|
|
|
|
maxAge = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "6M";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-05-20 23:08:51 +00:00
|
|
|
If non-null, delete all backup sets older than the given time. Old backup sets
|
|
|
|
will not be deleted if backup sets newer than time depend on them.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
maxFull = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
example = 2;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-05-20 23:08:51 +00:00
|
|
|
If non-null, delete all backups sets that are older than the count:th last full
|
|
|
|
backup (in other words, keep the last count full backups and
|
|
|
|
associated incremental sets).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
maxIncr = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
example = 1;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-05-20 23:08:51 +00:00
|
|
|
If non-null, delete incremental sets of all backups sets that are
|
|
|
|
older than the count:th last full backup (in other words, keep only
|
|
|
|
old full backups and not their increments).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd = {
|
|
|
|
services.duplicity = {
|
|
|
|
description = "backup files with duplicity";
|
|
|
|
|
|
|
|
environment.HOME = stateDirectory;
|
|
|
|
|
2021-05-20 23:08:51 +00:00
|
|
|
script =
|
|
|
|
let
|
|
|
|
target = escapeShellArg cfg.targetUrl;
|
|
|
|
extra = escapeShellArgs ([ "--archive-dir" stateDirectory ] ++ cfg.extraFlags);
|
|
|
|
dup = "${pkgs.duplicity}/bin/duplicity";
|
|
|
|
in
|
|
|
|
''
|
|
|
|
set -x
|
|
|
|
${dup} cleanup ${target} --force ${extra}
|
|
|
|
${lib.optionalString (cfg.cleanup.maxAge != null) "${dup} remove-older-than ${lib.escapeShellArg cfg.cleanup.maxAge} ${target} --force ${extra}"}
|
|
|
|
${lib.optionalString (cfg.cleanup.maxFull != null) "${dup} remove-all-but-n-full ${toString cfg.cleanup.maxFull} ${target} --force ${extra}"}
|
2021-06-28 23:13:55 +00:00
|
|
|
${lib.optionalString (cfg.cleanup.maxIncr != null) "${dup} remove-all-inc-of-but-n-full ${toString cfg.cleanup.maxIncr} ${target} --force ${extra}"}
|
2021-05-20 23:08:51 +00:00
|
|
|
exec ${dup} ${if cfg.fullIfOlderThan == "always" then "full" else "incr"} ${lib.escapeShellArgs (
|
|
|
|
[ cfg.root cfg.targetUrl ]
|
2020-04-24 23:36:52 +00:00
|
|
|
++ concatMap (p: [ "--include" p ]) cfg.include
|
|
|
|
++ concatMap (p: [ "--exclude" p ]) cfg.exclude
|
2021-05-20 23:08:51 +00:00
|
|
|
++ (lib.optionals (cfg.fullIfOlderThan != "never" && cfg.fullIfOlderThan != "always") [ "--full-if-older-than" cfg.fullIfOlderThan ])
|
|
|
|
)} ${extra}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2021-05-20 23:08:51 +00:00
|
|
|
serviceConfig = {
|
2020-04-24 23:36:52 +00:00
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
StateDirectory = baseNameOf stateDirectory;
|
|
|
|
} // optionalAttrs (localTarget != null) {
|
|
|
|
ReadWritePaths = localTarget;
|
|
|
|
} // optionalAttrs (cfg.secretFile != null) {
|
|
|
|
EnvironmentFile = cfg.secretFile;
|
|
|
|
};
|
|
|
|
} // optionalAttrs (cfg.frequency != null) {
|
|
|
|
startAt = cfg.frequency;
|
|
|
|
};
|
|
|
|
|
|
|
|
tmpfiles.rules = optional (localTarget != null) "d ${localTarget} 0700 root root -";
|
|
|
|
};
|
|
|
|
|
|
|
|
assertions = singleton {
|
|
|
|
# Duplicity will fail if the last file selection option is an include. It
|
|
|
|
# is not always possible to detect but this simple case can be caught.
|
2021-05-20 23:08:51 +00:00
|
|
|
assertion = cfg.include != [ ] -> cfg.exclude != [ ] || cfg.extraFlags != [ ];
|
2020-04-24 23:36:52 +00:00
|
|
|
message = ''
|
|
|
|
Duplicity will fail if you only specify included paths ("Because the
|
|
|
|
default is to include all files, the expression is redundant. Exiting
|
|
|
|
because this probably isn't what you meant.")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|