deployment.rst 7.6 KB

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