deployment.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. 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. no-port-forwarding,no-X11-forwarding,no-pty,
  53. no-agent-forwarding,no-user-rc <keytype> <key> <host>
  54. .. note:: The text shown above needs to be written on a single line!
  55. The options which are added to the key will perform the following:
  56. 1. Change working directory
  57. 2. Run ``borg serve`` restricted to the client base path
  58. 3. Restrict ssh and do not allow stuff which imposes a security risk
  59. Due to the ``cd`` command we use, the server automatically changes the current
  60. working directory. Then client doesn't need to have knowledge of the absolute
  61. or relative remote repository path and can directly access the repositories at
  62. ``<user>@<host>:<repo>``.
  63. .. note:: The setup above ignores all client given commandline parameters
  64. which are normally appended to the `borg serve` command.
  65. Client
  66. ------
  67. The client needs to initialize the `pictures` repository like this:
  68. borg init backup@backup01.srv.local:pictures
  69. Or with the full path (should actually never be used, as only for demonstrational purposes).
  70. The server should automatically change the current working directory to the `<client fqdn>` folder.
  71. borg init backup@backup01.srv.local:/home/backup/repos/johndoe.clnt.local/pictures
  72. When `johndoe.clnt.local` tries to access a not restricted path the following error is raised.
  73. John Doe tries to backup into the Web 01 path:
  74. borg init backup@backup01.srv.local:/home/backup/repos/web01.srv.local/pictures
  75. ::
  76. ~~~ SNIP ~~~
  77. Remote: borg.remote.PathNotAllowed: /home/backup/repos/web01.srv.local/pictures
  78. ~~~ SNIP ~~~
  79. Repository path not allowed
  80. Ansible
  81. -------
  82. Ansible takes care of all the system-specific commands to add the user, create the
  83. folder. Even when the configuration is changed the repository server configuration is
  84. satisfied and reproducible.
  85. Automate setting up an repository server with the user, group, folders and
  86. permissions a Ansible playbook could be used. Keep in mind the playbook
  87. uses the Arch Linux `pacman <https://www.archlinux.org/pacman/pacman.8.html>`_
  88. package manager to install and keep borg up-to-date.
  89. ::
  90. - hosts: backup01.srv.local
  91. vars:
  92. user: backup
  93. group: backup
  94. home: /home/backup
  95. pool: "{{ home }}/repos"
  96. auth_users:
  97. - host: johndoe.clnt.local
  98. key: "{{ lookup('file', '/path/to/keys/johndoe.clnt.local.pub') }}"
  99. - host: web01.clnt.local
  100. key: "{{ lookup('file', '/path/to/keys/web01.clnt.local.pub') }}"
  101. - host: app01.clnt.local
  102. key: "{{ lookup('file', '/path/to/keys/app01.clnt.local.pub') }}"
  103. tasks:
  104. - pacman: name=borg state=latest update_cache=yes
  105. - group: name="{{ group }}" state=present
  106. - user: name="{{ user }}" shell=/bin/bash home="{{ home }}" createhome=yes group="{{ group }}" groups= state=present
  107. - file: path="{{ home }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  108. - file: path="{{ home }}/.ssh" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  109. - file: path="{{ pool }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  110. - authorized_key: user="{{ user }}"
  111. key="{{ item.key }}"
  112. 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'
  113. with_items: "{{ auth_users }}"
  114. - file: path="{{ home }}/.ssh/authorized_keys" owner="{{ user }}" group="{{ group }}" mode=0600 state=file
  115. - file: path="{{ pool }}/{{ item.host }}" owner="{{ user }}" group="{{ group }}" mode=0700 state=directory
  116. with_items: "{{ auth_users }}"
  117. Enhancements
  118. ------------
  119. As this chapter only describes a simple and effective setup it could be further
  120. enhanced when supporting (a limited set) of client supplied commands. A wrapper
  121. for starting `borg serve` could be written. Or borg itself could be enhanced to
  122. autodetect it runs under SSH by checking the `SSH_ORIGINAL_COMMAND` environment
  123. variable. This is left open for future improvements.
  124. When extending ssh autodetection in borg no external wrapper script is necessary
  125. and no other interpreter or application has to be deployed.
  126. See also
  127. --------
  128. * `SSH Daemon manpage <http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man8/sshd.8>`_
  129. * `Ansible <https://docs.ansible.com>`_