Unverified Commit 3b82fcc6 authored by Guillaume Girol's avatar Guillaume Girol Committed by GitHub
Browse files

Merge pull request #318826 from NorfairKing/borgbackup-requiremount

borgbackup: Use RequiresMountsFor to require that the repo is mounted
parents def3b717 edb707e3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -104,6 +104,9 @@ let
            --what="sleep" \
            --why="Scheduled backup" \
        '' + backupScript;
      unitConfig = optionalAttrs (isLocalPath cfg.repo) {
        RequiresMountsFor = [ cfg.repo ];
      };
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
+26 −0
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ let
  keepFile = "important_file";
  keepFileData = "important_data";
  localRepo = "/root/back:up";
  # a repository on a file system which is not mounted automatically
  localRepoMount = "/noAutoMount";
  archiveName = "my_archive";
  remoteRepo = "borg@server:."; # No need to specify path
  privateKey = pkgs.writeText "id_ed25519" ''
@@ -42,6 +44,12 @@ in {

  nodes = {
    client = { ... }: {
      virtualisation.fileSystems.${localRepoMount} = {
        device = "tmpfs";
        fsType = "tmpfs";
        options = [ "noauto" ];
      };

      services.borgbackup.jobs = {

        local = {
@@ -65,6 +73,13 @@ in {
          startAt = [ ]; # Do not run automatically
        };

        localMount = {
          paths = dataDir;
          repo = localRepoMount;
          encryption.mode = "none";
          startAt = [ ];
        };

        remote = {
          paths = dataDir;
          repo = remoteRepo;
@@ -178,6 +193,17 @@ in {
            "cat /mnt/borg/${dataDir}/${keepFile}"
        )

    with subtest("localMount"):
        # the file system for the repo should not be already mounted
        client.fail("mount | grep ${localRepoMount}")
        # ensure trying to write to the mountpoint before the fs is mounted fails
        client.succeed("chattr +i ${localRepoMount}")
        borg = "borg"
        client.systemctl("start --wait borgbackup-job-localMount")
        client.fail("systemctl is-failed borgbackup-job-localMount")
        # Make sure exactly one archive has been created
        assert int(client.succeed("{} list '${localRepoMount}' | wc -l".format(borg))) > 0

    with subtest("remote"):
        borg = "BORG_RSH='ssh -oStrictHostKeyChecking=no -i /root/id_ed25519' borg"
        server.wait_for_unit("sshd.service")