git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/zfs: add autoReplication functionality

This adds a simple configuration for sending snapshots to a remote system using zfs-replicate that ties into the autoSnapshot settings already present in services.zfs.autoSnapshot.

commitbdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb authorAlex Brandt <alunduil@gmail.com> dateThu 25 Jul 2019, 21:30 (7 years ago) parent29bdb1c
1 file changed+90
nixos/modules/services/backup/zfs-replication.nix+90
nixos/modules/services/backup/zfs-replication.nixnew file+90
@@ -0,0 +1,90 @@
1+{ lib, pkgs, config, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.zfs.autoReplication;
7+ recursive = optionalString cfg.recursive " --recursive";
8+ followDelete = optionalString cfg.followDelete " --follow-delete";
9+in {
10+ options = {
11+ services.zfs.autoReplication = {
12+ enable = mkEnableOption "ZFS snapshot replication.";
13+
14+ followDelete = mkOption {
15+ description = "Remove remote snapshots that don't have a local correspondant.";
16+ default = true;
17+ type = types.bool;
18+ };
19+
20+ host = mkOption {
21+ description = "Remote host where snapshots should be sent.";
22+ example = "example.com";
23+ type = types.str;
24+ };
25+
26+ identityFilePath = mkOption {
27+ description = "Path to SSH key used to login to host.";
28+ example = "/home/username/.ssh/id_rsa";
29+ type = types.path;
30+ };
31+
32+ localFilesystem = mkOption {
33+ description = "Local ZFS fileystem from which snapshots should be sent. Defaults to the attribute name.";
34+ example = "pool/file/path";
35+ type = types.str;
36+ };
37+
38+ remoteFilesystem = mkOption {
39+ description = "Remote ZFS filesystem where snapshots should be sent.";
40+ example = "pool/file/path";
41+ type = types.str;
42+ };
43+
44+ recursive = mkOption {
45+ description = "Recursively discover snapshots to send.";
46+ default = true;
47+ type = types.bool;
48+ };
49+
50+ username = mkOption {
51+ description = "Username used by SSH to login to remote host.";
52+ example = "username";
53+ type = types.str;
54+ };
55+ };
56+ };
57+
58+ config = lib.mkIf cfg.enable {
59+ environment.systemPackages = [
60+ pkgs.lz4
61+ ];
62+
63+ systemd.services."zfs-replication" = {
64+ after = [
65+ "zfs-snapshot-daily.service"
66+ "zfs-snapshot-frequent.service"
67+ "zfs-snapshot-hourly.service"
68+ "zfs-snapshot-monthly.service"
69+ "zfs-snapshot-weekly.service"
70+ ];
71+ description = "ZFS Snapshot Replication";
72+ documentation = [
73+ "https://github.com/alunduil/zfs-replicate"
74+ ];
75+ restartIfChanged = false;
76+ serviceConfig.ExecStart = "${pkgs.zfs-replicate}/bin/zfs-replicate${recursive} -l ${escapeShellArg cfg.username} -i ${escapeShellArg cfg.identityFilePath}${followDelete} ${escapeShellArg cfg.host} ${escapeShellArg cfg.remoteFilesystem} ${escapeShellArg cfg.localFilesystem}";
77+ wantedBy = [
78+ "zfs-snapshot-daily.service"
79+ "zfs-snapshot-frequent.service"
80+ "zfs-snapshot-hourly.service"
81+ "zfs-snapshot-monthly.service"
82+ "zfs-snapshot-weekly.service"
83+ ];
84+ };
85+ };
86+
87+ meta = {
88+ maintainers = with lib.maintainers; [ alunduil ];
89+ };
90+}