quickstart.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 is meant to be run daily by the ``root`` user on
  45. different local machines. It backs up a machine's important files (but not the
  46. complete operating system) to a repository ``~/backup/main`` on a remote server.
  47. Some files which aren't necessarily needed in this backup are excluded. See
  48. :ref:`borg_patterns` on how to add more exclude options.
  49. After the backup this script also uses the :ref:`borg_prune` subcommand to keep
  50. only a certain number of old archives and deletes the others in order to preserve
  51. disk space.
  52. Before running, make sure that the repository is initialized as documented in
  53. :ref:`remote_repos` and that the script has the correct permissions to be executable
  54. by the root user, but not executable or readable by anyone else, i.e. root:root 0700.
  55. You can use this script as a starting point and modify it where it's necessary to fit
  56. your setup.
  57. Do not forget to test your created backups to make sure everything you need is being
  58. backed up and that the ``prune`` command is keeping and deleting the correct backups.
  59. ::
  60. #!/bin/sh
  61. # Setting this, so the repo does not need to be given on the commandline:
  62. export BORG_REPO=ssh://username@example.com:2022/~/backup/main
  63. # Setting this, so you won't be asked for your repository passphrase:
  64. export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
  65. # some helpers and error handling:
  66. function info () { echo -e "\n"`date` $@"\n" >&2; }
  67. trap "echo `date` Backup interrupted >&2; exit 2" SIGINT SIGTERM
  68. info "Starting backup"
  69. # Backup the most important directories into an archive named after
  70. # the machine this script is currently running on:
  71. borg create \
  72. --verbose \
  73. --filter AME \
  74. --list \
  75. --stats \
  76. --show-rc \
  77. --compression lz4 \
  78. --exclude-caches \
  79. --exclude '/home/*/.cache/*' \
  80. --exclude '/var/cache/*' \
  81. --exclude '/var/tmp/*' \
  82. \
  83. ::'{hostname}-{now}' \
  84. /etc \
  85. /home \
  86. /root \
  87. /var \
  88. backup_exit=$?
  89. info "Pruning repository"
  90. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  91. # archives of THIS machine. The '{hostname}-' prefix is very important to
  92. # limit prune's operation to this machine's archives and not apply to
  93. # other machines' archives also:
  94. borg prune \
  95. --list \
  96. --prefix '{hostname}-' \
  97. --show-rc \
  98. --keep-daily 7 \
  99. --keep-weekly 4 \
  100. --keep-monthly 6 \
  101. prune_exit=$?
  102. global_exit=$(( ${backup_exit} > ${prune_exit} ? ${backup_exit} : ${prune_exit} ))
  103. if [ ${global_exit} -eq 1 ];
  104. then
  105. info "Backup and/or Prune finished with a warning"
  106. fi
  107. if [ ${global_exit} -gt 1 ];
  108. then
  109. info "Backup and/or Prune finished with an error"
  110. fi
  111. exit ${global_exit}
  112. Pitfalls with shell variables and environment variables
  113. -------------------------------------------------------
  114. This applies to all environment variables you want borg to see, not just
  115. ``BORG_PASSPHRASE``. The short explanation is: always ``export`` your variable,
  116. and use single quotes if you're unsure of the details of your shell's expansion
  117. behavior. E.g.::
  118. export BORG_PASSPHRASE='complicated & long'
  119. This is because ``export`` exposes variables to subprocesses, which borg may be
  120. one of. More on ``export`` can be found in the "ENVIRONMENT" section of the
  121. bash(1) man page.
  122. Beware of how ``sudo`` interacts with environment variables. For example, you
  123. may be surprised that the following ``export`` has no effect on your command::
  124. export BORG_PASSPHRASE='complicated & long'
  125. sudo ./yourborgwrapper.sh # still prompts for password
  126. For more information, see sudo(8) man page. Hint: see ``env_keep`` in
  127. sudoers(5), or try ``sudo BORG_PASSPHRASE='yourphrase' borg`` syntax.
  128. .. Tip::
  129. To debug what your borg process is actually seeing, find its PID
  130. (``ps aux|grep borg``) and then look into ``/proc/<PID>/environ``.
  131. .. backup_compression:
  132. Backup compression
  133. ------------------
  134. The default is lz4 (very fast, but low compression ratio), but other methods are
  135. supported for different situations.
  136. If you have a fast repo storage and you want minimum CPU usage, no compression::
  137. $ borg create --compression none /path/to/repo::arch ~
  138. If you have a less fast repo storage and you want a bit more compression (N=0..9,
  139. 0 means no compression, 9 means high compression): ::
  140. $ borg create --compression zlib,N /path/to/repo::arch ~
  141. If you have a very slow repo storage and you want high compression (N=0..9, 0 means
  142. low compression, 9 means high compression): ::
  143. $ borg create --compression lzma,N /path/to/repo::arch ~
  144. You'll need to experiment a bit to find the best compression for your use case.
  145. Keep an eye on CPU load and throughput.
  146. .. _encrypted_repos:
  147. Repository encryption
  148. ---------------------
  149. Repository encryption can be enabled or disabled at repository creation time
  150. (the default is enabled, with `repokey` method)::
  151. $ borg init --encryption=none|repokey|keyfile PATH
  152. When repository encryption is enabled all data is encrypted using 256-bit AES_
  153. encryption and the integrity and authenticity is verified using `HMAC-SHA256`_.
  154. All data is encrypted on the client before being written to the repository. This
  155. means that an attacker who manages to compromise the host containing an
  156. encrypted archive will not be able to access any of the data, even while the backup
  157. is being made.
  158. |project_name| supports different methods to store the AES and HMAC keys.
  159. ``repokey`` mode
  160. The key is stored inside the repository (in its "config" file).
  161. Use this mode if you trust in your good passphrase giving you enough
  162. protection. The repository server never sees the plaintext key.
  163. ``keyfile`` mode
  164. The key is stored on your local disk (in ``~/.config/borg/keys/``).
  165. Use this mode if you want "passphrase and having-the-key" security.
  166. In both modes, the key is stored in encrypted form and can be only decrypted
  167. by providing the correct passphrase.
  168. For automated backups the passphrase can be specified using the
  169. `BORG_PASSPHRASE` environment variable.
  170. .. note:: Be careful about how you set that environment, see
  171. :ref:`this note about password environments <password_env>`
  172. for more information.
  173. .. warning:: The repository data is totally inaccessible without the key
  174. and the key passphrase.
  175. Make a backup copy of the key file (``keyfile`` mode) or repo config
  176. file (``repokey`` mode) and keep it at a safe place, so you still have
  177. the key in case it gets corrupted or lost. Also keep your passphrase
  178. at a safe place.
  179. You can make backups using :ref:`borg_key_export` subcommand.
  180. If you want to print a backup of your key to paper use the ``--paper``
  181. option of this command and print the result, or this print `template`_
  182. if you need a version with QR-Code.
  183. A backup inside of the backup that is encrypted with that key/passphrase
  184. won't help you with that, of course.
  185. .. _template: paperkey.html
  186. .. _remote_repos:
  187. Remote repositories
  188. -------------------
  189. |project_name| can initialize and access repositories on remote hosts if the
  190. host is accessible using SSH. This is fastest and easiest when |project_name|
  191. is installed on the remote host, in which case the following syntax is used::
  192. $ borg init user@hostname:/path/to/repo
  193. Note: please see the usage chapter for a full documentation of repo URLs.
  194. Remote operations over SSH can be automated with SSH keys. You can restrict the
  195. use of the SSH keypair by prepending a forced command to the SSH public key in
  196. the remote server's `authorized_keys` file. This example will start |project_name|
  197. in server mode and limit it to a specific filesystem path::
  198. 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[...]
  199. If it is not possible to install |project_name| on the remote host,
  200. it is still possible to use the remote host to store a repository by
  201. mounting the remote filesystem, for example, using sshfs::
  202. $ sshfs user@hostname:/path/to /path/to
  203. $ borg init /path/to/repo
  204. $ fusermount -u /path/to
  205. You can also use other remote filesystems in a similar way. Just be careful,
  206. not all filesystems out there are really stable and working good enough to
  207. be acceptable for backup usage.