deployment.rst 6.1 KB

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