about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/backup/borgbackup.md
blob: 39141f6ec8587e7f2f0a045505850277b225094a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# BorgBackup {#module-borgbase}

*Source:* {file}`modules/services/backup/borgbackup.nix`

*Upstream documentation:* <https://borgbackup.readthedocs.io/>

[BorgBackup](https://www.borgbackup.org/) (short: Borg)
is a deduplicating backup program. Optionally, it supports compression and
authenticated encryption.

The main goal of Borg is to provide an efficient and secure way to backup
data. The data deduplication technique used makes Borg suitable for daily
backups since only changes are stored. The authenticated encryption technique
makes it suitable for backups to not fully trusted targets.

## Configuring {#module-services-backup-borgbackup-configuring}

A complete list of options for the Borgbase module may be found
[here](#opt-services.borgbackup.jobs).

## Basic usage for a local backup {#opt-services-backup-borgbackup-local-directory}

A very basic configuration for backing up to a locally accessible directory is:
```
{
    opt.services.borgbackup.jobs = {
      { rootBackup = {
          paths = "/";
          exclude = [ "/nix" "/path/to/local/repo" ];
          repo = "/path/to/local/repo";
          doInit = true;
          encryption = {
            mode = "repokey";
            passphrase = "secret";
          };
          compression = "auto,lzma";
          startAt = "weekly";
        };
      }
    };
}
```

::: {.warning}
If you do not want the passphrase to be stored in the world-readable
Nix store, use passCommand. You find an example below.
:::

## Create a borg backup server {#opt-services-backup-create-server}

You should use a different SSH key for each repository you write to,
because the specified keys are restricted to running borg serve and can only
access this single repository. You need the output of the generate pub file.

```ShellSession
# sudo ssh-keygen -N '' -t ed25519 -f /run/keys/id_ed25519_my_borg_repo
# cat /run/keys/id_ed25519_my_borg_repo
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID78zmOyA+5uPG4Ot0hfAy+sLDPU1L4AiIoRYEIVbbQ/ root@nixos
```

Add the following snippet to your NixOS configuration:
```
{
  services.borgbackup.repos = {
    my_borg_repo = {
      authorizedKeys = [
        "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID78zmOyA+5uPG4Ot0hfAy+sLDPU1L4AiIoRYEIVbbQ/ root@nixos"
      ] ;
      path = "/var/lib/my_borg_repo" ;
    };
  };
}
```

## Backup to the borg repository server {#opt-services-backup-borgbackup-remote-server}

The following NixOS snippet creates an hourly backup to the service
(on the host nixos) as created in the section above. We assume
that you have stored a secret passphrasse in the file
{file}`/run/keys/borgbackup_passphrase`, which should be only
accessible by root

```
{
  services.borgbackup.jobs = {
    backupToLocalServer = {
      paths = [ "/etc/nixos" ];
      doInit = true;
      repo =  "borg@nixos:." ;
      encryption = {
        mode = "repokey-blake2";
        passCommand = "cat /run/keys/borgbackup_passphrase";
      };
      environment = { BORG_RSH = "ssh -i /run/keys/id_ed25519_my_borg_repo"; };
      compression = "auto,lzma";
      startAt = "hourly";
    };
  };
};
```

The following few commands (run as root) let you test your backup.
```
> nixos-rebuild switch
...restarting the following units: polkit.service
> systemctl restart borgbackup-job-backupToLocalServer
> sleep 10
> systemctl restart borgbackup-job-backupToLocalServer
> export BORG_PASSPHRASE=topSecrect
> borg list --rsh='ssh -i /run/keys/id_ed25519_my_borg_repo' borg@nixos:.
nixos-backupToLocalServer-2020-03-30T21:46:17 Mon, 2020-03-30 21:46:19 [84feb97710954931ca384182f5f3cb90665f35cef214760abd7350fb064786ac]
nixos-backupToLocalServer-2020-03-30T21:46:30 Mon, 2020-03-30 21:46:32 [e77321694ecd160ca2228611747c6ad1be177d6e0d894538898de7a2621b6e68]
```

## Backup to a hosting service {#opt-services-backup-borgbackup-borgbase}

Several companies offer [(paid) hosting services](https://www.borgbackup.org/support/commercial.html)
for Borg repositories.

To backup your home directory to borgbase you have to:

  - Generate a SSH key without a password, to access the remote server. E.g.

        sudo ssh-keygen -N '' -t ed25519 -f /run/keys/id_ed25519_borgbase

  - Create the repository on the server by following the instructions for your
    hosting server.
  - Initialize the repository on the server. Eg.

        sudo borg init --encryption=repokey-blake2  \
            --rsh "ssh -i /run/keys/id_ed25519_borgbase" \
            zzz2aaaaa@zzz2aaaaa.repo.borgbase.com:repo

  - Add it to your NixOS configuration, e.g.

        {
            services.borgbackup.jobs = {
            my_Remote_Backup = {
                paths = [ "/" ];
                exclude = [ "/nix" "'**/.cache'" ];
                repo =  "zzz2aaaaa@zzz2aaaaa.repo.borgbase.com:repo";
                  encryption = {
                  mode = "repokey-blake2";
                  passCommand = "cat /run/keys/borgbackup_passphrase";
                };
                environment = { BORG_RSH = "ssh -i /run/keys/id_ed25519_borgbase"; };
                compression = "auto,lzma";
                startAt = "daily";
            };
          };
        }}

## Vorta backup client for the desktop {#opt-services-backup-borgbackup-vorta}

Vorta is a backup client for macOS and Linux desktops. It integrates the
mighty BorgBackup with your desktop environment to protect your data from
disk failure, ransomware and theft.

It can be installed in NixOS e.g. by adding `pkgs.vorta`
to [](#opt-environment.systemPackages).

Details about using Vorta can be found under
[https://vorta.borgbase.com](https://vorta.borgbase.com/usage) .