pull-backup.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. Backing up in pull mode
  4. =======================
  5. Assuming you have a pull backup system set up with borg, where a backup server
  6. pulls the data from the target via SSHFS. In this mode, the backup client's file
  7. system is mounted remotely on the backup server. Pull mode is even possible if
  8. the SSH connection must be established by the client via a remote tunnel. Other
  9. network file systems like NFS or SMB could be used as well, but SSHFS is very
  10. simple to set up and probably the most secure one.
  11. There are some restrictions caused by SSHFS. For example, unless you define UID
  12. and GID mappings when mounting via ``sshfs``, owners and groups of the mounted
  13. file system will probably change, and you may not have access to those files if
  14. BorgBackup is not run with root privileges.
  15. SSHFS is a FUSE file system and uses the SFTP protocol, so there may be also
  16. other unsupported features that the actual implementations of ssfs, libfuse and
  17. sftp on the backup server do not support, like file name encodings, ACLs, xattrs
  18. or bsdflags. So there is no guarantee that you are able to restore a system
  19. completely in every aspect from such a backup.
  20. .. warning::
  21. To mount the client's root file system you will need root access to the
  22. client. This contradicts to the usual threat model of BorgBackup, where
  23. clients don't need to trust the backup server (data is encrypted). In pull
  24. mode the server (when logged in as root) could cause unlimited damage to the
  25. client. Therefore, pull mode should be used only from servers you do fully
  26. trust!
  27. Creating a backup
  28. -----------------
  29. Generally, in a pull backup situation there is no direct way for borg to know
  30. the client's original UID:GID name mapping of files, because Borg would use
  31. ``/etc/passwd`` and ``/etc/group`` of the backup server to map the names. To
  32. derive the right names, Borg needs to have access to the client's passwd and
  33. group files and use them in the backup process.
  34. The solution to this problem is chrooting into an sshfs mounted directory. In
  35. this example the whole client root file system is mounted. We use the
  36. stand-alone BorgBackup executable and copy it into the mounted file system to
  37. make Borg available after entering chroot; this can be skipped if Borg is
  38. already installed on the client.
  39. ::
  40. # Mount client root file system.
  41. mkdir /tmp/sshfs
  42. sshfs root@host:/ /tmp/sshfs
  43. # Mount BorgBackup repository inside it.
  44. mkdir /tmp/sshfs/borgrepo
  45. mount --bind /path/to/repo /tmp/sshfs/borgrepo
  46. # Make borg executable available.
  47. cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg
  48. # Mount important system directories and enter chroot.
  49. cd /tmp/sshfs
  50. for i in dev proc sys; do mount --bind /$i $i; done
  51. chroot /tmp/sshfs
  52. Now we are on the backup system but inside a chroot with the client's root file
  53. system. We have a copy of Borg binary in ``/usr/local/bin`` and the repository
  54. in ``/borgrepo``. Borg will back up the client's user/group names, and we can
  55. create the backup, retaining the original paths, excluding the repository:
  56. ::
  57. borg create --exclude /borgrepo --files-cache ctime,size /borgrepo::archive /
  58. For the sake of simplicity only ``/borgrepo`` is excluded here. You may want to
  59. set up an exclude file with additional files and folders to be excluded. Also
  60. note that we have to modify Borg's file change detection behaviour – SSHFS
  61. cannot guarantee stable inode numbers, so we have to supply the
  62. ``--files-cache`` option.
  63. Finally, we need to exit chroot, unmount all the stuff and clean up:
  64. ::
  65. exit # exit chroot
  66. rm /tmp/sshfs/usr/local/bin/borg
  67. cd /tmp/sshfs
  68. for i in dev proc sys borgrepo; do umount ./$i; done
  69. rmdir borgrepo
  70. cd ~
  71. umount /tmp/sshfs
  72. rmdir /tmp/sshfs
  73. Thanks to secuser on IRC for this how-to!
  74. Restore methods
  75. ---------------
  76. The counterpart of a pull backup is a push restore. Depending on the type of
  77. restore – full restore or partial restore – there are different methods to make
  78. sure the correct IDs are restored.
  79. Partial restore
  80. ~~~~~~~~~~~~~~~
  81. In case of a partial restore, using the archived UIDs/GIDs might lead to wrong
  82. results if the name-to-ID mapping on the target system has changed compared to
  83. backup time (might be the case e.g. for a fresh OS install).
  84. The workaround again is chrooting into an sshfs mounted directory, so Borg is
  85. able to map the user/group names of the backup files to the actual IDs on the
  86. client. This example is similar to the backup above – only the Borg command is
  87. different:
  88. ::
  89. # Mount client root file system.
  90. mkdir /tmp/sshfs
  91. sshfs root@host:/ /tmp/sshfs
  92. # Mount BorgBackup repository inside it.
  93. mkdir /tmp/sshfs/borgrepo
  94. mount --bind /path/to/repo /tmp/sshfs/borgrepo
  95. # Make borg executable available.
  96. cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg
  97. # Mount important system directories and enter chroot.
  98. cd /tmp/sshfs
  99. for i in dev proc sys; do mount --bind /$i $i; done
  100. chroot /tmp/sshfs
  101. Now we can run
  102. ::
  103. borg extract /borgrepo::archive PATH
  104. to partially restore whatever we like. Finally, do the clean-up:
  105. ::
  106. exit # exit chroot
  107. rm /tmp/sshfs/usr/local/bin/borg
  108. cd /tmp/sshfs
  109. for i in dev proc sys borgrepo; do umount ./$i; done
  110. rmdir borgrepo
  111. cd ~
  112. umount /tmp/sshfs
  113. rmdir /tmp/sshfs
  114. Full restore
  115. ~~~~~~~~~~~~
  116. When doing a full restore, we restore all files (including the ones containing
  117. the ID-to-name mapping, ``/etc/passwd`` and ``/etc/group``). Everything will be
  118. consistent automatically if we restore the numeric IDs stored in the archive. So
  119. there is no need for a chroot environment; we just mount the client file system
  120. and extract a backup, utilizing the ``--numeric-owner`` option:
  121. ::
  122. sshfs root@host:/ /mnt/sshfs
  123. cd /mnt/sshfs
  124. borg extract --numeric-owner /path/to/repo::archive
  125. cd ~
  126. umount /mnt/sshfs
  127. Simple (lossy) full restore
  128. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  129. Using ``borg export-tar`` it is possible to stream a backup to the client and
  130. directly extract it without the need of mounting with SSHFS:
  131. ::
  132. borg export-tar /path/to/repo::archive - | ssh root@host 'tar -C / -x'
  133. Note that in this scenario the tar format is the limiting factor – it cannot
  134. restore all the advanced features that BorgBackup supports. See
  135. :ref:`borg_export-tar` for limitations.