non-root-user.rst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. .. _non_root_user:
  4. ================================
  5. Backing up using a non-root user
  6. ================================
  7. This section describes how to run borg as a non-root user and still be able to
  8. backup every file on the system.
  9. Normally borg is run as the root user to bypass all filesystem permissions and
  10. be able to read all files. But in theory this also allows borg to modify or
  11. delete files on your system, in case of a bug for example.
  12. To eliminate this possibility, we can run borg as a non-root user and give it read-only
  13. permissions to all files on the system.
  14. Using Linux capabilities inside a systemd service
  15. =================================================
  16. One way to do so, is to use linux `capabilities
  17. <https://man7.org/linux/man-pages/man7/capabilities.7.html>`_ within a systemd
  18. service.
  19. Linux capabilities allow us to give parts of the privileges the root user has to
  20. a non-root user. This works on a per-thread level and does not give the permission
  21. to the non-root user as a whole.
  22. For this we need to run our backup script from a systemd service and use the `AmbientCapabilities
  23. <https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#AmbientCapabilities=>`_
  24. option added in systemd 229.
  25. A very basic unit file would look like this:
  26. ::
  27. [Unit]
  28. Description=Borg Backup
  29. [Service]
  30. Type=oneshot
  31. User=borg
  32. ExecStart=/usr/local/sbin/backup.sh
  33. AmbientCapabilities=CAP_DAC_READ_SEARCH
  34. The ``CAP_DAC_READ_SEARCH`` capability gives borg read-only access to all files and directories on the system.
  35. This service can then be started manually using ``systemctl start``, a systemd timer or other methods.
  36. Restore considerations
  37. ======================
  38. When restoring files, the root user should be used. When using the non-root user, borg extract will
  39. change all files to be owned by the non-root user. Using borg mount will not allow the non-root user
  40. to access files that it would not have access to on the system itself.
  41. Other than that, the same restore process, that would be used when running the backup as root, can be used.
  42. .. warning::
  43. When using a local repo and running borg commands as root, make sure to only use commands that do not
  44. modify the repo itself, like extract or mount. Modifying the repo using the root user will break
  45. the repo for the non-root user, since some files inside the repo will now be owned by root.