central-backup-server.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. .. _central-backup-server:
  4. Central repository server with Ansible or Salt
  5. ==============================================
  6. This section will give an example how to set up a borg repository server for multiple
  7. clients.
  8. Machines
  9. --------
  10. There are multiple machines used in this section 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. The client will be able to access any file or subdirectory inside of ``/home/backup/repos/<client fqdn>``
  41. but no other directories. You can allow a client to access several separate directories by passing multiple
  42. ``--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>``,
  43. which could make sense if multiple machines belong to one person which should then have access to all the
  44. backups of their machines.
  45. There is only one ssh key per client allowed. Keys are added for ``johndoe.clnt.local``, ``web01.srv.local`` and
  46. ``app01.srv.local``. But they will access the backup under only one UNIX user account as:
  47. ``backup@backup01.srv.local``. Every key in ``$HOME/.ssh/authorized_keys`` has a
  48. forced command and restrictions applied as shown below:
  49. ::
  50. command="cd /home/backup/repos/<client fqdn>;
  51. borg serve --restrict-to-path /home/backup/repos/<client fqdn>",
  52. restrict <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. ::
  68. borg init backup@backup01.srv.local:pictures
  69. Or with the full path (should actually never be used, as only for demonstration purposes).
  70. The server should automatically change the current working directory to the `<client fqdn>` folder.
  71. ::
  72. borg init backup@backup01.srv.local:/home/backup/repos/johndoe.clnt.local/pictures
  73. When `johndoe.clnt.local` tries to access a not restricted path the following error is raised.
  74. John Doe tries to back up into the Web 01 path:
  75. ::
  76. borg init backup@backup01.srv.local:/home/backup/repos/web01.srv.local/pictures
  77. ::
  78. ~~~ SNIP ~~~
  79. Remote: borg.remote.PathNotAllowed: /home/backup/repos/web01.srv.local/pictures
  80. ~~~ SNIP ~~~
  81. Repository path not allowed
  82. Ansible
  83. -------
  84. Ansible takes care of all the system-specific commands to add the user, create the
  85. folder, install and configure software.
  86. ::
  87. - hosts: backup01.srv.local
  88. vars:
  89. user: backup
  90. group: backup
  91. home: /home/backup
  92. pool: "{{ home }}/repos"
  93. auth_users:
  94. - host: johndoe.clnt.local
  95. key: "{{ lookup('file', '/path/to/keys/johndoe.clnt.local.pub') }}"
  96. - host: web01.clnt.local
  97. key: "{{ lookup('file', '/path/to/keys/web01.clnt.local.pub') }}"
  98. - host: app01.clnt.local
  99. key: "{{ lookup('file', '/path/to/keys/app01.clnt.local.pub') }}"
  100. tasks:
  101. - package: name=borg state=present
  102. - group: name="{{ group }}" state=present
  103. - user: name="{{ user }}" shell=/bin/bash home="{{ home }}" createhome=yes group="{{ group }}" groups= state=present
  104. - file: path="{{ home }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  105. - file: path="{{ home }}/.ssh" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  106. - file: path="{{ pool }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  107. - authorized_key: user="{{ user }}"
  108. key="{{ item.key }}"
  109. key_options='command="cd {{ pool }}/{{ item.host }};borg serve --restrict-to-path {{ pool }}/{{ item.host }}",restrict'
  110. with_items: "{{ auth_users }}"
  111. - file: path="{{ home }}/.ssh/authorized_keys" owner="{{ user }}" group="{{ group }}" mode=0600 state=file
  112. - file: path="{{ pool }}/{{ item.host }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  113. with_items: "{{ auth_users }}"
  114. Salt
  115. ----
  116. This is a configuration similar to the one above, configured to be deployed with
  117. Salt running on a Debian system.
  118. ::
  119. Install borg backup from pip:
  120. pkg.installed:
  121. - pkgs:
  122. - python3
  123. - python3-dev
  124. - python3-pip
  125. - python-virtualenv
  126. - libssl-dev
  127. - openssl
  128. - libacl1-dev
  129. - libacl1
  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. - restrict
  154. {% endfor %}
  155. Enhancements
  156. ------------
  157. As this section only describes a simple and effective setup, it could be further
  158. enhanced when supporting (a limited set) of client supplied commands. A wrapper
  159. for starting `borg serve` could be written. Or borg itself could be enhanced to
  160. autodetect it runs under SSH by checking the `SSH_ORIGINAL_COMMAND` environment
  161. variable. This is left open for future improvements.
  162. When extending ssh autodetection in borg no external wrapper script is necessary
  163. and no other interpreter or application has to be deployed.
  164. See also
  165. --------
  166. * `SSH Daemon manpage <https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man8/sshd.8>`_
  167. * `Ansible <https://docs.ansible.com>`_
  168. * `Salt <https://docs.saltstack.com/>`_