From 6ec3de9e4eae74f29d40f83d4f14cb68c65b224e Mon Sep 17 00:00:00 2001 From: Crony Akatsuki Date: Sun, 2 Feb 2025 14:26:19 +0100 Subject: [PATCH] Move restic to modules, also create backup services. --- hosts/nixos/home.nix | 1 - modules/home-manager/default.nix | 2 + modules/home-manager/restic.nix | 92 ++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 modules/home-manager/restic.nix diff --git a/hosts/nixos/home.nix b/hosts/nixos/home.nix index 085840a..ac4aa15 100644 --- a/hosts/nixos/home.nix +++ b/hosts/nixos/home.nix @@ -13,7 +13,6 @@ home.packages = with pkgs; [ alejandra keepassxc - restic age nix-output-monitor nvd diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index 39209ad..84d8e2d 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -8,6 +8,7 @@ ./mpv.nix ./obs-studio.nix ./nh.nix + ./restic.nix ]; crony.mangohud.enable = lib.mkDefault true; @@ -18,4 +19,5 @@ crony.mpv.enable = lib.mkDefault true; crony.obs-studio.enable = lib.mkDefault true; crony.nh.enable = lib.mkDefault true; + crony.restic.enable = lib.mkDefault true; } diff --git a/modules/home-manager/restic.nix b/modules/home-manager/restic.nix new file mode 100644 index 0000000..60df372 --- /dev/null +++ b/modules/home-manager/restic.nix @@ -0,0 +1,92 @@ +{ + config, + pkgs, + lib, + ... +}: { + options = { + crony.restic.enable = lib.mkEnableOption "Install and setup restic and backup services."; + }; + + config = lib.mkIf config.crony.restic.enable { + # Install restic package + home.packages = with pkgs; [ + restic + ]; + + ##-- Restic Timers And Services --## + + # Restic backup + systemd.user.services = { + restic_backup = { + Unit.Description = "Restic backup service"; + Service = { + Type = "oneshot"; + ExecStart = toString (pkgs.writeShellScript "restic-backup" '' + . /etc/restic/local + restic backup --files-from /home/crony/.config/restic/list --verbose && restic forget --keep-last 10 --keep-daily 7 --keep-weekly 5 --keep-monthly 12 + . /etc/restic/online + restic backup --files-from /home/crony/.config/restic/list --verbose && restic forget --keep-last 10 --keep-daily 7 --keep-weekly 5 --keep-monthly 12 + rsync -avP --delete "$HOME/.local/backup" "$HOME/Documents/Share/backup" + ''); + }; + }; + }; + systemd.user.timers = { + restic_backup = { + Unit.Description = "Restic backup timer"; + Timer = { + OnBootSec = "5m"; + OnUnitActiveSec = "6h"; + }; + Install.WantedBy = ["timers.target"]; + }; + }; + + # Restic check + systemd.user.services = { + restic_check = { + Unit.Description = "Restic check service"; + Service = { + Type = "oneshot"; + ExecStart = toString (pkgs.writeShellScript "restic-check" '' + . /etc/restic/local + restic check --read-data-subset=10% + . /etc/restic/online + restic check --read-data-subset=10% + ''); + }; + }; + }; + systemd.user.timers = { + restic_check = { + Unit.Description = "Restic check timer"; + Timer = {OnCalendar = "Thu *-*-* 18:00:00";}; + Install.WantedBy = ["timers.target"]; + }; + }; + + # Restic prune + systemd.user.services = { + restic_prune = { + Unit.Description = "Restic prune service"; + Service = { + Type = "oneshot"; + ExecStart = toString (pkgs.writeShellScript "restic-prune" '' + . /etc/restic/local + restic prune + . /etc/restic/online + restic prune + ''); + }; + }; + }; + systemd.user.timers = { + restic_prune = { + Unit.Description = "Restic prune timer"; + Timer = {OnCalendar = "Fri *-*-* 18:00:00";}; + Install.WantedBy = ["timers.target"]; + }; + }; + }; +}