quickstart.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 1. Before a backup can be made a repository has to be initialized::
  11. $ borg init /path/to/repo
  12. 2. Backup the ``~/src`` and ``~/Documents`` directories into an archive called
  13. *Monday*::
  14. $ borg create /path/to/repo::Monday ~/src ~/Documents
  15. 3. The next day create a new archive called *Tuesday*::
  16. $ borg create --stats /path/to/repo::Tuesday ~/src ~/Documents
  17. This backup will be a lot quicker and a lot smaller since only new never
  18. before seen data is stored. The ``--stats`` option causes |project_name| to
  19. output statistics about the newly created archive such as the amount of unique
  20. data (not shared with other archives)::
  21. ------------------------------------------------------------------------------
  22. Archive name: Tuesday
  23. Archive fingerprint: bd31004d58f51ea06ff735d2e5ac49376901b21d58035f8fb05dbf866566e3c2
  24. Time (start): Tue, 2016-02-16 18:15:11
  25. Time (end): Tue, 2016-02-16 18:15:11
  26. Duration: 0.19 seconds
  27. Number of files: 127
  28. ------------------------------------------------------------------------------
  29. Original size Compressed size Deduplicated size
  30. This archive: 4.16 MB 4.17 MB 26.78 kB
  31. All archives: 8.33 MB 8.34 MB 4.19 MB
  32. Unique chunks Total chunks
  33. Chunk index: 132 261
  34. ------------------------------------------------------------------------------
  35. 4. List all archives in the repository::
  36. $ borg list /path/to/repo
  37. Monday Mon, 2016-02-15 19:14:44
  38. Tuesday Tue, 2016-02-16 19:15:11
  39. 5. List the contents of the *Monday* archive::
  40. $ borg list /path/to/repo::Monday
  41. drwxr-xr-x user group 0 Mon, 2016-02-15 18:22:30 home/user/Documents
  42. -rw-r--r-- user group 7961 Mon, 2016-02-15 18:22:30 home/user/Documents/Important.doc
  43. ...
  44. 6. Restore the *Monday* archive by extracting the files relative to the current directory::
  45. $ borg extract /path/to/repo::Monday
  46. 7. Recover disk space by manually deleting the *Monday* archive::
  47. $ borg delete /path/to/repo::Monday
  48. .. Note::
  49. Borg is quiet by default (it works on WARNING log level).
  50. You can use options like ``--progress`` or ``--list`` to get specific
  51. reports during command execution. You can also add the ``-v`` (or
  52. ``--verbose`` or ``--info``) option to adjust the log level to INFO to
  53. get other informational messages.
  54. Important note about free space
  55. -------------------------------
  56. Before you start creating backups, please make sure that there is *always*
  57. a good amount of free space on the filesystem that has your backup repository
  58. (and also on ~/.cache). A few GB should suffice for most hard-drive sized
  59. repositories. See also :ref:`cache-memory-usage`.
  60. Borg doesn't use space reserved for root on repository disks (even when run as root),
  61. on file systems which do not support this mechanism (e.g. XFS) we recommend to
  62. reserve some space in Borg itself just to be safe by adjusting the
  63. ``additional_free_space`` setting in the ``[repository]`` section of a repositories
  64. ``config`` file. A good starting point is ``2G``.
  65. If |project_name| runs out of disk space, it tries to free as much space as it
  66. can while aborting the current operation safely, which allows to free more space
  67. by deleting/pruning archives. This mechanism is not bullet-proof in some
  68. circumstances [1]_.
  69. If you *really* run out of disk space, it can be hard or impossible to free space,
  70. because |project_name| needs free space to operate - even to delete backup
  71. archives.
  72. You can use some monitoring process or just include the free space information
  73. in your backup log files (you check them regularly anyway, right?).
  74. Also helpful:
  75. - create a big file as a "space reserve", that you can delete to free space
  76. - if you use LVM: use a LV + a filesystem that you can resize later and have
  77. some unallocated PEs you can add to the LV.
  78. - consider using quotas
  79. - use `prune` regularly
  80. .. [1] This failsafe can fail in these circumstances:
  81. - The underlying file system doesn't support statvfs(2), or returns incorrect
  82. data, or the repository doesn't reside on a single file system
  83. - Other tasks fill the disk simultaneously
  84. - Hard quotas (which may not be reflected in statvfs(2))
  85. Automating backups
  86. ------------------
  87. The following example script backs up ``/home`` and ``/var/www`` to a remote
  88. server. The script also uses the :ref:`borg_prune` subcommand to maintain a
  89. certain number of old archives:
  90. ::
  91. #!/bin/sh
  92. # setting this, so the repo does not need to be given on the commandline:
  93. export BORG_REPO=username@remoteserver.com:backup
  94. # setting this, so you won't be asked for your passphrase - make sure the
  95. # script has appropriate owner/group and mode, e.g. root.root 600:
  96. export BORG_PASSPHRASE=mysecret
  97. # Backup most important stuff:
  98. borg create --stats -C lz4 ::'{hostname}-{now:%Y-%m-%d}' \
  99. /etc \
  100. /home \
  101. /var \
  102. --exclude '/home/*/.cache' \
  103. --exclude '*.pyc'
  104. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  105. # archives of THIS machine. The '{hostname}-' prefix is very important to
  106. # limit prune's operation to this machine's archives and not apply to
  107. # other machine's archives also.
  108. borg prune --list $REPOSITORY --prefix '{hostname}-' \
  109. --keep-daily=7 --keep-weekly=4 --keep-monthly=6
  110. Pitfalls with shell variables and environment variables
  111. -------------------------------------------------------
  112. This applies to all environment variables you want borg to see, not just
  113. ``BORG_PASSPHRASE``. The short explanation is: always ``export`` your variable,
  114. and use single quotes if you're unsure of the details of your shell's expansion
  115. behavior. E.g.::
  116. export BORG_PASSPHRASE='complicated & long'
  117. This is because ``export`` exposes variables to subprocesses, which borg may be
  118. one of. More on ``export`` can be found in the "ENVIRONMENT" section of the
  119. bash(1) man page.
  120. Beware of how ``sudo`` interacts with environment variables. For example, you
  121. may be surprised that the following ``export`` has no effect on your command::
  122. export BORG_PASSPHRASE='complicated & long'
  123. sudo ./yourborgwrapper.sh # still prompts for password
  124. For more information, see sudo(8) man page. Hint: see ``env_keep`` in
  125. sudoers(5), or try ``sudo BORG_PASSPHRASE='yourphrase' borg`` syntax.
  126. .. Tip::
  127. To debug what your borg process is actually seeing, find its PID
  128. (``ps aux|grep borg``) and then look into ``/proc/<PID>/environ``.
  129. .. backup_compression:
  130. Backup compression
  131. ------------------
  132. Default is no compression, but we support different methods with high speed
  133. or high compression:
  134. If you have a fast repo storage and you want some compression: ::
  135. $ borg create --compression lz4 /path/to/repo::arch ~
  136. If you have a less fast repo storage and you want a bit more compression (N=0..9,
  137. 0 means no compression, 9 means high compression): ::
  138. $ borg create --compression zlib,N /path/to/repo::arch ~
  139. If you have a very slow repo storage and you want high compression (N=0..9, 0 means
  140. low compression, 9 means high compression): ::
  141. $ borg create --compression lzma,N /path/to/repo::arch ~
  142. You'll need to experiment a bit to find the best compression for your use case.
  143. Keep an eye on CPU load and throughput.
  144. .. _encrypted_repos:
  145. Repository encryption
  146. ---------------------
  147. Repository encryption can be enabled or disabled at repository creation time
  148. (the default is enabled, with `repokey` method)::
  149. $ borg init --encryption=none|repokey|keyfile PATH
  150. When repository encryption is enabled all data is encrypted using 256-bit AES_
  151. encryption and the integrity and authenticity is verified using `HMAC-SHA256`_.
  152. All data is encrypted on the client before being written to the repository. This
  153. means that an attacker who manages to compromise the host containing an
  154. encrypted archive will not be able to access any of the data, even while the backup
  155. is being made.
  156. |project_name| supports different methods to store the AES and HMAC keys.
  157. ``repokey`` mode
  158. The key is stored inside the repository (in its "config" file).
  159. Use this mode if you trust in your good passphrase giving you enough
  160. protection. The repository server never sees the plaintext key.
  161. ``keyfile`` mode
  162. The key is stored on your local disk (in ``~/.config/borg/keys/``).
  163. Use this mode if you want "passphrase and having-the-key" security.
  164. In both modes, the key is stored in encrypted form and can be only decrypted
  165. by providing the correct passphrase.
  166. For automated backups the passphrase can be specified using the
  167. `BORG_PASSPHRASE` environment variable.
  168. .. note:: Be careful about how you set that environment, see
  169. :ref:`this note about password environments <password_env>`
  170. for more information.
  171. .. warning:: The repository data is totally inaccessible without the key
  172. and the key passphrase.
  173. Make a backup copy of the key file (``keyfile`` mode) or repo config
  174. file (``repokey`` mode) and keep it at a safe place, so you still have
  175. the key in case it gets corrupted or lost. Also keep your passphrase
  176. at a safe place.
  177. You can make backups using :ref:`borg_key_export` subcommand.
  178. If you want to print a backup of your key to paper use the ``--paper``
  179. option of this command and print the result.
  180. A backup inside of the backup that is encrypted with that key/passphrase
  181. won't help you with that, of course.
  182. .. _remote_repos:
  183. Remote repositories
  184. -------------------
  185. |project_name| can initialize and access repositories on remote hosts if the
  186. host is accessible using SSH. This is fastest and easiest when |project_name|
  187. is installed on the remote host, in which case the following syntax is used::
  188. $ borg init user@hostname:/path/to/repo
  189. Note: please see the usage chapter for a full documentation of repo URLs.
  190. Remote operations over SSH can be automated with SSH keys. You can restrict the
  191. use of the SSH keypair by prepending a forced command to the SSH public key in
  192. the remote server's `authorized_keys` file. This example will start |project_name|
  193. in server mode and limit it to a specific filesystem path::
  194. 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[...]
  195. If it is not possible to install |project_name| on the remote host,
  196. it is still possible to use the remote host to store a repository by
  197. mounting the remote filesystem, for example, using sshfs::
  198. $ sshfs user@hostname:/path/to /path/to
  199. $ borg init /path/to/repo
  200. $ fusermount -u /path/to
  201. You can also use other remote filesystems in a similar way. Just be careful,
  202. not all filesystems out there are really stable and working good enough to
  203. be acceptable for backup usage.