2018-03-12 19:20:24 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
isLocalPath = x:
|
|
|
|
builtins.substring 0 1 x == "/" # absolute path
|
|
|
|
|| builtins.substring 0 1 x == "." # relative path
|
|
|
|
|| builtins.match "[.*:.*]" == null; # not machine:path
|
2019-09-30 15:35:13 +00:00
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
mkExcludeFile = cfg:
|
|
|
|
# Write each exclude pattern to a new line
|
2022-12-10 11:31:13 +00:00
|
|
|
pkgs.writeText "excludefile" (concatMapStrings (s: s + "\n") cfg.exclude);
|
|
|
|
|
|
|
|
mkPatternsFile = cfg:
|
|
|
|
# Write each pattern to a new line
|
|
|
|
pkgs.writeText "patternsfile" (concatMapStrings (s: s + "\n") cfg.patterns);
|
2018-03-12 19:20:24 +00:00
|
|
|
|
|
|
|
mkKeepArgs = cfg:
|
|
|
|
# If cfg.prune.keep e.g. has a yearly attribute,
|
|
|
|
# its content is passed on as --keep-yearly
|
|
|
|
concatStringsSep " "
|
|
|
|
(mapAttrsToList (x: y: "--keep-${x}=${toString y}") cfg.prune.keep);
|
|
|
|
|
2022-10-28 02:57:28 +00:00
|
|
|
mkBackupScript = name: cfg: pkgs.writeShellScript "${name}-script" (''
|
|
|
|
set -e
|
2018-03-12 19:20:24 +00:00
|
|
|
on_exit()
|
|
|
|
{
|
|
|
|
exitStatus=$?
|
|
|
|
${cfg.postHook}
|
|
|
|
exit $exitStatus
|
|
|
|
}
|
2022-07-02 10:51:12 +00:00
|
|
|
trap on_exit EXIT
|
2018-03-12 19:20:24 +00:00
|
|
|
|
2022-01-08 22:45:07 +00:00
|
|
|
archiveName="${if cfg.archiveBaseName == null then "" else cfg.archiveBaseName + "-"}$(date ${cfg.dateFormat})"
|
2018-03-12 19:20:24 +00:00
|
|
|
archiveSuffix="${optionalString cfg.appendFailedSuffix ".failed"}"
|
|
|
|
${cfg.preHook}
|
|
|
|
'' + optionalString cfg.doInit ''
|
|
|
|
# Run borg init if the repo doesn't exist yet
|
2018-05-24 08:25:52 +00:00
|
|
|
if ! borg list $extraArgs > /dev/null; then
|
|
|
|
borg init $extraArgs \
|
2018-03-12 19:20:24 +00:00
|
|
|
--encryption ${cfg.encryption.mode} \
|
|
|
|
$extraInitArgs
|
|
|
|
${cfg.postInit}
|
|
|
|
fi
|
|
|
|
'' + ''
|
2021-10-11 13:28:56 +00:00
|
|
|
(
|
|
|
|
set -o pipefail
|
|
|
|
${optionalString (cfg.dumpCommand != null) ''${escapeShellArg cfg.dumpCommand} | \''}
|
|
|
|
borg create $extraArgs \
|
|
|
|
--compression ${cfg.compression} \
|
|
|
|
--exclude-from ${mkExcludeFile cfg} \
|
2022-12-10 11:31:13 +00:00
|
|
|
--patterns-from ${mkPatternsFile cfg} \
|
2021-10-11 13:28:56 +00:00
|
|
|
$extraCreateArgs \
|
|
|
|
"::$archiveName$archiveSuffix" \
|
|
|
|
${if cfg.paths == null then "-" else escapeShellArgs cfg.paths}
|
|
|
|
)
|
2018-03-12 19:20:24 +00:00
|
|
|
'' + optionalString cfg.appendFailedSuffix ''
|
2018-05-24 08:25:52 +00:00
|
|
|
borg rename $extraArgs \
|
2018-05-03 14:18:56 +00:00
|
|
|
"::$archiveName$archiveSuffix" "$archiveName"
|
2018-03-12 19:20:24 +00:00
|
|
|
'' + ''
|
|
|
|
${cfg.postCreate}
|
|
|
|
'' + optionalString (cfg.prune.keep != { }) ''
|
2018-05-24 08:25:52 +00:00
|
|
|
borg prune $extraArgs \
|
2018-03-12 19:20:24 +00:00
|
|
|
${mkKeepArgs cfg} \
|
2022-12-10 15:46:34 +00:00
|
|
|
${optionalString (cfg.prune.prefix != null) "--glob-archives ${escapeShellArg "${cfg.prune.prefix}*"}"} \
|
2018-03-12 19:20:24 +00:00
|
|
|
$extraPruneArgs
|
|
|
|
${cfg.postPrune}
|
2022-10-28 02:57:28 +00:00
|
|
|
'');
|
2018-03-12 19:20:24 +00:00
|
|
|
|
|
|
|
mkPassEnv = cfg: with cfg.encryption;
|
|
|
|
if passCommand != null then
|
|
|
|
{ BORG_PASSCOMMAND = passCommand; }
|
|
|
|
else if passphrase != null then
|
|
|
|
{ BORG_PASSPHRASE = passphrase; }
|
|
|
|
else { };
|
|
|
|
|
2019-01-10 16:24:12 +00:00
|
|
|
mkBackupService = name: cfg:
|
2018-03-12 19:20:24 +00:00
|
|
|
let
|
|
|
|
userHome = config.users.users.${cfg.user}.home;
|
2022-10-28 02:57:28 +00:00
|
|
|
backupJobName = "borgbackup-job-${name}";
|
|
|
|
backupScript = mkBackupScript backupJobName cfg;
|
|
|
|
in nameValuePair backupJobName {
|
2018-03-12 19:20:24 +00:00
|
|
|
description = "BorgBackup job ${name}";
|
|
|
|
path = with pkgs; [
|
|
|
|
borgbackup openssh
|
|
|
|
];
|
2022-10-28 02:57:28 +00:00
|
|
|
script = "exec " + optionalString cfg.inhibitsSleep ''\
|
|
|
|
${pkgs.systemd}/bin/systemd-inhibit \
|
|
|
|
--who="borgbackup" \
|
|
|
|
--what="sleep" \
|
|
|
|
--why="Scheduled backup" \
|
|
|
|
'' + backupScript;
|
2018-03-12 19:20:24 +00:00
|
|
|
serviceConfig = {
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
# Only run when no other process is using CPU or disk
|
|
|
|
CPUSchedulingPolicy = "idle";
|
|
|
|
IOSchedulingClass = "idle";
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ReadWritePaths =
|
|
|
|
[ "${userHome}/.config/borg" "${userHome}/.cache/borg" ]
|
2018-05-03 14:18:56 +00:00
|
|
|
++ cfg.readWritePaths
|
2018-03-12 19:20:24 +00:00
|
|
|
# Borg needs write access to repo if it is not remote
|
|
|
|
++ optional (isLocalPath cfg.repo) cfg.repo;
|
2018-05-03 14:18:56 +00:00
|
|
|
PrivateTmp = cfg.privateTmp;
|
2018-03-12 19:20:24 +00:00
|
|
|
};
|
|
|
|
environment = {
|
|
|
|
BORG_REPO = cfg.repo;
|
2018-05-24 08:25:52 +00:00
|
|
|
inherit (cfg) extraArgs extraInitArgs extraCreateArgs extraPruneArgs;
|
2018-03-12 19:20:24 +00:00
|
|
|
} // (mkPassEnv cfg) // cfg.environment;
|
|
|
|
};
|
|
|
|
|
2021-05-18 01:54:13 +00:00
|
|
|
mkBackupTimers = name: cfg:
|
|
|
|
nameValuePair "borgbackup-job-${name}" {
|
|
|
|
description = "BorgBackup job ${name} timer";
|
2022-01-17 15:00:31 +00:00
|
|
|
wantedBy = [ "timers.target" ];
|
2021-05-18 01:54:13 +00:00
|
|
|
timerConfig = {
|
|
|
|
Persistent = cfg.persistentTimer;
|
2022-01-17 15:00:31 +00:00
|
|
|
OnCalendar = cfg.startAt;
|
2021-05-18 01:54:13 +00:00
|
|
|
};
|
2022-01-11 12:43:07 +00:00
|
|
|
# if remote-backup wait for network
|
|
|
|
after = optional (cfg.persistentTimer && !isLocalPath cfg.repo) "network-online.target";
|
2021-05-18 01:54:13 +00:00
|
|
|
};
|
|
|
|
|
2019-01-10 16:24:12 +00:00
|
|
|
# utility function around makeWrapper
|
|
|
|
mkWrapperDrv = {
|
|
|
|
original, name, set ? {}
|
|
|
|
}:
|
2021-08-15 15:12:23 +00:00
|
|
|
pkgs.runCommand "${name}-wrapper" {
|
2022-09-24 21:28:38 +00:00
|
|
|
nativeBuildInputs = [ pkgs.makeWrapper ];
|
2019-01-10 16:24:12 +00:00
|
|
|
} (with lib; ''
|
|
|
|
makeWrapper "${original}" "$out/bin/${name}" \
|
|
|
|
${concatStringsSep " \\\n " (mapAttrsToList (name: value: ''--set ${name} "${value}"'') set)}
|
|
|
|
'');
|
|
|
|
|
|
|
|
mkBorgWrapper = name: cfg: mkWrapperDrv {
|
|
|
|
original = "${pkgs.borgbackup}/bin/borg";
|
|
|
|
name = "borg-job-${name}";
|
|
|
|
set = { BORG_REPO = cfg.repo; } // (mkPassEnv cfg) // cfg.environment;
|
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
# Paths listed in ReadWritePaths must exist before service is started
|
|
|
|
mkActivationScript = name: cfg:
|
|
|
|
let
|
|
|
|
install = "install -o ${cfg.user} -g ${cfg.group}";
|
|
|
|
in
|
|
|
|
nameValuePair "borgbackup-job-${name}" (stringAfter [ "users" ] (''
|
2019-09-30 15:35:13 +00:00
|
|
|
# Ensure that the home directory already exists
|
2018-03-12 19:20:24 +00:00
|
|
|
# We can't assert createHome == true because that's not the case for root
|
2019-09-30 15:35:13 +00:00
|
|
|
cd "${config.users.users.${cfg.user}.home}"
|
2022-12-28 10:24:33 +00:00
|
|
|
# Create each directory separately to prevent root owned parent dirs
|
|
|
|
${install} -d .config .config/borg
|
|
|
|
${install} -d .cache .cache/borg
|
2019-09-30 15:35:13 +00:00
|
|
|
'' + optionalString (isLocalPath cfg.repo && !cfg.removableDevice) ''
|
2018-03-12 19:20:24 +00:00
|
|
|
${install} -d ${escapeShellArg cfg.repo}
|
|
|
|
''));
|
|
|
|
|
|
|
|
mkPassAssertion = name: cfg: {
|
|
|
|
assertion = with cfg.encryption;
|
|
|
|
mode != "none" -> passCommand != null || passphrase != null;
|
|
|
|
message =
|
|
|
|
"passCommand or passphrase has to be specified because"
|
|
|
|
+ '' borgbackup.jobs.${name}.encryption != "none"'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mkRepoService = name: cfg:
|
|
|
|
nameValuePair "borgbackup-repo-${name}" {
|
|
|
|
description = "Create BorgBackup repository ${name} directory";
|
|
|
|
script = ''
|
|
|
|
mkdir -p ${escapeShellArg cfg.path}
|
|
|
|
chown ${cfg.user}:${cfg.group} ${escapeShellArg cfg.path}
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
# The service's only task is to ensure that the specified path exists
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
mkAuthorizedKey = cfg: appendOnly: key:
|
|
|
|
let
|
|
|
|
# Because of the following line, clients do not need to specify an absolute repo path
|
|
|
|
cdCommand = "cd ${escapeShellArg cfg.path}";
|
|
|
|
restrictedArg = "--restrict-to-${if cfg.allowSubRepos then "path" else "repository"} .";
|
|
|
|
appendOnlyArg = optionalString appendOnly "--append-only";
|
|
|
|
quotaArg = optionalString (cfg.quota != null) "--storage-quota ${cfg.quota}";
|
|
|
|
serveCommand = "borg serve ${restrictedArg} ${appendOnlyArg} ${quotaArg}";
|
|
|
|
in
|
|
|
|
''command="${cdCommand} && ${serveCommand}",restrict ${key}'';
|
|
|
|
|
|
|
|
mkUsersConfig = name: cfg: {
|
|
|
|
users.${cfg.user} = {
|
|
|
|
openssh.authorizedKeys.keys =
|
|
|
|
(map (mkAuthorizedKey cfg false) cfg.authorizedKeys
|
|
|
|
++ map (mkAuthorizedKey cfg true) cfg.authorizedKeysAppendOnly);
|
|
|
|
useDefaultShell = true;
|
2021-08-08 12:00:00 +00:00
|
|
|
group = cfg.group;
|
2021-03-07 13:54:00 +00:00
|
|
|
isSystemUser = true;
|
2018-03-12 19:20:24 +00:00
|
|
|
};
|
|
|
|
groups.${cfg.group} = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
mkKeysAssertion = name: cfg: {
|
|
|
|
assertion = cfg.authorizedKeys != [ ] || cfg.authorizedKeysAppendOnly != [ ];
|
|
|
|
message =
|
|
|
|
"borgbackup.repos.${name} does not make sense"
|
|
|
|
+ " without at least one public key";
|
|
|
|
};
|
|
|
|
|
2021-10-11 13:28:56 +00:00
|
|
|
mkSourceAssertions = name: cfg: {
|
|
|
|
assertion = count isNull [ cfg.dumpCommand cfg.paths ] == 1;
|
|
|
|
message = ''
|
|
|
|
Exactly one of borgbackup.jobs.${name}.paths or borgbackup.jobs.${name}.dumpCommand
|
|
|
|
must be set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-09-30 15:35:13 +00:00
|
|
|
mkRemovableDeviceAssertions = name: cfg: {
|
|
|
|
assertion = !(isLocalPath cfg.repo) -> !cfg.removableDevice;
|
|
|
|
message = ''
|
|
|
|
borgbackup.repos.${name}: repo isn't a local path, thus it can't be a removable device!
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
in {
|
|
|
|
meta.maintainers = with maintainers; [ dotlambda ];
|
2023-01-24 23:33:40 +00:00
|
|
|
meta.doc = ./borgbackup.md;
|
2018-03-12 19:20:24 +00:00
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.services.borgbackup.jobs = mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-01-10 16:24:12 +00:00
|
|
|
Deduplicating backups using BorgBackup.
|
|
|
|
Adding a job will cause a borg-job-NAME wrapper to be added
|
|
|
|
to your system path, so that you can perform maintenance easily.
|
2020-03-31 07:38:59 +00:00
|
|
|
See also the chapter about BorgBackup in the NixOS manual.
|
2019-01-10 16:24:12 +00:00
|
|
|
'';
|
2018-03-12 19:20:24 +00:00
|
|
|
default = { };
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-03-31 07:38:59 +00:00
|
|
|
{ # for a local backup
|
2018-03-12 19:20:24 +00:00
|
|
|
rootBackup = {
|
|
|
|
paths = "/";
|
|
|
|
exclude = [ "/nix" ];
|
|
|
|
repo = "/path/to/local/repo";
|
|
|
|
encryption = {
|
|
|
|
mode = "repokey";
|
|
|
|
passphrase = "secret";
|
|
|
|
};
|
|
|
|
compression = "auto,lzma";
|
|
|
|
startAt = "weekly";
|
|
|
|
};
|
|
|
|
}
|
2020-03-31 07:38:59 +00:00
|
|
|
{ # Root backing each day up to a remote backup server. We assume that you have
|
2020-04-02 11:34:56 +00:00
|
|
|
# * created a password less key: ssh-keygen -N "" -t ed25519 -f /path/to/ssh_key
|
2020-03-31 07:38:59 +00:00
|
|
|
# best practices are: use -t ed25519, /path/to = /run/keys
|
|
|
|
# * the passphrase is in the file /run/keys/borgbackup_passphrase
|
|
|
|
# * you have initialized the repository manually
|
|
|
|
paths = [ "/etc" "/home" ];
|
|
|
|
exclude = [ "/nix" "'**/.cache'" ];
|
|
|
|
doInit = false;
|
|
|
|
repo = "user3@arep.repo.borgbase.com:repo";
|
|
|
|
encryption = {
|
|
|
|
mode = "repokey-blake2";
|
|
|
|
passCommand = "cat /path/to/passphrase";
|
|
|
|
};
|
|
|
|
environment = { BORG_RSH = "ssh -i /path/to/ssh_key"; };
|
|
|
|
compression = "auto,lzma";
|
|
|
|
startAt = "daily";
|
|
|
|
};
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
type = types.attrsOf (types.submodule (let globalConfig = config; in
|
|
|
|
{ name, config, ... }: {
|
|
|
|
options = {
|
|
|
|
|
|
|
|
paths = mkOption {
|
2021-10-11 13:28:56 +00:00
|
|
|
type = with types; nullOr (coercedTo str lib.singleton (listOf str));
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-11 13:28:56 +00:00
|
|
|
Path(s) to back up.
|
2022-07-28 21:19:15 +00:00
|
|
|
Mutually exclusive with {option}`dumpCommand`.
|
2021-10-11 13:28:56 +00:00
|
|
|
'';
|
2018-03-12 19:20:24 +00:00
|
|
|
example = "/home/user";
|
|
|
|
};
|
|
|
|
|
2021-10-11 13:28:56 +00:00
|
|
|
dumpCommand = mkOption {
|
|
|
|
type = with types; nullOr path;
|
|
|
|
default = null;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-11 13:28:56 +00:00
|
|
|
Backup the stdout of this program instead of filesystem paths.
|
2022-07-28 21:19:15 +00:00
|
|
|
Mutually exclusive with {option}`paths`.
|
2021-10-11 13:28:56 +00:00
|
|
|
'';
|
|
|
|
example = "/path/to/createZFSsend.sh";
|
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
repo = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Remote or local repository to back up to.";
|
2018-03-12 19:20:24 +00:00
|
|
|
example = "user@machine:/path/to/repo";
|
|
|
|
};
|
|
|
|
|
2019-09-30 15:35:13 +00:00
|
|
|
removableDevice = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Whether the repo (which must be local) is a removable device.";
|
2019-09-30 15:35:13 +00:00
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
archiveBaseName = mkOption {
|
2022-01-08 22:45:07 +00:00
|
|
|
type = types.nullOr (types.strMatching "[^/{}]+");
|
2018-03-12 19:20:24 +00:00
|
|
|
default = "${globalConfig.networking.hostName}-${name}";
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression ''"''${config.networking.hostName}-<name>"'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
How to name the created archives. A timestamp, whose format is
|
2022-07-28 21:19:15 +00:00
|
|
|
determined by {option}`dateFormat`, will be appended. The full
|
|
|
|
name can be modified at runtime (`$archiveName`).
|
|
|
|
Placeholders like `{hostname}` must not be used.
|
|
|
|
Use `null` for no base name.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dateFormat = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Arguments passed to {command}`date`
|
2018-03-12 19:20:24 +00:00
|
|
|
to create a timestamp suffix for the archive name.
|
|
|
|
'';
|
|
|
|
default = "+%Y-%m-%dT%H:%M:%S";
|
|
|
|
example = "-u +%s";
|
|
|
|
};
|
|
|
|
|
|
|
|
startAt = mkOption {
|
|
|
|
type = with types; either str (listOf str);
|
|
|
|
default = "daily";
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
When or how often the backup should run.
|
|
|
|
Must be in the format described in
|
2022-08-05 17:39:00 +00:00
|
|
|
{manpage}`systemd.time(7)`.
|
2018-03-12 19:20:24 +00:00
|
|
|
If you do not want the backup to start
|
2022-08-05 17:39:00 +00:00
|
|
|
automatically, use `[ ]`.
|
2020-03-31 07:38:59 +00:00
|
|
|
It will generate a systemd service borgbackup-job-NAME.
|
|
|
|
You may trigger it manually via systemctl restart borgbackup-job-NAME.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-05-18 01:54:13 +00:00
|
|
|
persistentTimer = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
example = true;
|
2022-08-05 17:39:00 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Set the `persistentTimer` option for the
|
|
|
|
{manpage}`systemd.timer(5)`
|
2021-05-18 01:54:13 +00:00
|
|
|
which triggers the backup immediately if the last trigger
|
|
|
|
was missed (e.g. if the system was powered down).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-10-28 02:57:28 +00:00
|
|
|
inhibitsSleep = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
example = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Prevents the system from sleeping while backing up.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The user {command}`borg` is run as.
|
2018-03-12 19:20:24 +00:00
|
|
|
User or group need read permission
|
2022-07-28 21:19:15 +00:00
|
|
|
for the specified {option}`paths`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "root";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
The group borg is run as. User or group needs read permission
|
2022-07-28 21:19:15 +00:00
|
|
|
for the specified {option}`paths`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "root";
|
|
|
|
};
|
|
|
|
|
|
|
|
encryption.mode = mkOption {
|
|
|
|
type = types.enum [
|
|
|
|
"repokey" "keyfile"
|
|
|
|
"repokey-blake2" "keyfile-blake2"
|
|
|
|
"authenticated" "authenticated-blake2"
|
|
|
|
"none"
|
|
|
|
];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Encryption mode to use. Setting a mode
|
2022-07-28 21:19:15 +00:00
|
|
|
other than `"none"` requires
|
|
|
|
you to specify a {option}`passCommand`
|
|
|
|
or a {option}`passphrase`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
2021-10-03 16:06:03 +00:00
|
|
|
example = "repokey-blake2";
|
2018-03-12 19:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
encryption.passCommand = mkOption {
|
|
|
|
type = with types; nullOr str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
A command which prints the passphrase to stdout.
|
2022-07-28 21:19:15 +00:00
|
|
|
Mutually exclusive with {option}`passphrase`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = "cat /path/to/passphrase_file";
|
|
|
|
};
|
|
|
|
|
|
|
|
encryption.passphrase = mkOption {
|
|
|
|
type = with types; nullOr str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
The passphrase the backups are encrypted with.
|
2022-07-28 21:19:15 +00:00
|
|
|
Mutually exclusive with {option}`passCommand`.
|
2018-03-12 19:20:24 +00:00
|
|
|
If you do not want the passphrase to be stored in the
|
2022-07-28 21:19:15 +00:00
|
|
|
world-readable Nix store, use {option}`passCommand`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
compression = mkOption {
|
|
|
|
# "auto" is optional,
|
|
|
|
# compression mode must be given,
|
|
|
|
# compression level is optional
|
|
|
|
type = types.strMatching "none|(auto,)?(lz4|zstd|zlib|lzma)(,[[:digit:]]{1,2})?";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Compression method to use. Refer to
|
2022-07-28 21:19:15 +00:00
|
|
|
{command}`borg help compression`
|
2018-03-12 19:20:24 +00:00
|
|
|
for all available options.
|
|
|
|
'';
|
|
|
|
default = "lz4";
|
|
|
|
example = "auto,lzma";
|
|
|
|
};
|
|
|
|
|
|
|
|
exclude = mkOption {
|
|
|
|
type = with types; listOf str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Exclude paths matching any of the given patterns. See
|
2022-07-28 21:19:15 +00:00
|
|
|
{command}`borg help patterns` for pattern syntax.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"/home/*/.cache"
|
|
|
|
"/nix"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-12-10 11:31:13 +00:00
|
|
|
patterns = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Include/exclude paths matching the given patterns. The first
|
|
|
|
matching patterns is used, so if an include pattern (prefix `+`)
|
|
|
|
matches before an exclude pattern (prefix `-`), the file is
|
|
|
|
backed up. See [{command}`borg help patterns`](https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-patterns) for pattern syntax.
|
|
|
|
'';
|
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"+ /home/susan"
|
|
|
|
"- /home/*"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2018-05-03 14:18:56 +00:00
|
|
|
readWritePaths = mkOption {
|
|
|
|
type = with types; listOf path;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-05-03 14:18:56 +00:00
|
|
|
By default, borg cannot write anywhere on the system but
|
2022-07-28 21:19:15 +00:00
|
|
|
`$HOME/.config/borg` and `$HOME/.cache/borg`.
|
2018-05-03 14:18:56 +00:00
|
|
|
If, for example, your preHook script needs to dump files
|
|
|
|
somewhere, put those directories here.
|
|
|
|
'';
|
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"/var/backup/mysqldump"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
privateTmp = mkOption {
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Set the `PrivateTmp` option for
|
2018-05-03 14:18:56 +00:00
|
|
|
the systemd-service. Set to false if you need sockets
|
|
|
|
or other files from global /tmp.
|
|
|
|
'';
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
doInit = mkOption {
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Run {command}`borg init` if the
|
|
|
|
specified {option}`repo` does not exist.
|
|
|
|
You should set this to `false`
|
2018-03-12 19:20:24 +00:00
|
|
|
if the repository is located on an external drive
|
|
|
|
that might not always be mounted.
|
|
|
|
'';
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
appendFailedSuffix = mkOption {
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Append a `.failed` suffix
|
2018-03-12 19:20:24 +00:00
|
|
|
to the archive name, which is only removed if
|
2022-07-28 21:19:15 +00:00
|
|
|
{command}`borg create` has a zero exit status.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
prune.keep = mkOption {
|
|
|
|
# Specifying e.g. `prune.keep.yearly = -1`
|
|
|
|
# means there is no limit of yearly archives to keep
|
|
|
|
# The regex is for use with e.g. --keep-within 1y
|
|
|
|
type = with types; attrsOf (either int (strMatching "[[:digit:]]+[Hdwmy]"));
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Prune a repository by deleting all archives not matching any of the
|
2022-07-28 21:19:15 +00:00
|
|
|
specified retention options. See {command}`borg help prune`
|
2018-03-12 19:20:24 +00:00
|
|
|
for the available options.
|
|
|
|
'';
|
|
|
|
default = { };
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2018-03-12 19:20:24 +00:00
|
|
|
{
|
|
|
|
within = "1d"; # Keep all archives from the last day
|
|
|
|
daily = 7;
|
|
|
|
weekly = 4;
|
|
|
|
monthly = -1; # Keep at least one archive for each month
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
prune.prefix = mkOption {
|
2022-01-08 22:45:07 +00:00
|
|
|
type = types.nullOr (types.str);
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Only consider archive names starting with this prefix for pruning.
|
|
|
|
By default, only archives created by this job are considered.
|
2022-07-28 21:19:15 +00:00
|
|
|
Use `""` or `null` to consider all archives.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = config.archiveBaseName;
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "archiveBaseName";
|
2018-03-12 19:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
environment = mkOption {
|
|
|
|
type = with types; attrsOf str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Environment variables passed to the backup script.
|
|
|
|
You can for example specify which SSH key to use.
|
|
|
|
'';
|
|
|
|
default = { };
|
|
|
|
example = { BORG_RSH = "ssh -i /path/to/key"; };
|
|
|
|
};
|
|
|
|
|
|
|
|
preHook = mkOption {
|
|
|
|
type = types.lines;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Shell commands to run before the backup.
|
|
|
|
This can for example be used to mount file systems.
|
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
# To add excluded paths at runtime
|
|
|
|
extraCreateArgs="$extraCreateArgs --exclude /some/path"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
postInit = mkOption {
|
|
|
|
type = types.lines;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Shell commands to run after {command}`borg init`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
postCreate = mkOption {
|
|
|
|
type = types.lines;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Shell commands to run after {command}`borg create`. The name
|
|
|
|
of the created archive is stored in `$archiveName`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
postPrune = mkOption {
|
|
|
|
type = types.lines;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Shell commands to run after {command}`borg prune`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
postHook = mkOption {
|
|
|
|
type = types.lines;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Shell commands to run just before exit. They are executed
|
|
|
|
even if a previous command exits with a non-zero exit code.
|
2022-07-28 21:19:15 +00:00
|
|
|
The latter is available as `$exitStatus`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
2018-05-03 14:18:56 +00:00
|
|
|
extraArgs = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional arguments for all {command}`borg` calls the
|
2018-05-03 14:18:56 +00:00
|
|
|
service has. Handle with care.
|
|
|
|
'';
|
|
|
|
default = "";
|
2018-05-24 08:25:52 +00:00
|
|
|
example = "--remote-path=/path/to/borg";
|
2018-05-03 14:18:56 +00:00
|
|
|
};
|
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
extraInitArgs = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional arguments for {command}`borg init`.
|
|
|
|
Can also be set at runtime using `$extraInitArgs`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
example = "--append-only";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraCreateArgs = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional arguments for {command}`borg create`.
|
|
|
|
Can also be set at runtime using `$extraCreateArgs`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
example = "--stats --checkpoint-interval 600";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraPruneArgs = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Additional arguments for {command}`borg prune`.
|
|
|
|
Can also be set at runtime using `$extraPruneArgs`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
example = "--save-space";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
options.services.borgbackup.repos = mkOption {
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Serve BorgBackup repositories to given public SSH keys,
|
|
|
|
restricting their access to the repository only.
|
2020-03-31 07:38:59 +00:00
|
|
|
See also the chapter about BorgBackup in the NixOS manual.
|
2018-03-12 19:20:24 +00:00
|
|
|
Also, clients do not need to specify the absolute path when accessing the repository,
|
2022-07-28 21:19:15 +00:00
|
|
|
i.e. `user@machine:.` is enough. (Note colon and dot.)
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = { };
|
|
|
|
type = types.attrsOf (types.submodule (
|
2018-07-20 20:56:59 +00:00
|
|
|
{ ... }: {
|
2018-03-12 19:20:24 +00:00
|
|
|
options = {
|
|
|
|
path = mkOption {
|
|
|
|
type = types.path;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Where to store the backups. Note that the directory
|
|
|
|
is created automatically, with correct permissions.
|
|
|
|
'';
|
|
|
|
default = "/var/lib/borgbackup";
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The user {command}`borg serve` is run as.
|
2018-03-12 19:20:24 +00:00
|
|
|
User or group needs write permission
|
2022-07-28 21:19:15 +00:00
|
|
|
for the specified {option}`path`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "borg";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The group {command}`borg serve` is run as.
|
2018-03-12 19:20:24 +00:00
|
|
|
User or group needs write permission
|
2022-07-28 21:19:15 +00:00
|
|
|
for the specified {option}`path`.
|
2018-03-12 19:20:24 +00:00
|
|
|
'';
|
|
|
|
default = "borg";
|
|
|
|
};
|
|
|
|
|
|
|
|
authorizedKeys = mkOption {
|
|
|
|
type = with types; listOf str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Public SSH keys that are given full write access to this repository.
|
|
|
|
You should use a different SSH key for each repository you write to, because
|
2022-07-28 21:19:15 +00:00
|
|
|
the specified keys are restricted to running {command}`borg serve`
|
2018-03-12 19:20:24 +00:00
|
|
|
and can only access this single repository.
|
|
|
|
'';
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
authorizedKeysAppendOnly = mkOption {
|
|
|
|
type = with types; listOf str;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Public SSH keys that can only be used to append new data (archives) to the repository.
|
|
|
|
Note that archives can still be marked as deleted and are subsequently removed from disk
|
|
|
|
upon accessing the repo with full write access, e.g. when pruning.
|
|
|
|
'';
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
allowSubRepos = mkOption {
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Allow clients to create repositories in subdirectories of the
|
2022-07-28 21:19:15 +00:00
|
|
|
specified {option}`path`. These can be accessed using
|
|
|
|
`user@machine:path/to/subrepo`. Note that a
|
|
|
|
{option}`quota` applies to repositories independently.
|
2018-03-12 19:20:24 +00:00
|
|
|
Therefore, if this is enabled, clients can create multiple
|
|
|
|
repositories and upload an arbitrary amount of data.
|
|
|
|
'';
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
quota = mkOption {
|
|
|
|
# See the definition of parse_file_size() in src/borg/helpers/parseformat.py
|
|
|
|
type = with types; nullOr (strMatching "[[:digit:].]+[KMGTP]?");
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2018-03-12 19:20:24 +00:00
|
|
|
Storage quota for the repository. This quota is ensured for all
|
2022-07-28 21:19:15 +00:00
|
|
|
sub-repositories if {option}`allowSubRepos` is enabled
|
2018-03-12 19:20:24 +00:00
|
|
|
but not for the overall storage space used.
|
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = "100G";
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf (with config.services.borgbackup; jobs != { } || repos != { })
|
|
|
|
(with config.services.borgbackup; {
|
|
|
|
assertions =
|
|
|
|
mapAttrsToList mkPassAssertion jobs
|
2019-09-30 15:35:13 +00:00
|
|
|
++ mapAttrsToList mkKeysAssertion repos
|
2021-10-11 13:28:56 +00:00
|
|
|
++ mapAttrsToList mkSourceAssertions jobs
|
2019-09-30 15:35:13 +00:00
|
|
|
++ mapAttrsToList mkRemovableDeviceAssertions jobs;
|
2018-03-12 19:20:24 +00:00
|
|
|
|
|
|
|
system.activationScripts = mapAttrs' mkActivationScript jobs;
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
# A job named "foo" is mapped to systemd.services.borgbackup-job-foo
|
|
|
|
mapAttrs' mkBackupService jobs
|
|
|
|
# A repo named "foo" is mapped to systemd.services.borgbackup-repo-foo
|
|
|
|
// mapAttrs' mkRepoService repos;
|
|
|
|
|
2021-05-18 01:54:13 +00:00
|
|
|
# A job named "foo" is mapped to systemd.timers.borgbackup-job-foo
|
2022-01-17 15:00:31 +00:00
|
|
|
# only generate the timer if interval (startAt) is set
|
|
|
|
systemd.timers = mapAttrs' mkBackupTimers (filterAttrs (_: cfg: cfg.startAt != []) jobs);
|
2021-05-18 01:54:13 +00:00
|
|
|
|
2018-03-12 19:20:24 +00:00
|
|
|
users = mkMerge (mapAttrsToList mkUsersConfig repos);
|
|
|
|
|
2019-01-10 16:24:12 +00:00
|
|
|
environment.systemPackages = with pkgs; [ borgbackup ] ++ (mapAttrsToList mkBorgWrapper jobs);
|
2018-03-12 19:20:24 +00:00
|
|
|
});
|
|
|
|
}
|