2021-04-25 13:00:00 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2019-01-05 12:55:01 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.duplicity;
|
|
|
|
|
|
|
|
stateDirectory = "/var/lib/duplicity";
|
|
|
|
|
2021-04-25 13:00:00 +01:00
|
|
|
localTarget =
|
|
|
|
if hasPrefix "file://" cfg.targetUrl
|
2019-01-05 12:55:01 +00:00
|
|
|
then removePrefix "file://" cfg.targetUrl else null;
|
|
|
|
|
2021-04-25 13:00:00 +01:00
|
|
|
in
|
|
|
|
{
|
2019-01-05 12:55:01 +00:00
|
|
|
options.services.duplicity = {
|
|
|
|
enable = mkEnableOption "backups with duplicity";
|
|
|
|
|
|
|
|
root = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/";
|
|
|
|
description = ''
|
|
|
|
Root directory to backup.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
include = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-04-25 13:00:00 +01:00
|
|
|
default = [ ];
|
2019-01-05 12:55:01 +00:00
|
|
|
example = [ "/home" ];
|
|
|
|
description = ''
|
|
|
|
List of paths to include into the backups. See the FILE SELECTION
|
|
|
|
section in <citerefentry><refentrytitle>duplicity</refentrytitle>
|
|
|
|
<manvolnum>1</manvolnum></citerefentry> for details on the syntax.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
exclude = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-04-25 13:00:00 +01:00
|
|
|
default = [ ];
|
2019-01-05 12:55:01 +00:00
|
|
|
description = ''
|
|
|
|
List of paths to exclude from backups. See the FILE SELECTION section in
|
|
|
|
<citerefentry><refentrytitle>duplicity</refentrytitle>
|
|
|
|
<manvolnum>1</manvolnum></citerefentry> for details on the syntax.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
targetUrl = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "s3://host:port/prefix";
|
|
|
|
description = ''
|
|
|
|
Target url to backup to. See the URL FORMAT section in
|
|
|
|
<citerefentry><refentrytitle>duplicity</refentrytitle>
|
|
|
|
<manvolnum>1</manvolnum></citerefentry> for supported urls.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
secretFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Path of a file containing secrets (gpg passphrase, access key...) in
|
|
|
|
the format of EnvironmentFile as described by
|
|
|
|
<citerefentry><refentrytitle>systemd.exec</refentrytitle>
|
|
|
|
<manvolnum>5</manvolnum></citerefentry>. For example:
|
|
|
|
<programlisting>
|
|
|
|
PASSPHRASE=<replaceable>...</replaceable>
|
|
|
|
AWS_ACCESS_KEY_ID=<replaceable>...</replaceable>
|
|
|
|
AWS_SECRET_ACCESS_KEY=<replaceable>...</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
frequency = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "daily";
|
|
|
|
description = ''
|
|
|
|
Run duplicity with the given frequency (see
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry> for the format).
|
|
|
|
If null, do not run automatically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraFlags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2021-04-25 13:00:00 +01:00
|
|
|
default = [ ];
|
2021-05-13 13:00:00 +01:00
|
|
|
example = [ "--backend-retry-delay" "100" ];
|
2019-01-05 12:55:01 +00:00
|
|
|
description = ''
|
|
|
|
Extra command-line flags passed to duplicity. See
|
|
|
|
<citerefentry><refentrytitle>duplicity</refentrytitle>
|
|
|
|
<manvolnum>1</manvolnum></citerefentry>.
|
|
|
|
'';
|
|
|
|
};
|
2021-04-25 13:00:00 +01:00
|
|
|
|
2021-05-13 13:00:00 +01:00
|
|
|
fullIfOlderThan = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "never";
|
|
|
|
example = "1M";
|
|
|
|
description = ''
|
|
|
|
If <literal>"never"</literal> (the default) always do incremental
|
|
|
|
backups (the first backup will be a full backup, of course). If
|
|
|
|
<literal>"always"</literal> always do full backups. Otherwise, this
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-25 13:00:00 +01:00
|
|
|
cleanup = {
|
|
|
|
maxAge = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "6M";
|
|
|
|
description = ''
|
|
|
|
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;
|
|
|
|
description = ''
|
|
|
|
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).
|
|
|
|
'';
|
|
|
|
};
|
2021-05-13 13:00:00 +01:00
|
|
|
maxIncr = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
example = 1;
|
|
|
|
description = ''
|
|
|
|
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).
|
|
|
|
'';
|
|
|
|
};
|
2021-04-25 13:00:00 +01:00
|
|
|
};
|
2019-01-05 12:55:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd = {
|
|
|
|
services.duplicity = {
|
|
|
|
description = "backup files with duplicity";
|
|
|
|
|
|
|
|
environment.HOME = stateDirectory;
|
|
|
|
|
2021-04-25 13:00:00 +01: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-27 08:39:23 +01:00
|
|
|
${lib.optionalString (cfg.cleanup.maxIncr != null) "${dup} remove-all-inc-of-but-n-full ${toString cfg.cleanup.maxIncr} ${target} --force ${extra}"}
|
2021-05-13 13:00:00 +01:00
|
|
|
exec ${dup} ${if cfg.fullIfOlderThan == "always" then "full" else "incr"} ${lib.escapeShellArgs (
|
2021-04-25 13:00:00 +01:00
|
|
|
[ cfg.root cfg.targetUrl ]
|
|
|
|
++ concatMap (p: [ "--include" p ]) cfg.include
|
|
|
|
++ concatMap (p: [ "--exclude" p ]) cfg.exclude
|
2021-05-13 13:00:00 +01:00
|
|
|
++ (lib.optionals (cfg.fullIfOlderThan != "never" && cfg.fullIfOlderThan != "always") [ "--full-if-older-than" cfg.fullIfOlderThan ])
|
2021-04-25 13:00:00 +01:00
|
|
|
)} ${extra}
|
2019-01-05 12:55:01 +00:00
|
|
|
'';
|
2021-04-25 13:00:00 +01:00
|
|
|
serviceConfig = {
|
2019-01-05 12:55:01 +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-04-25 13:00:00 +01:00
|
|
|
assertion = cfg.include != [ ] -> cfg.exclude != [ ] || cfg.extraFlags != [ ];
|
2019-01-05 12:55:01 +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.")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|