Borg backup: Difference between revisions

Klinger (talk | contribs)
m Undo revision 20640 The category is not redundant. This application is both an application and a backup app.
Tag: Undo
DHCP (talk | contribs)
m fix indentation
 
(4 intermediate revisions by 3 users not shown)
Line 3: Line 3:
This wiki article extends the documentation in the [https://nixos.org/manual/nixos/stable/#module-borgbase NixOS manual].
This wiki article extends the documentation in the [https://nixos.org/manual/nixos/stable/#module-borgbase NixOS manual].


<syntaxHighlight lang=bash>
== Installation ==
$ nix-env -iA nixpkgs.borgbackup
 
</syntaxHighlight>
It's easier to take the first steps with Borg by using a GUI - information about Vorta may also be found in the [https://nixos.org/manual/nixos/stable/#opt-services-backup-borgbackup-vorta NixOS manual].
 
To install Borg system-wide, use this:
 
{{File|name=/etc/nixos/configuration.nix|lang=nix|3=
environment.systemPackages = [
  pkgs.borgbackup
];}}


To be able to do remote backups it should be installed both locally and remotely, but usually no remote configuration required, only local one.
To be able to do remote backups it should be installed both locally and remotely, but usually no remote configuration required, only a local one.


== Creating backups ==
== Creating backups ==
Line 14: Line 21:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
  services.borgbackup.jobs.home-danbst = {
services.borgbackup.jobs.home-danbst = {
    paths = "/home/danbst";
  paths = "/home/danbst";
    encryption.mode = "none";
  encryption.mode = "none";
    environment.BORG_RSH = "ssh -i /home/danbst/.ssh/id_ed25519";
  environment.BORG_RSH = "ssh -i /home/danbst/.ssh/id_ed25519";
    repo = "ssh://user@example.com:23/path/to/backups-dir/home-danbst";
  repo = "ssh://user@example.com:23/path/to/backups-dir/home-danbst";
    compression = "auto,zstd";
  compression = "auto,zstd";
    startAt = "daily";
  startAt = "daily";
  };
};
</syntaxHighlight>
</syntaxHighlight>


Line 31: Line 38:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
  services.borgbackup.jobs =
services.borgbackup.jobs =
     let common-excludes = [
  let
          # Largest cache dirs
     common-excludes = [
          ".cache"
      # Largest cache dirs
          "*/cache2" # firefox
      ".cache"
          "*/Cache"
      "*/cache2" # firefox
          ".config/Slack/logs"
      "*/Cache"
          ".config/Code/CachedData"
      ".config/Slack/logs"
          ".container-diff"
      ".config/Code/CachedData"
          ".npm/_cacache"
      ".container-diff"
          # Work related dirs
      ".npm/_cacache"
          "*/node_modules"
      # Work related dirs
          "*/bower_components"
      "*/node_modules"
          "*/_build"
      "*/bower_components"
          "*/.tox"
      "*/_build"
          "*/venv"
      "*/.tox"
          "*/.venv"
      "*/venv"
        ];
      "*/.venv"
        work-dirs = [
    ];
          "/home/danbst/dev/company1"
    work-dirs = [
          "/home/danbst/dev/company2"
      "/home/danbst/dev/company1"
        ];
      "/home/danbst/dev/company2"
        basicBorgJob = name: {
    ];
          encryption.mode = "none";
    basicBorgJob = name: {
          environment.BORG_RSH = "ssh -o 'StrictHostKeyChecking=no' -i /home/danbst/.ssh/id_ed25519";
      encryption.mode = "none";
          environment.BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK = "yes";
      environment.BORG_RSH = "ssh -o 'StrictHostKeyChecking=no' -i /home/danbst/.ssh/id_ed25519";
          extraCreateArgs = "--verbose --stats --checkpoint-interval 600";
      environment.BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK = "yes";
          repo = "ssh://user@example.com//media/backup/${name}";
      extraCreateArgs = "--verbose --stats --checkpoint-interval 600";
          compression = "zstd,1";
      repo = "ssh://user@example.com//media/backup/${name}";
          startAt = "daily";
      compression = "zstd,1";
          user = "danbst";
      startAt = "daily";
        };
      user = "danbst";
   in {
    };
   in
  {
     home-danbst = basicBorgJob "backups/station/home-danbst" // rec {
     home-danbst = basicBorgJob "backups/station/home-danbst" // rec {
       paths = "/home/danbst";
       paths = "/home/danbst";
       exclude = work-dirs ++ map (x: paths + "/" + x) (common-excludes ++ [
       exclude = work-dirs ++ map (x: paths + "/" + x) (common-excludes ++ [ "Downloads" ]);
        "Downloads"
      ]);
     };
     };
     home-danbst-downloads = basicBorgJob "backups/station/home-danbst-downloads" // rec {
     home-danbst-downloads = basicBorgJob "backups/station/home-danbst-downloads" // rec {
Line 138: Line 145:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
    } // flip mapAttrs' config.services.borgbackup.jobs (name: value:
} // flip mapAttrs' config.services.borgbackup.jobs (name: value:
      nameValuePair "borgbackup-job-${name}" {
  nameValuePair "borgbackup-job-${name}" {
        unitConfig.OnFailure = "notify-problems@%i.service";
    unitConfig.OnFailure = "notify-problems@%i.service";
        preStart = lib.mkBefore ''
    preStart = lib.mkBefore ''
          # waiting for internet after resume-from-suspend
      # waiting for internet after resume-from-suspend
          until /run/wrappers/bin/ping google.com -c1 -q >/dev/null; do :; done
      until /run/wrappers/bin/ping google.com -c1 -q >/dev/null; do :; done
        '';
    '';
      }
  }
    );
);
    ...
