quickstart.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. .. include:: global.rst.inc
  2. .. highlight:: bash
  3. .. _quickstart:
  4. Quick Start
  5. ===========
  6. This chapter will get you started with Borg and covers
  7. various use cases.
  8. A step by step example
  9. ----------------------
  10. .. include:: quickstart_example.rst.inc
  11. Archives and repositories
  12. -------------------------
  13. A *Borg archive* is the result of a single backup (``borg create``). An archive
  14. stores a snapshot of the data of the files "inside" it. One can later extract or
  15. mount an archive to restore from a backup.
  16. *Repositories* are filesystem directories acting as self-contained stores of archives.
  17. Repositories can be accessed locally via path or remotely via ssh. Under the hood,
  18. repositories contain data blocks and a manifest tracking which blocks are in each
  19. archive. If some data hasn't changed from one backup to another, Borg can simply
  20. reference an already uploaded data chunk (deduplication).
  21. Important note about free space
  22. -------------------------------
  23. Before you start creating backups, please make sure that there is *always*
  24. a good amount of free space on the filesystem that has your backup repository
  25. (and also on ~/.cache). A few GB should suffice for most hard-drive sized
  26. repositories. See also :ref:`cache-memory-usage`.
  27. Borg doesn't use space reserved for root on repository disks (even when run as root),
  28. on file systems which do not support this mechanism (e.g. XFS) we recommend to reserve
  29. some space in Borg itself just to be safe by adjusting the ``additional_free_space``
  30. setting (a good starting point is ``2G``)::
  31. borg config /path/to/repo additional_free_space 2G
  32. If Borg runs out of disk space, it tries to free as much space as it
  33. can while aborting the current operation safely, which allows the user to free more space
  34. by deleting/pruning archives. This mechanism is not bullet-proof in some
  35. circumstances [1]_.
  36. If you *really* run out of disk space, it can be hard or impossible to free space,
  37. because Borg needs free space to operate - even to delete backup
  38. archives.
  39. You can use some monitoring process or just include the free space information
  40. in your backup log files (you check them regularly anyway, right?).
  41. Also helpful:
  42. - create a big file as a "space reserve", that you can delete to free space
  43. - if you use LVM: use a LV + a filesystem that you can resize later and have
  44. some unallocated PEs you can add to the LV.
  45. - consider using quotas
  46. - use `prune` regularly
  47. .. [1] This failsafe can fail in these circumstances:
  48. - The underlying file system doesn't support statvfs(2), or returns incorrect
  49. data, or the repository doesn't reside on a single file system
  50. - Other tasks fill the disk simultaneously
  51. - Hard quotas (which may not be reflected in statvfs(2))
  52. Important note about permissions
  53. --------------------------------
  54. Using root likely will be required if you want to backup files of other users
  55. or the operating system. If you only back up your own files, you neither need
  56. nor want to use root.
  57. Avoid to create a mixup of users and permissions in your repository (or cache).
  58. This can easily happen if you run borg using different user accounts (e.g. your
  59. non-privileged user and root) while accessing the same repo.
  60. Of course, a non-root user will have no permission to work with the files
  61. created by root (or another user) and borg operations will just fail with
  62. `Permission denied`.
  63. The easy way to avoid this is to always access the repo as the same user:
  64. For a local repository just always invoke borg as same user.
  65. For a remote repository: always use e.g. borg@remote_host. You can use this
  66. from different local users, the remote user accessing the repo will always be
  67. borg.
  68. If you need to access a local repository from different users, you can use the
  69. same method by using ssh to borg@localhost.
  70. Automating backups
  71. ------------------
  72. The following example script is meant to be run daily by the ``root`` user on
  73. different local machines. It backs up a machine's important files (but not the
  74. complete operating system) to a repository ``~/backup/main`` on a remote server.
  75. Some files which aren't necessarily needed in this backup are excluded. See
  76. :ref:`borg_patterns` on how to add more exclude options.
  77. After the backup this script also uses the :ref:`borg_prune` subcommand to keep
  78. only a certain number of old archives and deletes the others in order to preserve
  79. disk space.
  80. Before running, make sure that the repository is initialized as documented in
  81. :ref:`remote_repos` and that the script has the correct permissions to be executable
  82. by the root user, but not executable or readable by anyone else, i.e. root:root 0700.
  83. You can use this script as a starting point and modify it where it's necessary to fit
  84. your setup.
  85. Do not forget to test your created backups to make sure everything you need is being
  86. backed up and that the ``prune`` command is keeping and deleting the correct backups.
  87. ::
  88. #!/bin/sh
  89. # Setting this, so the repo does not need to be given on the commandline:
  90. export BORG_REPO=ssh://username@example.com:2022/~/backup/main
  91. # Setting this, so you won't be asked for your repository passphrase:
  92. export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
  93. # or this to ask an external program to supply the passphrase:
  94. export BORG_PASSCOMMAND='pass show backup'
  95. # some helpers and error handling:
  96. info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
  97. trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
  98. info "Starting backup"
  99. # Backup the most important directories into an archive named after
  100. # the machine this script is currently running on:
  101. borg create \
  102. --verbose \
  103. --filter AME \
  104. --list \
  105. --stats \
  106. --show-rc \
  107. --compression lz4 \
  108. --exclude-caches \
  109. --exclude '/home/*/.cache/*' \
  110. --exclude '/var/cache/*' \
  111. --exclude '/var/tmp/*' \
  112. \
  113. ::'{hostname}-{now}' \
  114. /etc \
  115. /home \
  116. /root \
  117. /var \
  118. backup_exit=$?
  119. info "Pruning repository"
  120. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  121. # archives of THIS machine. The '{hostname}-' prefix is very important to
  122. # limit prune's operation to this machine's archives and not apply to
  123. # other machines' archives also:
  124. borg prune \
  125. --list \
  126. --prefix '{hostname}-' \
  127. --show-rc \
  128. --keep-daily 7 \
  129. --keep-weekly 4 \
  130. --keep-monthly 6 \
  131. prune_exit=$?
  132. # use highest exit code as global exit code
  133. global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
  134. if [ ${global_exit} -eq 1 ];
  135. then
  136. info "Backup and/or Prune finished with a warning"
  137. fi
  138. if [ ${global_exit} -gt 1 ];
  139. then
  140. info "Backup and/or Prune finished with an error"
  141. fi
  142. exit ${global_exit}
  143. Pitfalls with shell variables and environment variables
  144. -------------------------------------------------------
  145. This applies to all environment variables you want Borg to see, not just
  146. ``BORG_PASSPHRASE``. The short explanation is: always ``export`` your variable,
  147. and use single quotes if you're unsure of the details of your shell's expansion
  148. behavior. E.g.::
  149. export BORG_PASSPHRASE='complicated & long'
  150. This is because ``export`` exposes variables to subprocesses, which Borg may be
  151. one of. More on ``export`` can be found in the "ENVIRONMENT" section of the
  152. bash(1) man page.
  153. Beware of how ``sudo`` interacts with environment variables. For example, you
  154. may be surprised that the following ``export`` has no effect on your command::
  155. export BORG_PASSPHRASE='complicated & long'
  156. sudo ./yourborgwrapper.sh # still prompts for password
  157. For more information, refer to the sudo(8) man page and ``env_keep`` in
  158. the sudoers(5) man page.
  159. .. Tip::
  160. To debug what your borg process is actually seeing, find its PID
  161. (``ps aux|grep borg``) and then look into ``/proc/<PID>/environ``.
  162. .. backup_compression:
  163. Backup compression
  164. ------------------
  165. The default is lz4 (very fast, but low compression ratio), but other methods are
  166. supported for different situations.
  167. You can use zstd for a wide range from high speed (and relatively low
  168. compression) using N=1 to high compression (and lower speed) using N=22.
  169. zstd is a modern compression algorithm and might be preferable over zlib and
  170. lzma, except if you need compatibility to older borg versions (< 1.1.4) that
  171. did not yet offer zstd.
  172. $ borg create --compression zstd,N /path/to/repo::arch ~
  173. Other options are:
  174. If you have a fast repo storage and you want minimum CPU usage, no compression::
  175. $ borg create --compression none /path/to/repo::arch ~
  176. If you have a less fast repo storage and you want a bit more compression (N=0..9,
  177. 0 means no compression, 9 means high compression): ::
  178. $ borg create --compression zlib,N /path/to/repo::arch ~
  179. If you have a very slow repo storage and you want high compression (N=0..9, 0 means
  180. low compression, 9 means high compression): ::
  181. $ borg create --compression lzma,N /path/to/repo::arch ~
  182. You'll need to experiment a bit to find the best compression for your use case.
  183. Keep an eye on CPU load and throughput.
  184. .. _encrypted_repos:
  185. Repository encryption
  186. ---------------------
  187. Repository encryption can be enabled or disabled at repository creation time
  188. (the default is enabled, with `repokey` method)::
  189. $ borg init --encryption=none|repokey|keyfile PATH
  190. When repository encryption is enabled all data is encrypted using 256-bit AES_
  191. encryption and the integrity and authenticity is verified using `HMAC-SHA256`_.
  192. All data is encrypted on the client before being written to the repository. This
  193. means that an attacker who manages to compromise the host containing an
  194. encrypted archive will not be able to access any of the data, even while the backup
  195. is being made.
  196. Borg supports different methods to store the AES and HMAC keys.
  197. ``repokey`` mode
  198. The key is stored inside the repository (in its "config" file).
  199. Use this mode if you trust in your good passphrase giving you enough
  200. protection. The repository server never sees the plaintext key.
  201. ``keyfile`` mode
  202. The key is stored on your local disk (in ``~/.config/borg/keys/``).
  203. Use this mode if you want "passphrase and having-the-key" security.
  204. In both modes, the key is stored in encrypted form and can be only decrypted
  205. by providing the correct passphrase.
  206. For automated backups the passphrase can be specified using the
  207. `BORG_PASSPHRASE` environment variable.
  208. .. note:: Be careful about how you set that environment, see
  209. :ref:`this note about password environments <password_env>`
  210. for more information.
  211. .. warning:: The repository data is totally inaccessible without the key
  212. and the key passphrase.
  213. Make a backup copy of the key file (``keyfile`` mode) or repo config
  214. file (``repokey`` mode) and keep it at a safe place, so you still have
  215. the key in case it gets corrupted or lost. Also keep your passphrase
  216. at a safe place.
  217. You can make backups using :ref:`borg_key_export` subcommand.
  218. If you want to print a backup of your key to paper use the ``--paper``
  219. option of this command and print the result, or print this `template`_
  220. if you need a version with QR-Code.
  221. A backup inside of the backup that is encrypted with that key/passphrase
  222. won't help you with that, of course.
  223. .. _template: paperkey.html
  224. .. _remote_repos:
  225. Remote repositories
  226. -------------------
  227. Borg can initialize and access repositories on remote hosts if the
  228. host is accessible using SSH. This is fastest and easiest when Borg
  229. is installed on the remote host, in which case the following syntax is used::
  230. $ borg init user@hostname:/path/to/repo
  231. Note: please see the usage chapter for a full documentation of repo URLs.
  232. Remote operations over SSH can be automated with SSH keys. You can restrict the
  233. use of the SSH keypair by prepending a forced command to the SSH public key in
  234. the remote server's `authorized_keys` file. This example will start Borg
  235. in server mode and limit it to a specific filesystem path::
  236. command="borg serve --restrict-to-path /path/to/repo",restrict ssh-rsa AAAAB3[...]
  237. If it is not possible to install Borg on the remote host,
  238. it is still possible to use the remote host to store a repository by
  239. mounting the remote filesystem, for example, using sshfs::
  240. $ sshfs user@hostname:/path/to /path/to
  241. $ borg init /path/to/repo
  242. $ fusermount -u /path/to
  243. You can also use other remote filesystems in a similar way. Just be careful,
  244. not all filesystems out there are really stable and working good enough to
  245. be acceptable for backup usage.