diff options
author | Alex Brandt <alunduil@gmail.com> | 2019-07-25 21:30:59 -0700 |
---|---|---|
committer | Alex Brandt <alunduil@gmail.com> | 2019-08-02 08:04:21 -0700 |
commit | bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb (patch) | |
tree | 2d7f03ecbcd68e7b117dafb79b3356cc1cbc168c /nixos/modules | |
parent | 29bdb1c67253054982448f6c4e8bc3d5c5ee3c8a (diff) | |
download | nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar.gz nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar.bz2 nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar.lz nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar.xz nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.tar.zst nixlib-bdd7b5a3abbc6f826f608dd68f5c0ed18fcff9cb.zip |
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.
Diffstat (limited to 'nixos/modules')
-rw-r--r-- | nixos/modules/services/backup/zfs-replication.nix | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/nixos/modules/services/backup/zfs-replication.nix b/nixos/modules/services/backup/zfs-replication.nix new file mode 100644 index 000000000000..785cedb98694 --- /dev/null +++ b/nixos/modules/services/backup/zfs-replication.nix @@ -0,0 +1,90 @@ +{ lib, pkgs, config, ... }: + +with lib; + +let + cfg = config.services.zfs.autoReplication; + recursive = optionalString cfg.recursive " --recursive"; + followDelete = optionalString cfg.followDelete " --follow-delete"; +in { + options = { + services.zfs.autoReplication = { + enable = mkEnableOption "ZFS snapshot replication."; + + followDelete = mkOption { + description = "Remove remote snapshots that don't have a local correspondant."; + default = true; + type = types.bool; + }; + + host = mkOption { + description = "Remote host where snapshots should be sent."; + example = "example.com"; + type = types.str; + }; + + identityFilePath = mkOption { + description = "Path to SSH key used to login to host."; + example = "/home/username/.ssh/id_rsa"; + type = types.path; + }; + + localFilesystem = mkOption { + description = "Local ZFS fileystem from which snapshots should be sent. Defaults to the attribute name."; + example = "pool/file/path"; + type = types.str; + }; + + remoteFilesystem = mkOption { + description = "Remote ZFS filesystem where snapshots should be sent."; + example = "pool/file/path"; + type = types.str; + }; + + recursive = mkOption { + description = "Recursively discover snapshots to send."; + default = true; + type = types.bool; + }; + + username = mkOption { + description = "Username used by SSH to login to remote host."; + example = "username"; + type = types.str; + }; + }; + }; + + config = lib.mkIf cfg.enable { + environment.systemPackages = [ + pkgs.lz4 + ]; + + systemd.services."zfs-replication" = { + 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; + 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}"; + 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 ]; + }; +} |