2
0

quickstart.rst 11 KB

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