Borg backup: Difference between revisions

imported>Danbst
Created page with "Borg is a backup tool to perform incremental backups, local or remote. <syntaxHighlight lang=bash> $ nix-env -iA nixpkgs.borgbackup </syntaxHighlight> To be able to do remot..."
 
imported>Danbst
No edit summary
Line 80: Line 80:


After doing at least one successful backup don't forget to test mount it (see next)
After doing at least one successful backup don't forget to test mount it (see next)
== Notifications when backup fails ==
Quite often backups do fail. To perform notifications about this situations, you can setup autonotifier for all NixOS borg jobs. This requires creating a separate module, but can be also done inplace in <code>/etc/nixos/configuration.nix</code>
Note, that example below was for Gnome-shell desktop! For other desktops it may require changes for how to get DBUS session properly!
<syntaxHighlight lang=nix>
{ pkgs, config, lib, ... }:
let
  borgbackupMonitor = { config, pkgs, lib, ... }: with lib; {
    key = "borgbackupMonitor";
    _file = "borgbackupMonitor";
    config.systemd.services = {
      "notify-problems@" = {
        enable = true;
        serviceConfig.User = "danbst";
        environment.SERVICE = "%i";
        script = ''
          export $(cat /proc/$(${pkgs.procps}/bin/pgrep "gnome-session" -u "$USER")/environ |grep -z '^DBUS_SESSION_BUS_ADDRESS=')
          ${pkgs.libnotify}/bin/notify-send -u critical "$SERVICE FAILED!" "Run journalctl -u $SERVICE for details"
        '';
      };
    } // flip mapAttrs' config.services.borgbackup.jobs (name: value:
      nameValuePair "borgbackup-job-${name}" {
        unitConfig.OnFailure = "notify-problems@%i.service";
      }
    );
   
    # optional, but this actually forces backup after boot in case laptop was powered off during scheduled event
    # for example, if you scheduled backups daily, your laptop should be powered on at 00:00
    config.systemd.timers = flip mapAttrs' config.services.borgbackup.jobs (name: value:
      nameValuePair "borgbackup-job-${name}" {
        timerConfig.Persistent = true;
      }
    );
  };
in {
  imports =
    [
      ....
      borgbackupMonitor
    ];
  ...
}
</syntaxHighlight>
== Don't try backup when network is unreachable ==
With persistent timers above you can get into a problem that after reboot backup is tried too fast, even when network is not yet available, and thus fails. This can be solved with systemd failed restart, or using internet-ready check in <code>preStart</code> script.
Patching previous example:
<syntaxHighlight lang=nix>
    } // flip mapAttrs' config.services.borgbackup.jobs (name: value:
      nameValuePair "borgbackup-job-${name}" {
        unitConfig.OnFailure = "notify-problems@%i.service";
        preStart = lib.mkBefore ''
          # waiting for internet after resume-from-suspend
          until /run/wrappers/bin/ping google.com -c1 -q >/dev/null; do :; done
        '';
      }
    );
    ...
</syntaxHighlight>


== Mounting point-in-time archives ==
== Mounting point-in-time archives ==