...
</syntaxHighlight>
</syntaxHighlight>


Line 154: Line 161:
First, check if there are any archives:
First, check if there are any archives:


<syntaxHighlight lang=bash>
<syntaxHighlight lang=console>
$ borg list user@example.name:/media/backup/backups/station/home-danbst
$ borg list user@example.name:/media/backup/backups/station/home-danbst
...
...
Line 168: Line 175:
Choose one of "archives" and mount it locally:
Choose one of "archives" and mount it locally:


<syntaxHighlight lang=bash>
<syntaxHighlight lang=console>
$ borgfs -f -o uid=1002 \
$ borgfs -f -o uid=1002 \
     user@example.com:/media/backup/backups/station/home-danbst::station-home-danbst-2020-06-10T00:00:46 \
     user@example.com:/media/backup/backups/station/home-danbst::station-home-danbst-2020-06-10T00:00:46 \
Line 182: Line 189:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
  fileSystems."/run/user/1002/borg-home-danbst" = {
fileSystems."/run/user/1002/borg-home-danbst" = {
    device = "user@example.com:/media/backup/backups/station/home-danbst::station-home-danbst-2020-06-10T00:00:46";
  device = "user@example.com:/media/backup/backups/station/home-danbst::station-home-danbst-2020-06-10T00:00:46";
    noCheck = true;
  noCheck = true;
    fsType = "fuse.borgfs";      # note that this requires a custom binary, see below
  fsType = "fuse.borgfs";      # note that this requires a custom binary, see below
    options = [ "x-systemd.automount" "noauto" "uid=1002" "exec" ];    # I'm using automount here to skip mount on boot, which slows startup
  options = [ "x-systemd.automount" "noauto" "uid=1002" "exec" ];    # I'm using automount here to skip mount on boot, which slows startup
  };
};
 
 
  # this one should mount the actual directory from the root view of backup
# this one should mount the actual directory from the root view of backup
  fileSystems."/run/user/1002/home-danbst" = {
fileSystems."/run/user/1002/home-danbst" = {
    device = "/run/user/1002/borg-home-danbst/home/danbst";
  device = "/run/user/1002/borg-home-danbst/home/danbst";
    options = [ "bind" ];
  options = [ "bind" ];
  };
};


  environment.systemPackages = [
environment.systemPackages = [
    ...
  ...
    (pkgs.writeScriptBin "mount.fuse.borgfs" ''
  (pkgs.writeScriptBin "mount.fuse.borgfs" ''
      #!/bin/sh
    #!/bin/sh
      export BORG_RSH="ssh -i /home/danbst/.ssh/id_ed25519"
    export BORG_RSH="ssh -i /home/danbst/.ssh/id_ed25519"
      export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
    export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
      export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
    export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
      exec ${pkgs.borgbackup}/bin/borgfs "$@"
    exec ${pkgs.borgbackup}/bin/borgfs "$@"
    '')
  '')
  ];
];
</syntaxHighlight>
</syntaxHighlight>