2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, utils, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
gcfg = config.services.tarsnap;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.tarsnap;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
configFile = name: cfg: ''
|
|
|
|
keyfile ${cfg.keyfile}
|
|
|
|
${optionalString (cfg.cachedir != null) "cachedir ${cfg.cachedir}"}
|
|
|
|
${optionalString cfg.nodump "nodump"}
|
|
|
|
${optionalString cfg.printStats "print-stats"}
|
|
|
|
${optionalString cfg.printStats "humanize-numbers"}
|
|
|
|
${optionalString (cfg.checkpointBytes != null) ("checkpoint-bytes "+cfg.checkpointBytes)}
|
|
|
|
${optionalString cfg.aggressiveNetworking "aggressive-networking"}
|
|
|
|
${concatStringsSep "\n" (map (v: "exclude ${v}") cfg.excludes)}
|
|
|
|
${concatStringsSep "\n" (map (v: "include ${v}") cfg.includes)}
|
|
|
|
${optionalString cfg.lowmem "lowmem"}
|
|
|
|
${optionalString cfg.verylowmem "verylowmem"}
|
|
|
|
${optionalString (cfg.maxbw != null) "maxbw ${toString cfg.maxbw}"}
|
|
|
|
${optionalString (cfg.maxbwRateUp != null) "maxbw-rate-up ${toString cfg.maxbwRateUp}"}
|
|
|
|
${optionalString (cfg.maxbwRateDown != null) "maxbw-rate-down ${toString cfg.maxbwRateDown}"}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "tarsnap" "cachedir" ] "Use services.tarsnap.archives.<name>.cachedir")
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.tarsnap = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "periodic tarsnap backups");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
keyfile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/root/tarsnap.key";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The keyfile which associates this machine with your tarsnap
|
|
|
|
account.
|
2022-08-12 12:06:08 +00:00
|
|
|
Create the keyfile with {command}`tarsnap-keygen`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Note that each individual archive (specified below) may also have its
|
|
|
|
own individual keyfile specified. Tarsnap does not allow multiple
|
|
|
|
concurrent backups with the same cache directory and key (starting a
|
|
|
|
new backup will cause another one to fail). If you have multiple
|
|
|
|
archives specified, you should either spread out your backups to be
|
|
|
|
far apart, or specify a separate key for each archive. By default
|
|
|
|
every archive defaults to using
|
2022-08-12 12:06:08 +00:00
|
|
|
`"/root/tarsnap.key"`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
It's recommended for backups that you generate a key for every archive
|
2022-08-12 12:06:08 +00:00
|
|
|
using `tarsnap-keygen(1)`, and then generate a
|
|
|
|
write-only tarsnap key using `tarsnap-keymgmt(1)`,
|
2020-04-24 23:36:52 +00:00
|
|
|
and keep your master key(s) for a particular machine off-site.
|
|
|
|
|
|
|
|
The keyfile name should be given as a string and not a path, to
|
|
|
|
avoid the key being copied into the Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
archives = mkOption {
|
2021-12-19 01:06:50 +00:00
|
|
|
type = types.attrsOf (types.submodule ({ config, options, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
keyfile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = gcfg.keyfile;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${opt.keyfile}";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Set a specific keyfile for this archive. This defaults to
|
2022-08-12 12:06:08 +00:00
|
|
|
`"/root/tarsnap.key"` if left unspecified.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Use this option if you want to run multiple backups
|
|
|
|
concurrently - each archive must have a unique key. You can
|
|
|
|
generate a write-only key derived from your master key (which
|
2022-08-12 12:06:08 +00:00
|
|
|
is recommended) using `tarsnap-keymgmt(1)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
Note: every archive must have an individual master key. You
|
|
|
|
must generate multiple keys with
|
2022-08-12 12:06:08 +00:00
|
|
|
`tarsnap-keygen(1)`, and then generate write
|
2020-04-24 23:36:52 +00:00
|
|
|
only keys from those.
|
|
|
|
|
|
|
|
The keyfile name should be given as a string and not a path, to
|
|
|
|
avoid the key being copied into the Nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cachedir = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = "/var/cache/tarsnap/${utils.escapeSystemdPath config.keyfile}";
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
"/var/cache/tarsnap/''${utils.escapeSystemdPath config.${options.keyfile}}"
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The cache allows tarsnap to identify previously stored data
|
|
|
|
blocks, reducing archival time and bandwidth usage.
|
|
|
|
|
|
|
|
Should the cache become desynchronized or corrupted, tarsnap
|
|
|
|
will refuse to run until you manually rebuild the cache with
|
2022-08-12 12:06:08 +00:00
|
|
|
{command}`tarsnap --fsck`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Set to `null` to disable caching.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
nodump = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Exclude files with the `nodump` flag.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
printStats = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Print global archive statistics upon completion.
|
|
|
|
The output is available via
|
2022-08-12 12:06:08 +00:00
|
|
|
{command}`systemctl status tarsnap-archive-name`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
checkpointBytes = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "1GB";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Create a checkpoint every `checkpointBytes`
|
2020-04-24 23:36:52 +00:00
|
|
|
of uploaded data (optionally specified using an SI prefix).
|
|
|
|
|
|
|
|
1GB is the minimum value. A higher value is recommended,
|
|
|
|
as checkpointing is expensive.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Set to `null` to disable checkpointing.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
period = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "01:15";
|
|
|
|
example = "hourly";
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Create archive at this interval.
|
|
|
|
|
|
|
|
The format is described in
|
2022-08-21 13:32:41 +00:00
|
|
|
{manpage}`systemd.time(7)`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
aggressiveNetworking = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Upload data over multiple TCP connections, potentially
|
|
|
|
increasing tarsnap's bandwidth utilisation at the cost
|
|
|
|
of slowing down all other network traffic. Not
|
|
|
|
recommended unless TCP congestion is the dominant
|
|
|
|
limiting factor.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
directories = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List of filesystem paths to archive.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
excludes = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Exclude files and directories matching these patterns.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
includes = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Include only files and directories matching these
|
|
|
|
patterns (the empty list includes everything).
|
|
|
|
|
|
|
|
Exclusions have precedence over inclusions.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
lowmem = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Reduce memory consumption by not caching small files.
|
|
|
|
Possibly beneficial if the average file size is smaller
|
|
|
|
than 1 MB and the number of files is lower than the
|
|
|
|
total amount of RAM in KB.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verylowmem = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Reduce memory consumption by a factor of 2 beyond what
|
2022-08-12 12:06:08 +00:00
|
|
|
`lowmem` does, at the cost of significantly
|
2020-04-24 23:36:52 +00:00
|
|
|
slowing down the archiving process.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
maxbw = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Abort archival if upstream bandwidth usage in bytes
|
|
|
|
exceeds this threshold.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
maxbwRateUp = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression "25 * 1000";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Upload bandwidth rate limit in bytes.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
maxbwRateDown = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression "50 * 1000";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Download bandwidth rate limit in bytes.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
verbose = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to produce verbose logging output.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
explicitSymlinks = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to follow symlinks specified as archives.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
followSymlinks = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to follow all symlinks in archive trees.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
default = {};
|
|
|
|
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
nixos =
|
|
|
|
{ directories = [ "/home" "/root/ssl" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
gamedata =
|
|
|
|
{ directories = [ "/var/lib/minecraft" ];
|
|
|
|
period = "*:30";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Tarsnap archive configurations. Each attribute names an archive
|
|
|
|
to be created at a given time interval, according to the options
|
|
|
|
associated with it. When uploading to the tarsnap server,
|
2021-01-15 22:18:51 +00:00
|
|
|
archive names are suffixed by a 1 second resolution timestamp,
|
2022-08-12 12:06:08 +00:00
|
|
|
with the format `%Y%m%d%H%M%S`.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
For each member of the set is created a timer which triggers the
|
2022-08-12 12:06:08 +00:00
|
|
|
instanced `tarsnap-archive-name` service unit. You may use
|
|
|
|
{command}`systemctl start tarsnap-archive-name` to
|
|
|
|
manually trigger creation of `archive-name` at
|
2020-04-24 23:36:52 +00:00
|
|
|
any time.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf gcfg.enable {
|
|
|
|
assertions =
|
|
|
|
(mapAttrsToList (name: cfg:
|
|
|
|
{ assertion = cfg.directories != [];
|
|
|
|
message = "Must specify paths for tarsnap to back up";
|
|
|
|
}) gcfg.archives) ++
|
|
|
|
(mapAttrsToList (name: cfg:
|
|
|
|
{ assertion = !(cfg.lowmem && cfg.verylowmem);
|
|
|
|
message = "You cannot set both lowmem and verylowmem";
|
|
|
|
}) gcfg.archives);
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
(mapAttrs' (name: cfg: nameValuePair "tarsnap-${name}" {
|
|
|
|
description = "Tarsnap archive '${name}'";
|
|
|
|
requires = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
|
2020-11-24 20:58:05 +00:00
|
|
|
path = with pkgs; [ iputils tarsnap util-linux ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# In order for the persistent tarsnap timer to work reliably, we have to
|
|
|
|
# make sure that the tarsnap server is reachable after systemd starts up
|
|
|
|
# the service - therefore we sleep in a loop until we can ping the
|
|
|
|
# endpoint.
|
|
|
|
preStart = ''
|
2021-10-01 09:20:50 +00:00
|
|
|
while ! ping -4 -q -c 1 v1-0-0-server.tarsnap.com &> /dev/null; do sleep 3; done
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
script = let
|
|
|
|
tarsnap = ''tarsnap --configfile "/etc/tarsnap/${name}.conf"'';
|
|
|
|
run = ''${tarsnap} -c -f "${name}-$(date +"%Y%m%d%H%M%S")" \
|
|
|
|
${optionalString cfg.verbose "-v"} \
|
|
|
|
${optionalString cfg.explicitSymlinks "-H"} \
|
|
|
|
${optionalString cfg.followSymlinks "-L"} \
|
|
|
|
${concatStringsSep " " cfg.directories}'';
|
2021-12-19 01:06:50 +00:00
|
|
|
cachedir = escapeShellArg cfg.cachedir;
|
2020-04-24 23:36:52 +00:00
|
|
|
in if (cfg.cachedir != null) then ''
|
2021-12-19 01:06:50 +00:00
|
|
|
mkdir -p ${cachedir}
|
|
|
|
chmod 0700 ${cachedir}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
( flock 9
|
2021-12-19 01:06:50 +00:00
|
|
|
if [ ! -e ${cachedir}/firstrun ]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
( flock 10
|
|
|
|
flock -u 9
|
|
|
|
${tarsnap} --fsck
|
|
|
|
flock 9
|
2021-12-19 01:06:50 +00:00
|
|
|
) 10>${cachedir}/firstrun
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
2021-12-19 01:06:50 +00:00
|
|
|
) 9>${cachedir}/lockf
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
exec flock ${cachedir}/firstrun ${run}
|
2020-04-24 23:36:52 +00:00
|
|
|
'' else "exec ${run}";
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
IOSchedulingClass = "idle";
|
|
|
|
NoNewPrivileges = "true";
|
|
|
|
CapabilityBoundingSet = [ "CAP_DAC_READ_SEARCH" ];
|
|
|
|
PermissionsStartOnly = "true";
|
|
|
|
};
|
|
|
|
}) gcfg.archives) //
|
|
|
|
|
|
|
|
(mapAttrs' (name: cfg: nameValuePair "tarsnap-restore-${name}"{
|
|
|
|
description = "Tarsnap restore '${name}'";
|
|
|
|
requires = [ "network-online.target" ];
|
|
|
|
|
2020-11-24 20:58:05 +00:00
|
|
|
path = with pkgs; [ iputils tarsnap util-linux ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
script = let
|
|
|
|
tarsnap = ''tarsnap --configfile "/etc/tarsnap/${name}.conf"'';
|
2021-02-05 17:12:51 +00:00
|
|
|
lastArchive = "$(${tarsnap} --list-archives | sort | tail -1)";
|
2020-04-24 23:36:52 +00:00
|
|
|
run = ''${tarsnap} -x -f "${lastArchive}" ${optionalString cfg.verbose "-v"}'';
|
2021-12-19 01:06:50 +00:00
|
|
|
cachedir = escapeShellArg cfg.cachedir;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in if (cfg.cachedir != null) then ''
|
2021-12-19 01:06:50 +00:00
|
|
|
mkdir -p ${cachedir}
|
|
|
|
chmod 0700 ${cachedir}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
( flock 9
|
2021-12-19 01:06:50 +00:00
|
|
|
if [ ! -e ${cachedir}/firstrun ]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
( flock 10
|
|
|
|
flock -u 9
|
|
|
|
${tarsnap} --fsck
|
|
|
|
flock 9
|
2021-12-19 01:06:50 +00:00
|
|
|
) 10>${cachedir}/firstrun
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
2021-12-19 01:06:50 +00:00
|
|
|
) 9>${cachedir}/lockf
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-12-19 01:06:50 +00:00
|
|
|
exec flock ${cachedir}/firstrun ${run}
|
2020-04-24 23:36:52 +00:00
|
|
|
'' else "exec ${run}";
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
IOSchedulingClass = "idle";
|
|
|
|
NoNewPrivileges = "true";
|
|
|
|
CapabilityBoundingSet = [ "CAP_DAC_READ_SEARCH" ];
|
|
|
|
PermissionsStartOnly = "true";
|
|
|
|
};
|
|
|
|
}) gcfg.archives);
|
|
|
|
|
|
|
|
# Note: the timer must be Persistent=true, so that systemd will start it even
|
|
|
|
# if e.g. your laptop was asleep while the latest interval occurred.
|
|
|
|
systemd.timers = mapAttrs' (name: cfg: nameValuePair "tarsnap-${name}"
|
|
|
|
{ timerConfig.OnCalendar = cfg.period;
|
|
|
|
timerConfig.Persistent = "true";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
}) gcfg.archives;
|
|
|
|
|
|
|
|
environment.etc =
|
|
|
|
mapAttrs' (name: cfg: nameValuePair "tarsnap/${name}.conf"
|
|
|
|
{ text = configFile name cfg;
|
|
|
|
}) gcfg.archives;
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.tarsnap ];
|
|
|
|
};
|
|
|
|
}
|