2021-11-20 17:34:13 +00:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2018-04-21 10:12:43 +00:00
|
|
|
|
|
|
|
with lib;
|
2019-01-23 20:29:02 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
# Type for a valid systemd unit option. Needed for correctly passing "timerConfig" to "systemd.timers"
|
2021-11-20 17:34:13 +00:00
|
|
|
inherit (utils.systemdUtils.unitOptions) unitOption;
|
2019-01-23 20:29:02 +00:00
|
|
|
in
|
2018-04-21 10:12:43 +00:00
|
|
|
{
|
|
|
|
options.services.restic.backups = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Periodic backups to create with Restic.
|
|
|
|
'';
|
2021-08-28 13:17:52 +00:00
|
|
|
type = types.attrsOf (types.submodule ({ config, name, ... }: {
|
2018-04-21 10:12:43 +00:00
|
|
|
options = {
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Read the repository password from a file.
|
|
|
|
'';
|
|
|
|
example = "/etc/nixos/restic-password";
|
2018-05-31 02:30:12 +00:00
|
|
|
};
|
2018-04-21 10:12:43 +00:00
|
|
|
|
2021-08-28 13:17:52 +00:00
|
|
|
environmentFile = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
# added on 2021-08-28, s3CredentialsFile should
|
|
|
|
# be removed in the future (+ remember the warning)
|
|
|
|
default = config.s3CredentialsFile;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
file containing the credentials to access the repository, in the
|
|
|
|
format of an EnvironmentFile as described by systemd.exec(5)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-05-31 02:30:12 +00:00
|
|
|
s3CredentialsFile = mkOption {
|
|
|
|
type = with types; nullOr str;
|
2018-08-02 02:52:41 +00:00
|
|
|
default = null;
|
2018-05-31 02:30:12 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
file containing the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
|
|
|
|
for an S3-hosted repository, in the format of an EnvironmentFile
|
|
|
|
as described by systemd.exec(5)
|
|
|
|
'';
|
2018-04-21 10:12:43 +00:00
|
|
|
};
|
|
|
|
|
2020-07-05 20:25:33 +00:00
|
|
|
rcloneOptions = mkOption {
|
|
|
|
type = with types; nullOr (attrsOf (oneOf [ str bool ]));
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Options to pass to rclone to control its behavior.
|
|
|
|
See <https://rclone.org/docs/#options> for
|
|
|
|
available options. When specifying option names, strip the
|
|
|
|
leading `--`. To set a flag such as
|
|
|
|
`--drive-use-trash`, which does not take a value,
|
|
|
|
set the value to the Boolean `true`.
|
|
|
|
'';
|
|
|
|
example = {
|
|
|
|
bwlimit = "10M";
|
|
|
|
drive-use-trash = "true";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
rcloneConfig = mkOption {
|
|
|
|
type = with types; nullOr (attrsOf (oneOf [ str bool ]));
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Configuration for the rclone remote being used for backup.
|
|
|
|
See the remote's specific options under rclone's docs at
|
|
|
|
<https://rclone.org/docs/>. When specifying
|
2020-08-07 13:43:58 +00:00
|
|
|
option names, use the "config" name specified in the docs.
|
2020-07-05 20:25:33 +00:00
|
|
|
For example, to set `--b2-hard-delete` for a B2
|
|
|
|
remote, use `hard_delete = true` in the
|
|
|
|
attribute set.
|
|
|
|
Warning: Secrets set in here will be world-readable in the Nix
|
|
|
|
store! Consider using the `rcloneConfigFile`
|
|
|
|
option instead to specify secret values separately. Note that
|
|
|
|
options set here will override those set in the config file.
|
|
|
|
'';
|
|
|
|
example = {
|
|
|
|
type = "b2";
|
|
|
|
account = "xxx";
|
|
|
|
key = "xxx";
|
|
|
|
hard_delete = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
rcloneConfigFile = mkOption {
|
|
|
|
type = with types; nullOr path;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Path to the file containing rclone configuration. This file
|
|
|
|
must contain configuration for the remote specified in this backup
|
|
|
|
set and also must be readable by root. Options set in
|
|
|
|
`rcloneConfig` will override those set in this
|
|
|
|
file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-04-21 10:12:43 +00:00
|
|
|
repository = mkOption {
|
2022-06-02 16:40:39 +00:00
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
2018-04-21 10:12:43 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
repository to backup to.
|
|
|
|
'';
|
|
|
|
example = "sftp:backup@192.168.1.100:/backups/${name}";
|
|
|
|
};
|
|
|
|
|
2022-06-02 16:40:39 +00:00
|
|
|
repositoryFile = mkOption {
|
|
|
|
type = with types; nullOr path;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Path to the file containing the repository location to backup to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-04-21 10:12:43 +00:00
|
|
|
paths = mkOption {
|
2021-03-20 00:14:43 +00:00
|
|
|
type = types.nullOr (types.listOf types.str);
|
|
|
|
default = null;
|
2018-04-21 10:12:43 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-03-20 00:14:43 +00:00
|
|
|
Which paths to backup. If null or an empty array, no
|
|
|
|
backup command will be run. This can be used to create a
|
|
|
|
prune-only job.
|
2018-04-21 10:12:43 +00:00
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"/var/lib/postgresql"
|
|
|
|
"/home/user/backup"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-12-03 23:00:58 +00:00
|
|
|
exclude = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Patterns to exclude when backing up. See
|
|
|
|
https://restic.readthedocs.io/en/latest/040_backup.html#excluding-files for
|
|
|
|
details on syntax.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"/var/cache"
|
|
|
|
"/home/*/.cache"
|
|
|
|
".git"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2018-04-21 10:12:43 +00:00
|
|
|
timerConfig = mkOption {
|
2019-01-23 20:29:02 +00:00
|
|
|
type = types.attrsOf unitOption;
|
2018-04-21 10:12:43 +00:00
|
|
|
default = {
|
|
|
|
OnCalendar = "daily";
|
2023-05-15 08:32:14 +00:00
|
|
|
Persistent = true;
|
2018-04-21 10:12:43 +00:00
|
|
|
};
|
|
|
|
description = lib.mdDoc ''
|
2022-12-31 11:19:48 +00:00
|
|
|
When to run the backup. See {manpage}`systemd.timer(5)` for details.
|
2018-04-21 10:12:43 +00:00
|
|
|
'';
|
|
|
|
example = {
|
|
|
|
OnCalendar = "00:05";
|
|
|
|
RandomizedDelaySec = "5h";
|
2023-05-15 08:32:14 +00:00
|
|
|
Persistent = true;
|
2018-04-21 10:12:43 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "root";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
As which user the backup should run.
|
|
|
|
'';
|
|
|
|
example = "postgresql";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraBackupArgs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-06-02 16:38:39 +00:00
|
|
|
default = [ ];
|
2018-04-21 10:12:43 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra arguments passed to restic backup.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"--exclude-file=/etc/nixos/restic-ignore"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
extraOptions = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-06-02 16:38:39 +00:00
|
|
|
default = [ ];
|
2018-04-21 10:12:43 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra extended options to be passed to the restic --option flag.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"sftp.command='ssh backup@192.168.1.100 -i /home/user/.ssh/id_rsa -s sftp'"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
initialize = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Create the repository if it doesn't exist.
|
|
|
|
'';
|
|
|
|
};
|
2018-07-22 02:24:19 +00:00
|
|
|
|
|
|
|
pruneOpts = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2022-06-02 16:38:39 +00:00
|
|
|
default = [ ];
|
2022-08-29 19:48:56 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
A list of options (--keep-\* et al.) for 'restic forget
|
2018-07-22 02:24:19 +00:00
|
|
|
--prune', to automatically prune old snapshots. The
|
|
|
|
'forget' command is run *after* the 'backup' command, so
|
2022-08-29 19:48:56 +00:00
|
|
|
keep that in mind when constructing the --keep-\* options.
|
2018-07-22 02:24:19 +00:00
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"--keep-daily 7"
|
|
|
|
"--keep-weekly 5"
|
|
|
|
"--keep-monthly 12"
|
|
|
|
"--keep-yearly 75"
|
|
|
|
];
|
|
|
|
};
|
2018-08-05 16:53:53 +00:00
|
|
|
|
2022-08-29 23:40:28 +00:00
|
|
|
checkOpts = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A list of options for 'restic check', which is run after
|
|
|
|
pruning.
|
|
|
|
'';
|
|
|
|
example = [
|
|
|
|
"--with-cache"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2018-08-05 16:53:53 +00:00
|
|
|
dynamicFilesFrom = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A script that produces a list of files to back up. The
|
|
|
|
results of this command are given to the '--files-from'
|
|
|
|
option.
|
|
|
|
'';
|
|
|
|
example = "find /home/matt/git -type d -name .git";
|
|
|
|
};
|
2022-06-02 16:43:03 +00:00
|
|
|
|
|
|
|
backupPrepareCommand = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A script that must run before starting the backup process.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
backupCleanupCommand = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
A script that must run after finishing the backup process.
|
|
|
|
'';
|
|
|
|
};
|
2022-07-27 17:47:41 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.restic;
|
|
|
|
defaultText = literalExpression "pkgs.restic";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Restic package to use.
|
|
|
|
'';
|
|
|
|
};
|
2018-04-21 10:12:43 +00:00
|
|
|
};
|
|
|
|
}));
|
2022-06-02 16:38:39 +00:00
|
|
|
default = { };
|
2018-04-21 10:12:43 +00:00
|
|
|
example = {
|
|
|
|
localbackup = {
|
|
|
|
paths = [ "/home" ];
|
2022-12-03 23:00:58 +00:00
|
|
|
exclude = [ "/home/*/.cache" ];
|
2018-04-21 10:12:43 +00:00
|
|
|
repository = "/mnt/backup-hdd";
|
|
|
|
passwordFile = "/etc/nixos/secrets/restic-password";
|
|
|
|
initialize = true;
|
|
|
|
};
|
|
|
|
remotebackup = {
|
|
|
|
paths = [ "/home" ];
|
|
|
|
repository = "sftp:backup@host:/backups/home";
|
|
|
|
passwordFile = "/etc/nixos/secrets/restic-password";
|
|
|
|
extraOptions = [
|
|
|
|
"sftp.command='ssh backup@host -i /etc/nixos/secrets/backup-private-key -s sftp'"
|
|
|
|
];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "00:05";
|
|
|
|
RandomizedDelaySec = "5h";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2021-08-28 13:17:52 +00:00
|
|
|
warnings = mapAttrsToList (n: v: "services.restic.backups.${n}.s3CredentialsFile is deprecated, please use services.restic.backups.${n}.environmentFile instead.") (filterAttrs (n: v: v.s3CredentialsFile != null) config.services.restic.backups);
|
2022-12-03 21:48:44 +00:00
|
|
|
assertions = mapAttrsToList (n: v: {
|
|
|
|
assertion = (v.repository == null) != (v.repositoryFile == null);
|
|
|
|
message = "services.restic.backups.${n}: exactly one of repository or repositoryFile should be set";
|
|
|
|
}) config.services.restic.backups;
|
2018-04-21 10:12:43 +00:00
|
|
|
systemd.services =
|
2022-06-02 16:38:39 +00:00
|
|
|
mapAttrs'
|
|
|
|
(name: backup:
|
|
|
|
let
|
|
|
|
extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
|
2022-07-27 17:47:41 +00:00
|
|
|
resticCmd = "${backup.package}/bin/restic${extraOptions}";
|
2023-06-25 09:47:43 +00:00
|
|
|
excludeFlags = optional (backup.exclude != []) "--exclude-file=${pkgs.writeText "exclude-patterns" (concatStringsSep "\n" backup.exclude)}";
|
2022-06-02 16:38:39 +00:00
|
|
|
filesFromTmpFile = "/run/restic-backups-${name}/includes";
|
|
|
|
backupPaths =
|
|
|
|
if (backup.dynamicFilesFrom == null)
|
2023-03-19 20:44:31 +00:00
|
|
|
then optionalString (backup.paths != null) (concatStringsSep " " backup.paths)
|
2022-06-02 16:38:39 +00:00
|
|
|
else "--files-from ${filesFromTmpFile}";
|
|
|
|
pruneCmd = optionals (builtins.length backup.pruneOpts > 0) [
|
2023-03-11 11:23:24 +00:00
|
|
|
(resticCmd + " forget --prune " + (concatStringsSep " " backup.pruneOpts))
|
|
|
|
(resticCmd + " check " + (concatStringsSep " " backup.checkOpts))
|
2022-06-02 16:38:39 +00:00
|
|
|
];
|
|
|
|
# Helper functions for rclone remotes
|
|
|
|
rcloneRemoteName = builtins.elemAt (splitString ":" backup.repository) 1;
|
|
|
|
rcloneAttrToOpt = v: "RCLONE_" + toUpper (builtins.replaceStrings [ "-" ] [ "_" ] v);
|
|
|
|
rcloneAttrToConf = v: "RCLONE_CONFIG_" + toUpper (rcloneRemoteName + "_" + v);
|
|
|
|
toRcloneVal = v: if lib.isBool v then lib.boolToString v else v;
|
|
|
|
in
|
|
|
|
nameValuePair "restic-backups-${name}" ({
|
|
|
|
environment = {
|
2023-03-11 11:23:24 +00:00
|
|
|
RESTIC_CACHE_DIR = "%C/restic-backups-${name}";
|
2022-06-02 16:38:39 +00:00
|
|
|
RESTIC_PASSWORD_FILE = backup.passwordFile;
|
|
|
|
RESTIC_REPOSITORY = backup.repository;
|
2022-06-02 16:40:39 +00:00
|
|
|
RESTIC_REPOSITORY_FILE = backup.repositoryFile;
|
2022-06-02 16:38:39 +00:00
|
|
|
} // optionalAttrs (backup.rcloneOptions != null) (mapAttrs'
|
|
|
|
(name: value:
|
|
|
|
nameValuePair (rcloneAttrToOpt name) (toRcloneVal value)
|
|
|
|
)
|
|
|
|
backup.rcloneOptions) // optionalAttrs (backup.rcloneConfigFile != null) {
|
|
|
|
RCLONE_CONFIG = backup.rcloneConfigFile;
|
|
|
|
} // optionalAttrs (backup.rcloneConfig != null) (mapAttrs'
|
|
|
|
(name: value:
|
|
|
|
nameValuePair (rcloneAttrToConf name) (toRcloneVal value)
|
|
|
|
)
|
|
|
|
backup.rcloneConfig);
|
|
|
|
path = [ pkgs.openssh ];
|
|
|
|
restartIfChanged = false;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2023-03-11 11:23:24 +00:00
|
|
|
ExecStart = (optionals (backupPaths != "") [ "${resticCmd} backup ${concatStringsSep " " (backup.extraBackupArgs ++ excludeFlags)} ${backupPaths}" ])
|
2022-06-02 16:38:39 +00:00
|
|
|
++ pruneCmd;
|
|
|
|
User = backup.user;
|
|
|
|
RuntimeDirectory = "restic-backups-${name}";
|
|
|
|
CacheDirectory = "restic-backups-${name}";
|
|
|
|
CacheDirectoryMode = "0700";
|
2020-10-06 11:32:58 +00:00
|
|
|
PrivateTmp = true;
|
2022-06-02 16:38:39 +00:00
|
|
|
} // optionalAttrs (backup.environmentFile != null) {
|
|
|
|
EnvironmentFile = backup.environmentFile;
|
|
|
|
};
|
2022-06-02 16:43:03 +00:00
|
|
|
} // optionalAttrs (backup.initialize || backup.dynamicFilesFrom != null || backup.backupPrepareCommand != null) {
|
2022-06-02 16:38:39 +00:00
|
|
|
preStart = ''
|
2022-06-02 16:43:03 +00:00
|
|
|
${optionalString (backup.backupPrepareCommand != null) ''
|
|
|
|
${pkgs.writeScript "backupPrepareCommand" backup.backupPrepareCommand}
|
|
|
|
''}
|
2022-06-02 16:38:39 +00:00
|
|
|
${optionalString (backup.initialize) ''
|
|
|
|
${resticCmd} snapshots || ${resticCmd} init
|
|
|
|
''}
|
|
|
|
${optionalString (backup.dynamicFilesFrom != null) ''
|
|
|
|
${pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom} > ${filesFromTmpFile}
|
|
|
|
''}
|
|
|
|
'';
|
2022-06-02 16:43:03 +00:00
|
|
|
} // optionalAttrs (backup.dynamicFilesFrom != null || backup.backupCleanupCommand != null) {
|
2022-07-25 14:53:01 +00:00
|
|
|
postStop = ''
|
2022-06-02 16:43:03 +00:00
|
|
|
${optionalString (backup.backupCleanupCommand != null) ''
|
|
|
|
${pkgs.writeScript "backupCleanupCommand" backup.backupCleanupCommand}
|
|
|
|
''}
|
|
|
|
${optionalString (backup.dynamicFilesFrom != null) ''
|
|
|
|
rm ${filesFromTmpFile}
|
|
|
|
''}
|
2022-06-02 16:38:39 +00:00
|
|
|
'';
|
|
|
|
})
|
|
|
|
)
|
|
|
|
config.services.restic.backups;
|
2018-04-21 10:12:43 +00:00
|
|
|
systemd.timers =
|
2022-06-02 16:38:39 +00:00
|
|
|
mapAttrs'
|
|
|
|
(name: backup: nameValuePair "restic-backups-${name}" {
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = backup.timerConfig;
|
|
|
|
})
|
|
|
|
config.services.restic.backups;
|
2018-04-21 10:12:43 +00:00
|
|
|
};
|
|
|
|
}
|