quickstart.rst 11 KB

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