central-backup-server.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. Central repository server with Ansible or Salt
  4. ==============================================
  5. This section will give an example how to setup a borg repository server for multiple
  6. clients.
  7. Machines
  8. --------
  9. There are multiple machines used in this section and will further be named by their
  10. respective fully qualified domain name (fqdn).
  11. * The backup server: `backup01.srv.local`
  12. * The clients:
  13. - John Doe's desktop: `johndoe.clnt.local`
  14. - Webserver 01: `web01.srv.local`
  15. - Application server 01: `app01.srv.local`
  16. User and group
  17. --------------
  18. The repository server needs to have only one UNIX user for all the clients.
  19. Recommended user and group with additional settings:
  20. * User: `backup`
  21. * Group: `backup`
  22. * Shell: `/bin/bash` (or other capable to run the `borg serve` command)
  23. * Home: `/home/backup`
  24. Most clients shall initiate a backup from the root user to catch all
  25. users, groups and permissions (e.g. when backing up `/home`).
  26. Folders
  27. -------
  28. The following folder tree layout is suggested on the repository server:
  29. * User home directory, /home/backup
  30. * Repositories path (storage pool): /home/backup/repos
  31. * Clients restricted paths (`/home/backup/repos/<client fqdn>`):
  32. - johndoe.clnt.local: `/home/backup/repos/johndoe.clnt.local`
  33. - web01.srv.local: `/home/backup/repos/web01.srv.local`
  34. - app01.srv.local: `/home/backup/repos/app01.srv.local`
  35. Restrictions
  36. ------------
  37. Borg is instructed to restrict clients into their own paths:
  38. ``borg serve --restrict-to-path /home/backup/repos/<client fqdn>``
  39. The client will be able to access any file or subdirectory inside of ``/home/backup/repos/<client fqdn>``
  40. but no other directories. You can allow a client to access several separate directories by passing multiple
  41. `--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>``,
  42. which could make sense if multiple machines belong to one person which should then have access to all the
  43. backups of their machines.
  44. There is only one ssh key per client allowed. Keys are added for ``johndoe.clnt.local``, ``web01.srv.local`` and
  45. ``app01.srv.local``. But they will access the backup under only one UNIX user account as:
  46. ``backup@backup01.srv.local``. Every key in ``$HOME/.ssh/authorized_keys`` has a
  47. forced command and restrictions applied as shown below:
  48. ::
  49. command="cd /home/backup/repos/<client fqdn>;
  50. borg serve --restrict-to-path /home/backup/repos/<client fqdn>",
  51. no-port-forwarding,no-X11-forwarding,no-pty,
  52. no-agent-forwarding,no-user-rc <keytype> <key> <host>
  53. .. note:: The text shown above needs to be written on a single line!
  54. The options which are added to the key will perform the following:
  55. 1. Change working directory
  56. 2. Run ``borg serve`` restricted to the client base path
  57. 3. Restrict ssh and do not allow stuff which imposes a security risk
  58. Due to the ``cd`` command we use, the server automatically changes the current
  59. working directory. Then client doesn't need to have knowledge of the absolute
  60. or relative remote repository path and can directly access the repositories at
  61. ``<user>@<host>:<repo>``.
  62. .. note:: The setup above ignores all client given commandline parameters
  63. which are normally appended to the `borg serve` command.
  64. Client
  65. ------
  66. The client needs to initialize the `pictures` repository like this:
  67. borg init backup@backup01.srv.local:pictures
  68. Or with the full path (should actually never be used, as only for demonstrational purposes).
  69. The server should automatically change the current working directory to the `<client fqdn>` folder.
  70. borg init backup@backup01.srv.local:/home/backup/repos/johndoe.clnt.local/pictures
  71. When `johndoe.clnt.local` tries to access a not restricted path the following error is raised.
  72. John Doe tries to backup into the Web 01 path:
  73. borg init backup@backup01.srv.local:/home/backup/repos/web01.srv.local/pictures
  74. ::
  75. ~~~ SNIP ~~~
  76. Remote: borg.remote.PathNotAllowed: /home/backup/repos/web01.srv.local/pictures
  77. ~~~ SNIP ~~~
  78. Repository path not allowed
  79. Ansible
  80. -------
  81. Ansible takes care of all the system-specific commands to add the user, create the
  82. folder. Even when the configuration is changed the repository server configuration is
  83. satisfied and reproducible.
  84. Automate setting up an repository server with the user, group, folders and
  85. permissions a Ansible playbook could be used. Keep in mind the playbook
  86. uses the Arch Linux `pacman <https://www.archlinux.org/pacman/pacman.8.html>`_
  87. package manager to install and keep borg up-to-date.
  88. ::
  89. - hosts: backup01.srv.local
  90. vars:
  91. user: backup
  92. group: backup
  93. home: /home/backup
  94. pool: "{{ home }}/repos"
  95. auth_users:
  96. - host: johndoe.clnt.local
  97. key: "{{ lookup('file', '/path/to/keys/johndoe.clnt.local.pub') }}"
  98. - host: web01.clnt.local
  99. key: "{{ lookup('file', '/path/to/keys/web01.clnt.local.pub') }}"
  100. - host: app01.clnt.local
  101. key: "{{ lookup('file', '/path/to/keys/app01.clnt.local.pub') }}"
  102. tasks:
  103. - pacman: name=borg state=latest update_cache=yes
  104. - group: name="{{ group }}" state=present
  105. - user: name="{{ user }}" shell=/bin/bash home="{{ home }}" createhome=yes group="{{ group }}" groups= state=present
  106. - file: path="{{ home }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  107. - file: path="{{ home }}/.ssh" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  108. - file: path="{{ pool }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  109. - authorized_key: user="{{ user }}"
  110. key="{{ item.key }}"
  111. key_options='command="cd {{ pool }}/{{ item.host }};borg serve --restrict-to-path {{ pool }}/{{ item.host }}",no-port-forwarding,no-X11-forwarding,no-pty,no-agent-forwarding,no-user-rc'
  112. with_items: "{{ auth_users }}"
  113. - file: path="{{ home }}/.ssh/authorized_keys" owner="{{ user }}" group="{{ group }}" mode=0600 state=file
  114. - file: path="{{ pool }}/{{ item.host }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  115. with_items: "{{ auth_users }}"
  116. Salt
  117. ----
  118. This is a configuration similar to the one above, configured to be deployed with
  119. Salt running on a Debian system.
  120. ::
  121. Install borg backup from pip:
  122. pkg.installed:
  123. - pkgs:
  124. - python3
  125. - python3-dev
  126. - python3-pip
  127. - python-virtualenv
  128. - libssl-dev
  129. - openssl
  130. - libacl1-dev
  131. - libacl1
  132. - liblz4-dev
  133. - liblz4-1
  134. - build-essential
  135. - libfuse-dev
  136. - fuse
  137. - pkg-config
  138. pip.installed:
  139. - pkgs: ["borgbackup"]
  140. - bin_env: /usr/bin/pip3
  141. Setup backup user:
  142. user.present:
  143. - name: backup
  144. - fullname: Backup User
  145. - home: /home/backup
  146. - shell: /bin/bash
  147. # CAUTION!
  148. # If you change the ssh command= option below, it won't necessarily get pushed to the backup
  149. # server correctly unless you delete the ~/.ssh/authorized_keys file and re-create it!
  150. {% for host in backupclients %}
  151. Give backup access to {{host}}:
  152. ssh_auth.present:
  153. - user: backup
  154. - source: salt://conf/ssh-pubkeys/{{host}}-backup.id_ecdsa.pub
  155. - options:
  156. - command="cd /home/backup/repos/{{host}}; borg serve --restrict-to-path /home/backup/repos/{{host}}"
  157. - no-port-forwarding
  158. - no-X11-forwarding
  159. - no-pty
  160. - no-agent-forwarding
  161. - no-user-rc
  162. {% endfor %}
  163. Enhancements
  164. ------------
  165. As this section only describes a simple and effective setup it could be further
  166. enhanced when supporting (a limited set) of client supplied commands. A wrapper
  167. for starting `borg serve` could be written. Or borg itself could be enhanced to
  168. autodetect it runs under SSH by checking the `SSH_ORIGINAL_COMMAND` environment
  169. variable. This is left open for future improvements.
  170. When extending ssh autodetection in borg no external wrapper script is necessary
  171. and no other interpreter or application has to be deployed.
  172. See also
  173. --------
  174. * `SSH Daemon manpage <http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man8/sshd.8>`_
  175. * `Ansible <https://docs.ansible.com>`_
  176. * `Salt <https://docs.saltstack.com/>`_