File: central-backup-server.rst

package info (click to toggle)
borgbackup2 2.0.0b20-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,852 kB
  • sloc: python: 33,830; pascal: 3,599; sh: 215; makefile: 156; tcl: 94; ansic: 21
file content (220 lines) | stat: -rw-r--r-- 7,738 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
.. include:: ../global.rst.inc
.. highlight:: none
.. _central-backup-server:

Central repository server with Ansible or Salt
==============================================

This section gives an example of how to set up a Borg repository server for multiple
clients.

Machines
--------

This section uses multiple machines, referred to by their
respective fully qualified domain names (FQDNs).

* The backup server: `backup01.srv.local`
* The clients:

  - John Doe's desktop: `johndoe.clnt.local`
  - Web server 01: `web01.srv.local`
  - Application server 01: `app01.srv.local`

User and group
--------------

The repository server should have a single UNIX user for all the clients.
Recommended user and group with additional settings:

* User: `backup`
* Group: `backup`
* Shell: `/bin/bash` (or another shell capable of running the `borg serve` command)
* Home: `/home/backup`

Most clients should initiate a backup as the root user to capture all
users, groups, and permissions (e.g., when backing up `/home`).

Folders
-------

The following directory layout is suggested on the repository server:

* User home directory, /home/backup
* Repositories path (storage pool): /home/backup/repos
* Clients’ restricted paths (`/home/backup/repos/<client fqdn>`):

  - johndoe.clnt.local: `/home/backup/repos/johndoe.clnt.local`
  - web01.srv.local: `/home/backup/repos/web01.srv.local`
  - app01.srv.local: `/home/backup/repos/app01.srv.local`

Restrictions
------------

Borg is instructed to restrict clients into their own paths:
``borg serve --restrict-to-path /home/backup/repos/<client fqdn>``

The client will be able to access any file or subdirectory inside of ``/home/backup/repos/<client fqdn>``
but no other directories. You can allow a client to access several separate directories by passing multiple
``--restrict-to-path`` flags, for instance: ``borg serve --restrict-to-path /home/backup/repos/<client fqdn> --restrict-to-path /home/backup/repos/<other client fqdn>``,
which could make sense if multiple machines belong to one person which should then have access to all the
backups of their machines.

Only one SSH key per client is allowed. Keys are added for ``johndoe.clnt.local``, ``web01.srv.local`` and
``app01.srv.local``. They will access the backup under a single UNIX user account as
``backup@backup01.srv.local``. Every key in ``$HOME/.ssh/authorized_keys`` has a
forced command and restrictions applied, as shown below:

::

  command="cd /home/backup/repos/<client fqdn>;
           borg serve --restrict-to-path /home/backup/repos/<client fqdn>",
           restrict <keytype> <key> <host>

.. note:: The text shown above needs to be written on a single line!

The options added to the key perform the following:

1. Change working directory
2. Run ``borg serve`` restricted to the client base path
3. Restrict ssh and do not allow stuff which imposes a security risk

Because of the ``cd`` command, the server automatically changes the current
working directory. The client then does not need to know the absolute
or relative remote repository path and can directly access the repositories at
``ssh://<user>@<host>/./<repo>``.

.. note:: The setup above ignores all client-given command line parameters
          that are normally appended to the `borg serve` command.

Client
------

The client needs to initialize the `pictures` repository like this:

::

 borg init ssh://backup@backup01.srv.local/./pictures

Or with the full path (this should not be used in practice; it is only for demonstration purposes).
The server automatically changes the current working directory to the `<client fqdn>` directory.

::

  borg init ssh://backup@backup01.srv.local/home/backup/repos/johndoe.clnt.local/pictures

When `johndoe.clnt.local` tries to access a path outside its restriction, the following error is raised.
John Doe tries to back up into the web01 path:

::

  borg init ssh://backup@backup01.srv.local/home/backup/repos/web01.srv.local/pictures

::

  ~~~ SNIP ~~~
  Remote: borg.remote.PathNotAllowed: /home/backup/repos/web01.srv.local/pictures
  ~~~ SNIP ~~~
  Repository path not allowed

Ansible
-------

Ansible takes care of all the system-specific commands to add the user, create the
folder, install and configure software.

::

  - hosts: backup01.srv.local
    vars:
      user: backup
      group: backup
      home: /home/backup
      pool: "{{ home }}/repos"
      auth_users:
        - host: johndoe.clnt.local
          key: "{{ lookup('file', '/path/to/keys/johndoe.clnt.local.pub') }}"
        - host: web01.clnt.local
          key: "{{ lookup('file', '/path/to/keys/web01.clnt.local.pub') }}"
        - host: app01.clnt.local
          key: "{{ lookup('file', '/path/to/keys/app01.clnt.local.pub') }}"
    tasks:
    - package: name=borg state=present
    - group: name="{{ group }}" state=present
    - user: name="{{ user }}" shell=/bin/bash home="{{ home }}" createhome=yes group="{{ group }}" groups= state=present
    - file: path="{{ home }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
    - file: path="{{ home }}/.ssh" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
    - file: path="{{ pool }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
    - authorized_key: user="{{ user }}"
                      key="{{ item.key }}"
                      key_options='command="cd {{ pool }}/{{ item.host }};borg serve --restrict-to-path {{ pool }}/{{ item.host }}",restrict'
      with_items: "{{ auth_users }}"
    - file: path="{{ home }}/.ssh/authorized_keys" owner="{{ user }}" group="{{ group }}" mode=0600 state=file
    - file: path="{{ pool }}/{{ item.host }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
      with_items: "{{ auth_users }}"

Salt
----

This is a configuration similar to the one above, configured to be deployed with
Salt running on a Debian system.

::

  Install borg backup from pip:
    pkg.installed:
      - pkgs:
        - python3
        - python3-dev
        - python3-pip
        - python-virtualenv
        - libssl-dev
        - openssl
        - libacl1-dev
        - libacl1
        - build-essential
        - libfuse-dev
        - fuse
        - pkg-config
    pip.installed:
      - pkgs: ["borgbackup"]
      - bin_env: /usr/bin/pip3

  Setup backup user:
    user.present:
      - name: backup
      - fullname: Backup User
      - home: /home/backup
      - shell: /bin/bash
  # CAUTION!
  # If you change the ssh command= option below, it won't necessarily get pushed to the backup
  # server correctly unless you delete the ~/.ssh/authorized_keys file and re-create it!
  {% for host in backupclients %}
  Give backup access to {{host}}:
    ssh_auth.present:
      - user: backup
      - source: salt://conf/ssh-pubkeys/{{host}}-backup.id_ecdsa.pub
      - options:
        - command="cd /home/backup/repos/{{host}}; borg serve --restrict-to-path /home/backup/repos/{{host}}"
        - restrict
  {% endfor %}


Enhancements
------------

As this section only describes a simple and effective setup, it could be further
enhanced when supporting (a limited set) of client supplied commands. A wrapper
for starting `borg serve` could be written. Or borg itself could be enhanced to
autodetect it runs under SSH by checking the `SSH_ORIGINAL_COMMAND` environment
variable. This is left open for future improvements.

When extending ssh autodetection in borg no external wrapper script is necessary
and no other interpreter or application has to be deployed.

See also
--------

* `SSH Daemon manpage <https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man8/sshd.8>`_
* `Ansible <https://docs.ansible.com>`_
* `Salt <https://docs.saltstack.com/>`_