quickstart.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. .. include:: global.rst.inc
  2. .. highlight:: bash
  3. .. _quickstart:
  4. Quick Start
  5. ===========
  6. This chapter will get you started with |project_name| and covers
  7. various use cases.
  8. A step by step example
  9. ----------------------
  10. .. include:: quickstart_example.rst.inc
  11. Important note about free space
  12. -------------------------------
  13. Before you start creating backups, please make sure that there is *always*
  14. a good amount of free space on the filesystem that has your backup repository
  15. (and also on ~/.cache). A few GB should suffice for most hard-drive sized
  16. repositories. See also :ref:`cache-memory-usage`.
  17. Borg doesn't use space reserved for root on repository disks (even when run as root),
  18. on file systems which do not support this mechanism (e.g. XFS) we recommend to
  19. reserve some space in Borg itself just to be safe by adjusting the
  20. ``additional_free_space`` setting in the ``[repository]`` section of a repositories
  21. ``config`` file. A good starting point is ``2G``.
  22. If |project_name| runs out of disk space, it tries to free as much space as it
  23. can while aborting the current operation safely, which allows to free more space
  24. by deleting/pruning archives. This mechanism is not bullet-proof in some
  25. circumstances [1]_.
  26. If you *really* run out of disk space, it can be hard or impossible to free space,
  27. because |project_name| needs free space to operate - even to delete backup
  28. archives.
  29. You can use some monitoring process or just include the free space information
  30. in your backup log files (you check them regularly anyway, right?).
  31. Also helpful:
  32. - create a big file as a "space reserve", that you can delete to free space
  33. - if you use LVM: use a LV + a filesystem that you can resize later and have
  34. some unallocated PEs you can add to the LV.
  35. - consider using quotas
  36. - use `prune` regularly
  37. .. [1] This failsafe can fail in these circumstances:
  38. - The underlying file system doesn't support statvfs(2), or returns incorrect
  39. data, or the repository doesn't reside on a single file system
  40. - Other tasks fill the disk simultaneously
  41. - Hard quotas (which may not be reflected in statvfs(2))
  42. Automating backups
  43. ------------------
  44. The following example script backs up ``/home`` and ``/var/www`` to a remote
  45. server. The script also uses the :ref:`borg_prune` subcommand to maintain a
  46. certain number of old archives:
  47. ::
  48. #!/bin/sh
  49. # setting this, so the repo does not need to be given on the commandline:
  50. export BORG_REPO=username@remoteserver.com:backup
  51. # setting this, so you won't be asked for your passphrase - make sure the
  52. # script has appropriate owner/group and mode, e.g. root.root 600:
  53. export BORG_PASSPHRASE=mysecret
  54. # Backup most important stuff:
  55. borg create --stats -C lz4 ::'{hostname}-{now:%Y-%m-%d}' \
  56. /etc \
  57. /home \
  58. /var \
  59. --exclude '/home/*/.cache' \
  60. --exclude '*.pyc'
  61. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  62. # archives of THIS machine. The '{hostname}-' prefix is very important to
  63. # limit prune's operation to this machine's archives and not apply to
  64. # other machine's archives also.
  65. borg prune --list $REPOSITORY --prefix '{hostname}-' \
  66. --keep-daily=7 --keep-weekly=4 --keep-monthly=6
  67. Pitfalls with shell variables and environment variables
  68. -------------------------------------------------------
  69. This applies to all environment variables you want borg to see, not just
  70. ``BORG_PASSPHRASE``. The short explanation is: always ``export`` your variable,
  71. and use single quotes if you're unsure of the details of your shell's expansion
  72. behavior. E.g.::
  73. export BORG_PASSPHRASE='complicated & long'
  74. This is because ``export`` exposes variables to subprocesses, which borg may be
  75. one of. More on ``export`` can be found in the "ENVIRONMENT" section of the
  76. bash(1) man page.
  77. Beware of how ``sudo`` interacts with environment variables. For example, you
  78. may be surprised that the following ``export`` has no effect on your command::
  79. export BORG_PASSPHRASE='complicated & long'
  80. sudo ./yourborgwrapper.sh # still prompts for password
  81. For more information, see sudo(8) man page. Hint: see ``env_keep`` in
  82. sudoers(5), or try ``sudo BORG_PASSPHRASE='yourphrase' borg`` syntax.
  83. .. Tip::
  84. To debug what your borg process is actually seeing, find its PID
  85. (``ps aux|grep borg``) and then look into ``/proc/<PID>/environ``.
  86. .. backup_compression:
  87. Backup compression
  88. ------------------
  89. Default is no compression, but we support different methods with high speed
  90. or high compression:
  91. If you have a fast repo storage and you want some compression: ::
  92. $ borg create --compression lz4 /path/to/repo::arch ~
  93. If you have a less fast repo storage and you want a bit more compression (N=0..9,
  94. 0 means no compression, 9 means high compression): ::
  95. $ borg create --compression zlib,N /path/to/repo::arch ~
  96. If you have a very slow repo storage and you want high compression (N=0..9, 0 means
  97. low compression, 9 means high compression): ::
  98. $ borg create --compression lzma,N /path/to/repo::arch ~
  99. You'll need to experiment a bit to find the best compression for your use case.
  100. Keep an eye on CPU load and throughput.
  101. .. _encrypted_repos:
  102. Repository encryption
  103. ---------------------
  104. Repository encryption can be enabled or disabled at repository creation time
  105. (the default is enabled, with `repokey` method)::
  106. $ borg init --encryption=none|repokey|keyfile PATH
  107. When repository encryption is enabled all data is encrypted using 256-bit AES_
  108. encryption and the integrity and authenticity is verified using `HMAC-SHA256`_.
  109. All data is encrypted on the client before being written to the repository. This
  110. means that an attacker who manages to compromise the host containing an
  111. encrypted archive will not be able to access any of the data, even while the backup
  112. is being made.
  113. |project_name| supports different methods to store the AES and HMAC keys.
  114. ``repokey`` mode
  115. The key is stored inside the repository (in its "config" file).
  116. Use this mode if you trust in your good passphrase giving you enough
  117. protection. The repository server never sees the plaintext key.
  118. ``keyfile`` mode
  119. The key is stored on your local disk (in ``~/.config/borg/keys/``).
  120. Use this mode if you want "passphrase and having-the-key" security.
  121. In both modes, the key is stored in encrypted form and can be only decrypted
  122. by providing the correct passphrase.
  123. For automated backups the passphrase can be specified using the
  124. `BORG_PASSPHRASE` environment variable.
  125. .. note:: Be careful about how you set that environment, see
  126. :ref:`this note about password environments <password_env>`
  127. for more information.
  128. .. warning:: The repository data is totally inaccessible without the key
  129. and the key passphrase.
  130. Make a backup copy of the key file (``keyfile`` mode) or repo config
  131. file (``repokey`` mode) and keep it at a safe place, so you still have
  132. the key in case it gets corrupted or lost. Also keep your passphrase
  133. at a safe place.
  134. You can make backups using :ref:`borg_key_export` subcommand.
  135. If you want to print a backup of your key to paper use the ``--paper``
  136. option of this command and print the result, or this print `template`_
  137. if you need a version with QR-Code.
  138. A backup inside of the backup that is encrypted with that key/passphrase
  139. won't help you with that, of course.
  140. .. _template: paperkey.html
  141. .. _remote_repos:
  142. Remote repositories
  143. -------------------
  144. |project_name| can initialize and access repositories on remote hosts if the
  145. host is accessible using SSH. This is fastest and easiest when |project_name|
  146. is installed on the remote host, in which case the following syntax is used::
  147. $ borg init user@hostname:/path/to/repo
  148. Note: please see the usage chapter for a full documentation of repo URLs.
  149. Remote operations over SSH can be automated with SSH keys. You can restrict the
  150. use of the SSH keypair by prepending a forced command to the SSH public key in
  151. the remote server's `authorized_keys` file. This example will start |project_name|
  152. in server mode and limit it to a specific filesystem path::
  153. command="borg serve --restrict-to-path /path/to/repo",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc ssh-rsa AAAAB3[...]
  154. If it is not possible to install |project_name| on the remote host,
  155. it is still possible to use the remote host to store a repository by
  156. mounting the remote filesystem, for example, using sshfs::
  157. $ sshfs user@hostname:/path/to /path/to
  158. $ borg init /path/to/repo
  159. $ fusermount -u /path/to
  160. You can also use other remote filesystems in a similar way. Just be careful,
  161. not all filesystems out there are really stable and working good enough to
  162. be acceptable for backup usage.