···
{ config, lib, pkgs, ... }:
+
# { "1d" = "1h"; "1w" = "1d"; }
+
attrToPlan = attrs: concatStringsSep "," (builtins.attrValues (
+
mapAttrs (n: v: "${n}=>${v}") attrs));
+
The znapzend backup plan to use for the source.
+
The plan specifies how often to backup and for how long to keep the
+
backups. It consists of a series of retention periodes to interval
+
retA=>intA,retB=>intB,...
+
Both intervals and retention periods are expressed in standard units
+
of time or multiples of them. You can use both the full name or a
+
shortcut according to the following listing:
+
second|sec|s, minute|min, hour|h, day|d, week|w, month|mon|m, year|y
+
See <citerefentry><refentrytitle>znapzendzetup</refentrytitle><manvolnum>1</manvolnum></citerefentry> for more info.
+
planExample = "1h=>10min,1d=>1h,1w=>1d,1m=>1w,1y=>1m";
+
# A type for a string of the form number{b|k|M|G}
+
mbufferSizeType = str // {
+
check = x: str.check x && builtins.isList (builtins.match "^[0-9]+[bkMG]$" x);
+
description = "string of the form number{b|k|M|G}";
+
# Type for a string that must contain certain other strings (the list parameter).
+
# Note that these would need regex escaping.
+
stringContainingStrings = list: let
+
matching = s: map (str: builtins.match ".*${str}.*" s) list;
+
check = x: str.check x && all isList (matching x);
+
description = "string containing all of the characters ${concatStringsSep ", " list}";
+
timestampType = stringContainingStrings [ "%Y" "%m" "%d" "%H" "%M" "%S" ];
+
destType = srcConfig: submodule ({ name, ... }: {
+
description = "Label for this destination. Defaults to the attribute name.";
+
description = planDescription;
+
description = "Dataset name to send snapshots to.";
+
Host to use for the destination dataset. Can be prefixed with
+
<literal>user@</literal> to specify the ssh user.
+
example = "john@example.com";
+
Command to run before sending the snapshot to the destination.
+
Intended to run a remote script via <command>ssh</command> on the
+
destination, e.g. to bring up a backup disk or server or to put a
+
zpool online/offline. See also <option>postsend</option>.
+
example = "ssh root@bserv zpool import -Nf tank";
+
Command to run after sending the snapshot to the destination.
+
Intended to run a remote script via <command>ssh</command> on the
+
destination, e.g. to bring up a backup disk or server or to put a
+
zpool online/offline. See also <option>presend</option>.
+
example = "ssh root@bserv zpool export tank";
+
label = mkDefault name;
+
plan = mkDefault srcConfig.plan;
+
srcType = submodule ({ name, config, ... }: {
+
description = "Whether to enable this source.";
+
description = "Whether to do recursive snapshots.";
+
description = "Whether to use <command>mbuffer</command>.";
+
type = nullOr ints.u16;
+
Port to use for <command>mbuffer</command>.
+
If this is null, it will run <command>mbuffer</command> through
+
If this is not null, it will run <command>mbuffer</command>
+
directly through TCP, which is not encrypted but faster. In that
+
case the given port needs to be open on the destination host.
+
type = mbufferSizeType;
+
The size for <command>mbuffer</command>.
+
Supports the units b, k, M, G.
+
Command to run before snapshots are taken on the source dataset,
+
e.g. for database locking/flushing. See also
+
<option>postsnap</option>.
+
example = literalExample ''
+
''${pkgs.mariadb}/bin/mysql -e "set autocommit=0;flush tables with read lock;\\! ''${pkgs.coreutils}/bin/sleep 600" & ''${pkgs.coreutils}/bin/echo $! > /tmp/mariadblock.pid ; sleep 10
+
Command to run after snapshots are taken on the source dataset,
+
e.g. for database unlocking. See also <option>presnap</option>.
+
example = literalExample ''
+
''${pkgs.coreutils}/bin/kill `''${pkgs.coreutils}/bin/cat /tmp/mariadblock.pid`;''${pkgs.coreutils}/bin/rm /tmp/mariadblock.pid
+
timestampFormat = mkOption {
+
The timestamp format to use for constructing snapshot names.
+
The syntax is <literal>strftime</literal>-like. The string must
+
consist of the mandatory <literal>%Y %m %d %H %M %S</literal>.
+
Optionally <literal>- _ . :</literal> characters as well as any
+
alphanumeric character are allowed. If suffixed by a
+
<literal>Z</literal>, times will be in UTC.
+
default = "%Y-%m-%d-%H%M%S";
+
example = "znapzend-%m.%d.%Y-%H%M%SZ";
+
Specify delay (in seconds) before sending snaps to the destination.
+
May be useful if you want to control sending time.
+
description = planDescription;
+
description = "The dataset to use for this source.";
+
destinations = mkOption {
+
type = loaOf (destType config);
+
description = "Additional destinations.";
+
example = literalExample ''
+
dataset = "btank/backup";
+
presend = "zpool import -N btank";
+
postsend = "zpool export btank";
+
host = "john@example.com";
+
dataset = mkDefault name;
+
### Generating the configuration from here
cfg = config.services.znapzend;
+
onOff = b: if b then "on" else "off";
+
nullOff = b: if isNull b then "off" else toString b;
+
stripSlashes = replaceStrings [ "/" ] [ "." ];
+
attrsToFile = config: concatStringsSep "\n" (builtins.attrValues (
+
mapAttrs (n: v: "${n}=${v}") config));
+
mkDestAttrs = dst: with dst;
+
mapAttrs' (n: v: nameValuePair "dst_${label}${n}" v) ({
+
"" = optionalString (! isNull host) "${host}:" + dataset;
+
} // optionalAttrs (presend != null) {
+
} // optionalAttrs (postsend != null) {
+
mkSrcAttrs = srcCfg: with srcCfg; {
+
enabled = onOff enable;
+
mbuffer = with mbuffer; if enable then "${pkgs.mbuffer}/bin/mbuffer"
+
+ optionalString (port != null) ":${toString port}" else "off";
+
mbuffer_size = mbuffer.size;
+
post_znap_cmd = nullOff postsnap;
+
pre_znap_cmd = nullOff presnap;
+
recursive = onOff recursive;
+
tsformat = timestampFormat;
+
zend_delay = toString sendDelay;
+
} // fold (a: b: a // b) {} (
+
map mkDestAttrs (builtins.attrValues destinations)
+
files = mapAttrs' (n: srcCfg: let
+
fileText = attrsToFile (mkSrcAttrs srcCfg);
+
value = pkgs.writeText (stripSlashes srcCfg.dataset) fileText;
+
enable = mkEnableOption "ZnapZend ZFS backup daemon";
+
type = enum ["debug" "info" "warning" "err" "alert"];
+
The log level when logging to file. Any of debug, info, warning, err,
+
alert. Default in daemonized form is debug.
default = "syslog::daemon";
example = "/var/log/znapzend.log";
+
Where to log to (syslog::<facility> or <filepath>).
description = "Does all changes to the filesystem except destroy.";
autoCreation = mkOption {
+
description = "Automatically create the destination dataset if it does not exists.";
+
description = "Znapzend configuration.";
+
example = literalExample ''
+
# Make snapshots of tank/home every hour, keep those for 1 day,
+
# keep every days snapshot for 1 month, etc.
+
plan = "1d=>1h,1m=>1d,1y=>1m";
+
# Send all those snapshots to john@example.com:rtank/john as well
+
destinations.remote = {
+
host = "john@example.com";
+
dataset = "rtank/john";
+
Do not persist any stateful znapzend setups. If this option is
+
enabled, your previously set znapzend setups will be cleared and only
+
the ones defined with this module will be applied.
···
path = with pkgs; [ zfs mbuffer openssh ];
+
preStart = optionalString cfg.pure ''
+
echo Resetting znapzend zetups
+
${pkgs.znapzend}/bin/znapzendzetup list \
+
| grep -oP '(?<=\*\*\* backup plan: ).*(?= \*\*\*)' \
+
| xargs ${pkgs.znapzend}/bin/znapzendzetup delete
+
'' + concatStringsSep "\n" (mapAttrsToList (dataset: config: ''
+
echo Importing znapzend zetup ${config} for dataset ${dataset}
+
${pkgs.znapzend}/bin/znapzendzetup import --write ${dataset} ${config}
+
args = concatStringsSep " " [
+
"--loglevel=${cfg.logLevel}"
+
(optionalString cfg.noDestroy "--nodestroy")
+
(optionalString cfg.autoCreation "--autoCreation")
+
]; in "${pkgs.znapzend}/bin/znapzend ${args}";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
+
meta.maintainers = with maintainers; [ infinisil ];