quickstart.rst 11 KB

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