2019-07-26 04:30:59 +00:00
|
|
|
{ lib, pkgs, config, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.zfs.autoReplication;
|
2024-08-28 19:18:54 +00:00
|
|
|
recursive = lib.optionalString cfg.recursive " --recursive";
|
|
|
|
followDelete = lib.optionalString cfg.followDelete " --follow-delete";
|
2019-07-26 04:30:59 +00:00
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.zfs.autoReplication = {
|
2024-08-28 19:18:54 +00:00
|
|
|
enable = lib.mkEnableOption "ZFS snapshot replication";
|
2019-07-26 04:30:59 +00:00
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
followDelete = lib.mkOption {
|
2022-12-18 00:31:14 +00:00
|
|
|
description = "Remove remote snapshots that don't have a local correspondent.";
|
2019-07-26 04:30:59 +00:00
|
|
|
default = true;
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.bool;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
host = lib.mkOption {
|
2020-06-20 06:25:16 +00:00
|
|
|
description = "Remote host where snapshots should be sent. `lz4` is expected to be installed on this host.";
|
2019-07-26 04:30:59 +00:00
|
|
|
example = "example.com";
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.str;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
identityFilePath = lib.mkOption {
|
2019-07-26 04:30:59 +00:00
|
|
|
description = "Path to SSH key used to login to host.";
|
|
|
|
example = "/home/username/.ssh/id_rsa";
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.path;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
localFilesystem = lib.mkOption {
|
2022-12-18 00:31:14 +00:00
|
|
|
description = "Local ZFS filesystem from which snapshots should be sent. Defaults to the attribute name.";
|
2019-07-26 04:30:59 +00:00
|
|
|
example = "pool/file/path";
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.str;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
remoteFilesystem = lib.mkOption {
|
2019-07-26 04:30:59 +00:00
|
|
|
description = "Remote ZFS filesystem where snapshots should be sent.";
|
|
|
|
example = "pool/file/path";
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.str;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
recursive = lib.mkOption {
|
2019-07-26 04:30:59 +00:00
|
|
|
description = "Recursively discover snapshots to send.";
|
|
|
|
default = true;
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.bool;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-28 19:18:54 +00:00
|
|
|
username = lib.mkOption {
|
2019-07-26 04:30:59 +00:00
|
|
|
description = "Username used by SSH to login to remote host.";
|
|
|
|
example = "username";
|
2024-08-28 19:18:54 +00:00
|
|
|
type = lib.types.str;
|
2019-07-26 04:30:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.lz4
|
|
|
|
];
|
|
|
|
|
2019-08-13 21:52:01 +00:00
|
|
|
systemd.services.zfs-replication = {
|
2019-07-26 04:30:59 +00:00
|
|
|
after = [
|
|
|
|
"zfs-snapshot-daily.service"
|
|
|
|
"zfs-snapshot-frequent.service"
|
|
|
|
"zfs-snapshot-hourly.service"
|
|
|
|
"zfs-snapshot-monthly.service"
|
|
|
|
"zfs-snapshot-weekly.service"
|
|
|
|
];
|
|
|
|
description = "ZFS Snapshot Replication";
|
|
|
|
documentation = [
|
|
|
|
"https://github.com/alunduil/zfs-replicate"
|
|
|
|
];
|
|
|
|
restartIfChanged = false;
|
2024-08-28 19:18:54 +00:00
|
|
|
serviceConfig.ExecStart = "${pkgs.zfs-replicate}/bin/zfs-replicate${recursive} -l ${lib.escapeShellArg cfg.username} -i ${lib.escapeShellArg cfg.identityFilePath}${followDelete} ${lib.escapeShellArg cfg.host} ${lib.escapeShellArg cfg.remoteFilesystem} ${lib.escapeShellArg cfg.localFilesystem}";
|
2019-07-26 04:30:59 +00:00
|
|
|
wantedBy = [
|
|
|
|
"zfs-snapshot-daily.service"
|
|
|
|
"zfs-snapshot-frequent.service"
|
|
|
|
"zfs-snapshot-hourly.service"
|
|
|
|
"zfs-snapshot-monthly.service"
|
|
|
|
"zfs-snapshot-weekly.service"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
maintainers = with lib.maintainers; [ alunduil ];
|
|
|
|
};
|
|
|
|
}
|