89 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Nix
		
	
	
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Nix
		
	
	
	
	
	
{pkgs, lib, ...}:
 | 
						|
let
 | 
						|
  snapshotsDir = "/snapshots/home";
 | 
						|
in
 | 
						|
{
 | 
						|
 | 
						|
  environment.systemPackages = with pkgs; [
 | 
						|
    restic
 | 
						|
    btrbk
 | 
						|
    libnotify
 | 
						|
  ];
 | 
						|
 | 
						|
 | 
						|
  # btrbk systemd service and timer for daily home snapshots
 | 
						|
  systemd.services.btrbk-home-snapshot = {
 | 
						|
    description = "Create daily btrbk snapshot of the home subvolume";
 | 
						|
    serviceConfig = {
 | 
						|
      Type = "oneshot";
 | 
						|
      User = "remy";
 | 
						|
      ExecStartPre = "${lib.getExe pkgs.libnotify} \"Backup\" \"Creating BTRBK snapshot of /home.\"";
 | 
						|
      # run the btrbk command as superuser
 | 
						|
      ExecStart = "+${lib.getExe pkgs.btrbk} -c /etc/btrbk/home.conf run";
 | 
						|
      Environment = "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus"; # ensure the notification is sent to the correct display
 | 
						|
      ExecStartPost = "${lib.getExe pkgs.libnotify} \"Backup\" \"Snapshot of /home created successfully.\"";
 | 
						|
    };
 | 
						|
  };
 | 
						|
 | 
						|
  systemd.timers.btrbk-home-snapshot = {
 | 
						|
    enable = true;
 | 
						|
    description = "Daily timer for btrbk home snapshot";
 | 
						|
    wantedBy = [ "timers.target" ];
 | 
						|
    timerConfig = {
 | 
						|
      OnCalendar = "*-*-* 12:00";
 | 
						|
      Persistent = true; # ensures missed runs are triggered after resume
 | 
						|
    };
 | 
						|
  };
 | 
						|
  # ensure the target directory exists
 | 
						|
  systemd.tmpfiles.settings = {
 | 
						|
    "btrbk snapshots" = {
 | 
						|
      "${snapshotsDir}" = {
 | 
						|
        d = {
 | 
						|
          group = "root";
 | 
						|
          user = "root";
 | 
						|
          mode = "0770";
 | 
						|
        };
 | 
						|
      };
 | 
						|
    };
 | 
						|
  };
 | 
						|
 | 
						|
  # btrbk config for home snapshots
 | 
						|
  environment.etc."btrbk/home.conf".text = ''
 | 
						|
    timestamp_format long
 | 
						|
    # keep snapshots for 2 days independently of the call
 | 
						|
    snapshot_preserve_min   2d
 | 
						|
    # retain daily snapshots for 14 days
 | 
						|
    snapshot_preserve      14d
 | 
						|
 | 
						|
    subvolume /home
 | 
						|
    snapshot_dir ${snapshotsDir}
 | 
						|
  '';
 | 
						|
 | 
						|
  # Now create a restic backup off the newest btrbk snapshot
 | 
						|
  systemd.services.restic-backup-latest-snapshot = {
 | 
						|
    description = "Backup home subvolume using restic";
 | 
						|
    serviceConfig = {
 | 
						|
      Type = "oneshot";
 | 
						|
      ExecStart = "${lib.getExe pkgs.restic} -r /home/snapshots/restic-backup backup /home/snapshots/home-$(date +%Y-%m-%d_%H-%M-%S)";
 | 
						|
      # send a notification when the service is done
 | 
						|
      ExecStartPost = "su remy -c 'notify-send \"Restic Backup\" \"Home backup created successfully.\"'";
 | 
						|
    };
 | 
						|
  };
 | 
						|
  # # the udev rule:
 | 
						|
  # services.udev.extraRules = ''
 | 
						|
  #   ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_LABEL}=="backup-restic", TAG+="systemd", ENV{SYSTEMD_WANTS}="backup-restic.service"
 | 
						|
  # '';
 | 
						|
 | 
						|
  # # the systemd service:
 | 
						|
  # systemd.services.backup-restic = {
 | 
						|
  #   description = "Backup using restic (triggered when USB drive is plugged in)";
 | 
						|
  #   after = [ "local-fs.target" ];
 | 
						|
  #   wantedBy = [ "multi-user.target" ];
 | 
						|
  #   serviceConfig = {
 | 
						|
  #     Type = "oneshot";
 | 
						|
  #     # TODO: adapt command
 | 
						|
  #     ExecStart = "${lib.getExe pkgs.restic} backup /home/username";
 | 
						|
  #   };
 | 
						|
  # };
 | 
						|
}
 |