Borg backup: Difference between revisions
imported>Danbst No edit summary |
m fix indentation |
||
| (11 intermediate revisions by 7 users not shown) | |||
| Line 1: | Line 1: | ||
Borg is a backup | [https://www.borgbackup.org/ BorgBackup] (short: Borg) is a deduplicating incremental backup program for local and remote data. Optionally, it supports compression and authenticated encryption. | ||
This wiki article extends the documentation in the [https://nixos.org/manual/nixos/stable/#module-borgbase NixOS manual]. | |||
== Installation == | |||
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 12: | Line 21: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
services.borgbackup.jobs.home-danbst = { | |||
paths = "/home/danbst"; | |||
encryption.mode = "none"; | |||
environment.BORG_RSH = "ssh -i /home/danbst/.ssh/id_ed25519"; | |||
repo = "ssh://user@example.com:23/path/to/backups-dir/home-danbst"; | |||
compression = "auto,zstd"; | |||
startAt = "daily"; | |||
}; | |||
</syntaxHighlight> | </syntaxHighlight> | ||
| Line 29: | Line 38: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
services.borgbackup.jobs = | |||
let | |||
common-excludes = [ | |||
# Largest cache dirs | |||
".cache" | |||
"*/cache2" # firefox | |||
"*/Cache" | |||
".config/Slack/logs" | |||
".config/Code/CachedData" | |||
".container-diff" | |||
".npm/_cacache" | |||
# Work related dirs | |||
"*/node_modules" | |||
"*/bower_components" | |||
"*/_build" | |||
"*/.tox" | |||
"*/venv" | |||
"*/.venv" | |||
]; | |||
work-dirs = [ | |||
"/home/danbst/dev/company1" | |||
"/home/danbst/dev/company2" | |||
]; | |||
basicBorgJob = name: { | |||
encryption.mode = "none"; | |||
environment.BORG_RSH = "ssh -o 'StrictHostKeyChecking=no' -i /home/danbst/.ssh/id_ed25519"; | |||
environment.BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK = "yes"; | |||
extraCreateArgs = "--verbose --stats --checkpoint-interval 600"; | |||
repo = "ssh://user@example.com//media/backup/${name}"; | |||
compression = "zstd,1"; | |||
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" ]); | ||
}; | }; | ||
home-danbst-downloads = basicBorgJob "backups/station/home-danbst-downloads" // rec { | home-danbst-downloads = basicBorgJob "backups/station/home-danbst-downloads" // rec { | ||
| Line 136: | Line 145: | ||
<syntaxHighlight lang=nix> | <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> | </syntaxHighlight> | ||
| Line 152: | Line 161: | ||
First, check if there are any archives: | First, check if there are any archives: | ||
<syntaxHighlight lang= | <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 166: | Line 175: | ||
Choose one of "archives" and mount it locally: | Choose one of "archives" and mount it locally: | ||
<syntaxHighlight lang= | <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 180: | Line 189: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
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"; | |||
noCheck = true; | |||
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 | |||
}; | |||
# this one should mount the actual directory from the root view of backup | |||
fileSystems."/run/user/1002/home-danbst" = { | |||
device = "/run/user/1002/borg-home-danbst/home/danbst"; | |||
options = [ "bind" ]; | |||
}; | |||
environment.systemPackages = [ | |||
... | |||
(pkgs.writeScriptBin "mount.fuse.borgfs" '' | |||
#!/bin/sh | |||
export BORG_RSH="ssh -i /home/danbst/.ssh/id_ed25519" | |||
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes | |||
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes | |||
exec ${pkgs.borgbackup}/bin/borgfs "$@" | |||
'') | |||
]; | |||
</syntaxHighlight> | </syntaxHighlight> | ||
If anybody reading this have found a way to mount as a user properly, please update the code above. | If anybody reading this have found a way to mount as a user properly, please update the code above. | ||
[[Category:Applications]] | |||
[[Category:Backup]] | |||
[[Category:NixOS Manual]] | |||
[[Category:Cookbook]] | |||