pull-backup.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. .. include:: ../global.rst.inc
  2. .. highlight:: none
  3. .. _pull_backup:
  4. =======================
  5. Backing up in pull mode
  6. =======================
  7. Typically the borg client connects to a backup server using SSH as a transport
  8. when initiating a backup. This is referred to as push mode.
  9. If you however require the backup server to initiate the connection or prefer
  10. it to initiate the backup run, one of the following workarounds is required to
  11. allow such a pull mode setup.
  12. A common use case for pull mode is to backup a remote server to a local personal
  13. computer.
  14. SSHFS
  15. =====
  16. Assuming you have a pull backup system set up with borg, where a backup server
  17. pulls the data from the target via SSHFS. In this mode, the backup client's file
  18. system is mounted remotely on the backup server. Pull mode is even possible if
  19. the SSH connection must be established by the client via a remote tunnel. Other
  20. network file systems like NFS or SMB could be used as well, but SSHFS is very
  21. simple to set up and probably the most secure one.
  22. There are some restrictions caused by SSHFS. For example, unless you define UID
  23. and GID mappings when mounting via ``sshfs``, owners and groups of the mounted
  24. file system will probably change, and you may not have access to those files if
  25. BorgBackup is not run with root privileges.
  26. SSHFS is a FUSE file system and uses the SFTP protocol, so there may be also
  27. other unsupported features that the actual implementations of ssfs, libfuse and
  28. sftp on the backup server do not support, like file name encodings, ACLs, xattrs
  29. or flags. So there is no guarantee that you are able to restore a system
  30. completely in every aspect from such a backup.
  31. .. warning::
  32. To mount the client's root file system you will need root access to the
  33. client. This contradicts to the usual threat model of BorgBackup, where
  34. clients don't need to trust the backup server (data is encrypted). In pull
  35. mode the server (when logged in as root) could cause unlimited damage to the
  36. client. Therefore, pull mode should be used only from servers you do fully
  37. trust!
  38. Creating a backup
  39. -----------------
  40. Generally, in a pull backup situation there is no direct way for borg to know
  41. the client's original UID:GID name mapping of files, because Borg would use
  42. ``/etc/passwd`` and ``/etc/group`` of the backup server to map the names. To
  43. derive the right names, Borg needs to have access to the client's passwd and
  44. group files and use them in the backup process.
  45. The solution to this problem is chrooting into an sshfs mounted directory. In
  46. this example the whole client root file system is mounted. We use the
  47. stand-alone BorgBackup executable and copy it into the mounted file system to
  48. make Borg available after entering chroot; this can be skipped if Borg is
  49. already installed on the client.
  50. ::
  51. # Mount client root file system.
  52. mkdir /tmp/sshfs
  53. sshfs root@host:/ /tmp/sshfs
  54. # Mount BorgBackup repository inside it.
  55. mkdir /tmp/sshfs/borgrepo
  56. mount --bind /path/to/repo /tmp/sshfs/borgrepo
  57. # Make borg executable available.
  58. cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg
  59. # Mount important system directories and enter chroot.
  60. cd /tmp/sshfs
  61. for i in dev proc sys; do mount --bind /$i $i; done
  62. chroot /tmp/sshfs
  63. Now we are on the backup system but inside a chroot with the client's root file
  64. system. We have a copy of Borg binary in ``/usr/local/bin`` and the repository
  65. in ``/borgrepo``. Borg will back up the client's user/group names, and we can
  66. create the backup, retaining the original paths, excluding the repository:
  67. ::
  68. borg create --exclude /borgrepo --files-cache ctime,size /borgrepo::archive /
  69. For the sake of simplicity only ``/borgrepo`` is excluded here. You may want to
  70. set up an exclude file with additional files and folders to be excluded. Also
  71. note that we have to modify Borg's file change detection behaviour – SSHFS
  72. cannot guarantee stable inode numbers, so we have to supply the
  73. ``--files-cache`` option.
  74. Finally, we need to exit chroot, unmount all the stuff and clean up:
  75. ::
  76. exit # exit chroot
  77. rm /tmp/sshfs/usr/local/bin/borg
  78. cd /tmp/sshfs
  79. for i in dev proc sys borgrepo; do umount ./$i; done
  80. rmdir borgrepo
  81. cd ~
  82. umount /tmp/sshfs
  83. rmdir /tmp/sshfs
  84. Thanks to secuser on IRC for this how-to!
  85. Restore methods
  86. ---------------
  87. The counterpart of a pull backup is a push restore. Depending on the type of
  88. restore – full restore or partial restore – there are different methods to make
  89. sure the correct IDs are restored.
  90. Partial restore
  91. ~~~~~~~~~~~~~~~
  92. In case of a partial restore, using the archived UIDs/GIDs might lead to wrong
  93. results if the name-to-ID mapping on the target system has changed compared to
  94. backup time (might be the case e.g. for a fresh OS install).
  95. The workaround again is chrooting into an sshfs mounted directory, so Borg is
  96. able to map the user/group names of the backup files to the actual IDs on the
  97. client. This example is similar to the backup above – only the Borg command is
  98. different:
  99. ::
  100. # Mount client root file system.
  101. mkdir /tmp/sshfs
  102. sshfs root@host:/ /tmp/sshfs
  103. # Mount BorgBackup repository inside it.
  104. mkdir /tmp/sshfs/borgrepo
  105. mount --bind /path/to/repo /tmp/sshfs/borgrepo
  106. # Make borg executable available.
  107. cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg
  108. # Mount important system directories and enter chroot.
  109. cd /tmp/sshfs
  110. for i in dev proc sys; do mount --bind /$i $i; done
  111. chroot /tmp/sshfs
  112. Now we can run
  113. ::
  114. borg extract /borgrepo::archive PATH
  115. to partially restore whatever we like. Finally, do the clean-up:
  116. ::
  117. exit # exit chroot
  118. rm /tmp/sshfs/usr/local/bin/borg
  119. cd /tmp/sshfs
  120. for i in dev proc sys borgrepo; do umount ./$i; done
  121. rmdir borgrepo
  122. cd ~
  123. umount /tmp/sshfs
  124. rmdir /tmp/sshfs
  125. Full restore
  126. ~~~~~~~~~~~~
  127. When doing a full restore, we restore all files (including the ones containing
  128. the ID-to-name mapping, ``/etc/passwd`` and ``/etc/group``). Everything will be
  129. consistent automatically if we restore the numeric IDs stored in the archive. So
  130. there is no need for a chroot environment; we just mount the client file system
  131. and extract a backup, utilizing the ``--numeric-owner`` option:
  132. ::
  133. sshfs root@host:/ /mnt/sshfs
  134. cd /mnt/sshfs
  135. borg extract --numeric-owner /path/to/repo::archive
  136. cd ~
  137. umount /mnt/sshfs
  138. Simple (lossy) full restore
  139. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. Using ``borg export-tar`` it is possible to stream a backup to the client and
  141. directly extract it without the need of mounting with SSHFS:
  142. ::
  143. borg export-tar /path/to/repo::archive - | ssh root@host 'tar -C / -x'
  144. Note that in this scenario the tar format is the limiting factor – it cannot
  145. restore all the advanced features that BorgBackup supports. See
  146. :ref:`borg_export-tar` for limitations.
  147. socat
  148. =====
  149. In this setup a SSH connection from the backup server to the client is
  150. established that uses SSH reverse port forwarding to transparently
  151. tunnel data between UNIX domain sockets on the client and server and the socat
  152. tool to connect these with the borg client and server processes, respectively.
  153. The program socat has to be available on the backup server and on the client
  154. to be backed up.
  155. When **pushing** a backup the borg client (holding the data to be backed up)
  156. connects to the backup server via ssh, starts ``borg serve`` on the backup
  157. server and communicates via standard input and output (transported via SSH)
  158. with the process on the backup server.
  159. With the help of socat this process can be reversed. The backup server will
  160. create a connection to the client (holding the data to be backed up) and will
  161. **pull** the data.
  162. In the following example *borg-server* connects to *borg-client* to pull a backup.
  163. To provide a secure setup sockets should be stored in ``/run/borg``, only
  164. accessible to the users that run the backup process. So on both systems,
  165. *borg-server* and *borg-client* the folder ``/run/borg`` has to be created::
  166. sudo mkdir -m 0700 /run/borg
  167. On *borg-server* the socket file is opened by the user running the ``borg
  168. serve`` process writing to the repository
  169. so the user has to have read and write permissions on ``/run/borg``::
  170. borg-server:~$ sudo chown borgs /run/borg
  171. On *borg-client* the socket file is created by ssh, so the user used to connect
  172. to *borg-client* has to have read and write permissions on ``/run/borg``::
  173. borg-client:~$ sudo chown borgc /run/borg
  174. On *borg-server*, we have to start the command ``borg serve`` and make its
  175. standard input and output available to a unix socket::
  176. borg-server:~$ socat UNIX-LISTEN:/run/borg/reponame.sock,fork EXEC:"borg serve --append-only --restrict-to-path /path/to/repo"
  177. Socat will wait until a connection is opened. Then socat will execute the
  178. command given, redirecting Standard Input and Output to the unix socket. The
  179. optional arguments for ``borg serve`` are not necessary but a sane default.
  180. .. note::
  181. When used in production you may also use systemd socket-based activation
  182. instead of socat on the server side. You would wrap the ``borg serve`` command
  183. in a `service unit`_ and configure a matching `socket unit`_
  184. to start the service whenever a client connects to the socket.
  185. .. _service unit: https://www.freedesktop.org/software/systemd/man/systemd.service.html
  186. .. _socket unit: https://www.freedesktop.org/software/systemd/man/systemd.socket.html
  187. Now we need a way to access the unix socket on *borg-client* (holding the
  188. data to be backed up), as we created the unix socket on *borg-server*
  189. Opening a SSH connection from the *borg-server* to the *borg-client* with reverse port
  190. forwarding can do this for us::
  191. borg-server:~$ ssh -R /run/borg/reponame.sock:/run/borg/reponame.sock borgc@borg-client
  192. .. note::
  193. As the default value of OpenSSH for ``StreamLocalBindUnlink`` is ``no``, the
  194. socket file created by sshd is not removed. Trying to connect a second time,
  195. will print a short warning, and the forwarding does **not** take place::
  196. Warning: remote port forwarding failed for listen path /run/borg/reponame.sock
  197. When you are done, you have to manually remove the socket file, otherwise
  198. you may see an error like this when trying to execute borg commands::
  199. Remote: YYYY/MM/DD HH:MM:SS socat[XXX] E connect(5, AF=1 "/run/borg/reponame.sock", 13): Connection refused
  200. Connection closed by remote host. Is borg working on the server?
  201. When a process opens the socket on *borg-client*, SSH will forward all
  202. data to the socket on *borg-server*.
  203. The next step is to tell borg on *borg-client* to use the unix socket to communicate with the
  204. ``borg serve`` command on *borg-server* via the socat socket instead of SSH::
  205. borg-client:~$ export BORG_RSH="sh -c 'exec socat STDIO UNIX-CONNECT:/run/borg/reponame.sock'"
  206. The default value for ``BORG_RSH`` is ``ssh``. By default Borg uses SSH to create
  207. the connection to the backup server. Therefore Borg parses the repo URL
  208. and adds the server name (and other arguments) to the SSH command. Those
  209. arguments can not be handled by socat. We wrap the command with ``sh`` to
  210. ignore all arguments intended for the SSH command.
  211. All Borg commands can now be executed on *borg-client*. For example to create a
  212. backup execute the ``borg create`` command::
  213. borg-client:~$ borg create ssh://borg-server/path/to/repo::archive /path_to_backup
  214. When automating backup creation, the
  215. interactive ssh session may seem inappropriate. An alternative way of creating
  216. a backup may be the following command::
  217. borg-server:~$ ssh \
  218. -R /run/borg/reponame.sock:/run/borg/reponame.sock \
  219. borgc@borg-client \
  220. borg create \
  221. --rsh "sh -c 'exec socat STDIO UNIX-CONNECT:/run/borg/reponame.sock'" \
  222. ssh://borg-server/path/to/repo::archive /path_to_backup \
  223. ';' rm /run/borg/reponame.sock
  224. This command also automatically removes the socket file after the ``borg
  225. create`` command is done.
  226. ssh-agent
  227. =========
  228. In this scenario *borg-server* initiates an SSH connection to *borg-client* and forwards the authentication
  229. agent connection.
  230. After that, it works similar to the push mode:
  231. *borg-client* initiates another SSH connection back to *borg-server* using the forwarded authentication agent
  232. connection to authenticate itself, starts ``borg serve`` and communicates with it.
  233. Using this method requires ssh access of user *borgs* to *borgc@borg-client*, where:
  234. * *borgs* is the user on the server side with read/write access to local borg repository.
  235. * *borgc* is the user on the client side with read access to files meant to be backed up.
  236. Applying this method for automated backup operations
  237. ----------------------------------------------------
  238. Assume that the borg-client host is untrusted.
  239. Therefore we do some effort to prevent a hostile user on the borg-client side to do something harmful.
  240. In case of a fully trusted borg-client the method could be simplified.
  241. Preparing the server side
  242. ~~~~~~~~~~~~~~~~~~~~~~~~~
  243. Do this once for each client on *borg-server* to allow *borgs* to connect itself on *borg-server* using a
  244. dedicated ssh key:
  245. ::
  246. borgs@borg-server$ install -m 700 -d ~/.ssh/
  247. borgs@borg-server$ ssh-keygen -N '' -t rsa -f ~/.ssh/borg-client_key
  248. borgs@borg-server$ { echo -n 'command="borg serve --append-only --restrict-to-repo ~/repo",restrict '; cat ~/.ssh/borg-client_key.pub; } >> ~/.ssh/authorized_keys
  249. borgs@borg-server$ chmod 600 ~/.ssh/authorized_keys
  250. ``install -m 700 -d ~/.ssh/``
  251. Create directory ~/.ssh with correct permissions if it does not exist yet.
  252. ``ssh-keygen -N '' -t rsa -f ~/.ssh/borg-client_key``
  253. Create an ssh key dedicated to communication with borg-client.
  254. .. note::
  255. Another more complex approach is using a unique ssh key for each pull operation.
  256. This is more secure as it guarantees that the key will not be used for other purposes.
  257. ``{ echo -n 'command="borg serve --append-only --restrict-to-repo ~/repo",restrict '; cat ~/.ssh/borg-client_key.pub; } >> ~/.ssh/authorized_keys``
  258. Add borg-client's ssh public key to ~/.ssh/authorized_keys with forced command and restricted mode.
  259. The borg client is restricted to use one repo at the specified path and to append-only operation.
  260. Commands like *delete*, *prune* and *compact* have to be executed another way, for example directly on *borg-server*
  261. side or from a privileged, less restricted client (using another authorized_keys entry).
  262. ``chmod 600 ~/.ssh/authorized_keys``
  263. Fix permissions of ~/.ssh/authorized_keys.
  264. Pull operation
  265. ~~~~~~~~~~~~~~
  266. Initiating borg command execution from *borg-server* (e.g. init)::
  267. borgs@borg-server$ (
  268. eval $(ssh-agent) > /dev/null
  269. ssh-add -q ~/.ssh/borg-client_key
  270. echo 'your secure borg key passphrase' | \
  271. ssh -A -o StrictHostKeyChecking=no borgc@borg-client "BORG_PASSPHRASE=\$(cat) borg --rsh 'ssh -o StrictHostKeyChecking=no' init --encryption repokey ssh://borgs@borg-server/~/repo"
  272. kill "${SSH_AGENT_PID}"
  273. )
  274. Parentheses around commands are needed to avoid interference with a possibly already running ssh-agent.
  275. Parentheses are not needed when using a dedicated bash process.
  276. ``eval $(ssh-agent) > /dev/null``
  277. Run the SSH agent in the background and export related environment variables to the current bash session.
  278. ``ssh-add -q ~/.ssh/borg-client_key``
  279. Load the SSH private key dedicated to communication with the borg-client into the SSH agent.
  280. Look at ``man 1 ssh-add`` for a more detailed explanation.
  281. .. note::
  282. Care needs to be taken when loading keys into the SSH agent. Users on the *borg-client* having read/write permissions
  283. to the agent's UNIX-domain socket (at least borgc and root in our case) can access the agent on *borg-server* through
  284. the forwarded connection and can authenticate using any of the identities loaded into the agent
  285. (look at ``man 1 ssh`` for more detailed explanation). Therefore there are some security considerations:
  286. * Private keys loaded into the agent must not be used to enable access anywhere else.
  287. * The keys meant to be loaded into the agent must be specified explicitly, not from default locations.
  288. * The *borg-client*'s entry in *borgs@borg-server:~/.ssh/authorized_keys* must be as restrictive as possible.
  289. ``echo 'your secure borg key passphrase' | ssh -A -o StrictHostKeyChecking=no borgc@borg-client "BORG_PASSPHRASE=\$(cat) borg --rsh 'ssh -o StrictHostKeyChecking=no' init --encryption repokey ssh://borgs@borg-server/~/repo"``
  290. Run the *borg init* command on *borg-client*.
  291. *ssh://borgs@borg-server/~/repo* refers to the repository *repo* within borgs's home directory on *borg-server*.
  292. *StrictHostKeyChecking=no* is used to automatically add host keys to *~/.ssh/known_hosts* without user intervention.
  293. ``kill "${SSH_AGENT_PID}"``
  294. Kill ssh-agent with loaded keys when it is not needed anymore.