| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 | .. include:: ../global.rst.inc.. highlight:: noneBacking up in pull mode=======================Assuming you have a pull backup system set up with borg, where a backup serverpulls the data from the target via SSHFS. In this mode, the backup client's filesystem is mounted remotely on the backup server. Pull mode is even possible ifthe SSH connection must be established by the client via a remote tunnel. Othernetwork file systems like NFS or SMB could be used as well, but SSHFS is verysimple to set up and probably the most secure one.There are some restrictions caused by SSHFS. For example, unless you define UIDand GID mappings when mounting via ``sshfs``, owners and groups of the mountedfile system will probably change, and you may not have access to those files ifBorgBackup is not run with root privileges.SSHFS is a FUSE file system and uses the SFTP protocol, so there may be alsoother unsupported features that the actual implementations of ssfs, libfuse andsftp on the backup server do not support, like file name encodings, ACLs, xattrsor bsdflags. So there is no guarantee that you are able to restore a systemcompletely in every aspect from such a backup... warning::    To mount the client's root file system you will need root access to the    client. This contradicts to the usual threat model of BorgBackup, where    clients don't need to trust the backup server (data is encrypted). In pull    mode the server (when logged in as root) could cause unlimited damage to the    client. Therefore, pull mode should be used only from servers you do fully    trust!Creating a backup-----------------Generally, in a pull backup situation there is no direct way for borg to knowthe client's original UID:GID name mapping of files, because Borg would use``/etc/passwd`` and ``/etc/group`` of the backup server to map the names. Toderive the right names, Borg needs to have access to the client's passwd andgroup files and use them in the backup process.The solution to this problem is chrooting into an sshfs mounted directory. Inthis example the whole client root file system is mounted. We use thestand-alone BorgBackup executable and copy it into the mounted file system tomake Borg available after entering chroot; this can be skipped if Borg isalready installed on the client.::    # Mount client root file system.    mkdir /tmp/sshfs    sshfs root@host:/ /tmp/sshfs    # Mount BorgBackup repository inside it.    mkdir /tmp/sshfs/borgrepo    mount --bind /path/to/repo /tmp/sshfs/borgrepo    # Make borg executable available.    cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg    # Mount important system directories and enter chroot.    cd /tmp/sshfs    for i in dev proc sys; do mount --bind /$i $i; done    chroot /tmp/sshfsNow we are on the backup system but inside a chroot with the client's root filesystem. We have a copy of Borg binary in ``/usr/local/bin`` and the repositoryin ``/borgrepo``. Borg will back up the client's user/group names, and we cancreate the backup, retaining the original paths, excluding the repository:::    borg create --exclude /borgrepo --files-cache ctime,size /borgrepo::archive /For the sake of simplicity only ``/borgrepo`` is excluded here. You may want toset up an exclude file with additional files and folders to be excluded. Alsonote that we have to modify Borg's file change detection behaviour – SSHFScannot guarantee stable inode numbers, so we have to supply the``--files-cache`` option.Finally, we need to exit chroot, unmount all the stuff and clean up:::    exit # exit chroot    rm /tmp/sshfs/usr/local/bin/borg    cd /tmp/sshfs    for i in dev proc sys borgrepo; do umount ./$i; done    rmdir borgrepo    cd ~    umount /tmp/sshfs    rmdir /tmp/sshfsThanks to secuser on IRC for this how-to!Restore methods---------------The counterpart of a pull backup is a push restore. Depending on the type ofrestore – full restore or partial restore – there are different methods to makesure the correct IDs are restored.Partial restore~~~~~~~~~~~~~~~In case of a partial restore, using the archived UIDs/GIDs might lead to wrongresults if the name-to-ID mapping on the target system has changed compared tobackup time (might be the case e.g. for a fresh OS install).The workaround again is chrooting into an sshfs mounted directory, so Borg isable to map the user/group names of the backup files to the actual IDs on theclient. This example is similar to the backup above – only the Borg command isdifferent:::    # Mount client root file system.    mkdir /tmp/sshfs    sshfs root@host:/ /tmp/sshfs    # Mount BorgBackup repository inside it.    mkdir /tmp/sshfs/borgrepo    mount --bind /path/to/repo /tmp/sshfs/borgrepo    # Make borg executable available.    cp /usr/local/bin/borg /tmp/sshfs/usr/local/bin/borg    # Mount important system directories and enter chroot.    cd /tmp/sshfs    for i in dev proc sys; do mount --bind /$i $i; done    chroot /tmp/sshfsNow we can run::    borg extract /borgrepo::archive PATHto partially restore whatever we like. Finally, do the clean-up:::    exit # exit chroot    rm /tmp/sshfs/usr/local/bin/borg    cd /tmp/sshfs    for i in dev proc sys borgrepo; do umount ./$i; done    rmdir borgrepo    cd ~    umount /tmp/sshfs    rmdir /tmp/sshfsFull restore~~~~~~~~~~~~When doing a full restore, we restore all files (including the ones containingthe ID-to-name mapping, ``/etc/passwd`` and ``/etc/group``). Everything will beconsistent automatically if we restore the numeric IDs stored in the archive. Sothere is no need for a chroot environment; we just mount the client file systemand extract a backup, utilizing the ``--numeric-owner`` option:::    sshfs root@host:/ /mnt/sshfs    cd /mnt/sshfs    borg extract --numeric-owner /path/to/repo::archive    cd ~    umount /mnt/sshfsSimple (lossy) full restore~~~~~~~~~~~~~~~~~~~~~~~~~~~Using ``borg export-tar`` it is possible to stream a backup to the client anddirectly extract it without the need of mounting with SSHFS:::    borg export-tar /path/to/repo::archive - | ssh root@host 'tar -C / -x'Note that in this scenario the tar format is the limiting factor – it cannotrestore all the advanced features that BorgBackup supports. See:ref:`borg_export-tar` for limitations.
 |