borgBackup to external disk when connected next time - how to in nixos

Hello,

I’m creating backups via the configuration file of nixos.

  • at the moment only onto the ssd where nixos runs on.

So I would like to know if someone has an example for how to configure it for the situation when it should backup to an external ssd (usb3) accourding to “startat”/systemd.timer and/or automatically when the usb ssd gets connected next time to the pc.

All of this is highly hypothetical, but systemd has mount units on which you can depend:

  1. mount your ssd
  2. systemctl list-units | grep mount
  3. find you ssd mount unit, say mnt-ssd.mount
{
systemd.services."borgbackup-job-${jobname}" = {
requires = [ "mnt-ssd.mount" ];
after = [ "mnt-sdd.mount" ];
};
}

This probably requires that the mountpoint for the ssd is in fstab (that is, declared in the fileSystems nixos options).

Then, the systemd timer will launch borg backup just as before, but systemd will attempt to mount and probably wait for the device before proceeding. After a probably configurable timeout, if the device does not show up, systemd will give up with dependency failed.

Hello symphorien,

[the external ssd is encrypted via LVM]

i tried the following:

let
  BackupSSDext = "BackupExternal" ; 
in
{
    systemd.timers."borgbackup-job-${BackupSSDext}".timerConfig.Persistent = true;

    systemd = { 
        services."borgbackup-job-${BackupSSDext}" = {
        requires = [ "backUp.mount" ]; 
        after    = [ "backUp.mount" ];
        };
    }; 

    services = {
        borgbackup.jobs = {
        "${BackupSSDext}" = { 
            compression = "auto,zstd";
            paths=[    
                "/home/ae/.thunderbird/"
                ];
            exclude=[ 
            "**/.Trash" 
            "/home/*/.cache" 
            ];

            repo = "/mnt/backUp/borgBackup/"; 

            encryption = {
                mode = "repokey";           
                passCommand="cat /home/user/.passpw"; 
            };
            startAt = "daily";
        };

      };
    };
}

i set the mount point and via nixos-generate-config it got the right entry
(I commented out the boot.initrd.luks. … )

  fileSystems."/backUp" =
    { device = "/dev/disk/by-uuid/e5c81bb0-ac6d-4a7e-8fc9-a0ac63315388";
      fsType = "ext4";
      mountPoint = "/backUp" ;

      neededForBoot = false;
      options = [ "noauto" ];

    };
  #boot.initrd.luks.devices."luks-991355a1-c442-4798-8aba-ec26b149b2b2".device = "/dev/disk/by-uuid/991355a1-c442-4798-8aba-ec26b149b2b2";

it worked with nixos-rebuild switch when the ssd was connected but is not doing automatic backups the next day it is connected.


danbst could you take a look (if you have time) if you see (an) errors?

it worked with nixos-rebuild switch when the ssd was connected but is not doing automatic backups the next day it is connected.

This is expected. The dependencies only instruct systemd to attempt to mount the ssd when the timer fires. So you have to wait for the next trigger of the timer, that it, midnight.

Maybe you can add `wantedBy = [ “backUp.mount” ] on the borg backup service. This way mounting will trigger the backup.

1 Like