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. back up 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. However, in theory this also allows Borg to modify or
  11. delete files on your system (for example, in case of a bug).
  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 grant parts of the root user’s privileges to
  20. a non-root user. This works on a per-thread level and does not grant permissions
  21. to the non-root user as a whole.
  22. For this, we need to run the 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. Use the root user when restoring files. If you use the non-root user, ``borg extract`` will
  39. change ownership of all restored files to the non-root user. Using ``borg mount`` will not allow the
  40. non-root user to access files it would not be able to access on the system itself.
  41. Other than that, you can use the same restore process you would use when running the backup as root.
  42. .. warning::
  43. When using a local repository and running Borg commands as root, make sure to use only commands that do not
  44. modify the repository itself, such as extract or mount. Modifying the repository as root will break it for the
  45. non-root user, since some files inside the repository will then be owned by root.