quickstart.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 various use cases.
  7. A step by step example
  8. ----------------------
  9. .. include:: quickstart_example.rst.inc
  10. Archives and repositories
  11. -------------------------
  12. A *Borg archive* is the result of a single backup (``borg create``). An archive
  13. stores a snapshot of the data of the files "inside" it. One can later extract or
  14. mount an archive to restore from a backup.
  15. *Repositories* are filesystem directories acting as self-contained stores of archives.
  16. Repositories can be accessed locally via path or remotely via ssh. Under the hood,
  17. repositories contain data blocks and a manifest that tracks which blocks are in each
  18. archive. If some data hasn't changed between backups, Borg simply
  19. references an already uploaded data chunk (deduplication).
  20. .. _about_free_space:
  21. Important note about free space
  22. -------------------------------
  23. Before you start creating backups, ensure that there is *always* plenty
  24. of free space on the destination 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 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 do 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 archives.
  38. You can use some monitoring process or just include the free space information
  39. in your backup log files (you check them regularly anyway, right?).
  40. Also helpful:
  41. - create a big file as a "space reserve", that you can delete to free space
  42. - if you use LVM: use a LV + a filesystem that you can resize later and have
  43. some unallocated PEs you can add to the LV.
  44. - consider using quotas
  45. - use `prune` and `compact` regularly
  46. .. [1] This failsafe can fail in these circumstances:
  47. - The underlying file system doesn't support statvfs(2), or returns incorrect
  48. data, or the repository doesn't reside on a single file system
  49. - Other tasks fill the disk simultaneously
  50. - Hard quotas (which may not be reflected in statvfs(2))
  51. Important note about permissions
  52. --------------------------------
  53. To avoid permission issues (in your borg repository or borg cache), **always
  54. access the repository using the same user account**.
  55. If you want to back up files of other users or the operating system, running
  56. borg as root likely will be required (otherwise you get `Permission denied`
  57. errors).
  58. If you only back up your own files, run it as your normal user (i.e. not root).
  59. For a local repository always use the same user to invoke borg.
  60. For a remote repository: always use e.g. ssh://borg@remote_host. You can use this
  61. from different local users, the remote user running borg and accessing the
  62. repo will always be `borg`.
  63. If you need to access a local repository from different users, you can use the
  64. same method by using ssh to borg@localhost.
  65. Important note about files changing during the backup process
  66. -------------------------------------------------------------
  67. Borg does not do anything about the internal consistency of the data
  68. it backs up. It just reads and backs up each file in whatever state
  69. that file is when Borg gets to it. On an active system, this can lead
  70. to two kinds of inconsistency:
  71. - By the time Borg backs up a file, it might have changed since the backup process was initiated
  72. - A file could change while Borg is backing it up, making the file internally inconsistent
  73. If you have a set of files and want to ensure that they are backed up
  74. in a specific or consistent state, you must take steps to prevent
  75. changes to those files during the backup process. There are a few
  76. common techniques to achieve this.
  77. - Avoid running any programs that might change the files.
  78. - Snapshot files, filesystems, container storage volumes, or logical volumes.
  79. LVM or ZFS might be useful here.
  80. - Dump databases or stop the database servers.
  81. - Shut down virtual machines before backing up their disk image files.
  82. - Shut down containers before backing up their storage volumes.
  83. For some systems, Borg might work well enough without these
  84. precautions. If you are simply backing up the files on a system that
  85. isn't very active (e.g. in a typical home directory), Borg usually
  86. works well enough without further care for consistency. Log files and
  87. caches might not be in a perfect state, but this is rarely a problem.
  88. For databases, virtual machines, and containers, there are specific
  89. techniques for backing them up that do not simply use Borg to back up
  90. the underlying filesystem. For databases, check your database
  91. documentation for techniques that will save the database state between
  92. transactions. For virtual machines, consider running the backup on
  93. the VM itself or mounting the filesystem while the VM is shut down.
  94. For Docker containers, perhaps docker's "save" command can help.
  95. Automating backups
  96. ------------------
  97. The following example script is meant to be run daily by the ``root`` user on
  98. different local machines. It backs up a machine's important files (but not the
  99. complete operating system) to a repository ``~/backup/main`` on a remote server.
  100. Some files which aren't necessarily needed in this backup are excluded. See
  101. :ref:`borg_patterns` on how to add more exclude options.
  102. After the backup, this script also uses the :ref:`borg_prune` subcommand to keep
  103. a certain number of old archives and deletes the others.
  104. Finally, it uses the :ref:`borg_compact` subcommand to remove deleted objects
  105. from the segment files in the repository to free disk space.
  106. Before running, make sure that the repository is initialized as documented in
  107. :ref:`remote_repos` and that the script has the correct permissions to be executable
  108. by the root user, but not executable or readable by anyone else, i.e. root:root 0700.
  109. You can use this script as a starting point and modify it where it's necessary to fit
  110. your setup.
  111. Do not forget to test your created backups to make sure everything you need is
  112. backed up and that the ``prune`` command keeps and deletes the correct backups.
  113. ::
  114. #!/bin/sh
  115. # Setting this, so the repo does not need to be given on the commandline:
  116. export BORG_REPO=ssh://username@example.com:2022/~/backup/main
  117. # See the section "Passphrase notes" for more infos.
  118. export BORG_PASSPHRASE='XYZl0ngandsecurepa_55_phrasea&&123'
  119. # some helpers and error handling:
  120. info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
  121. trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
  122. info "Starting backup"
  123. # Back up the most important directories into an archive named after
  124. # the machine this script is currently running on:
  125. borg create \
  126. --verbose \
  127. --filter AME \
  128. --list \
  129. --stats \
  130. --show-rc \
  131. --compression lz4 \
  132. --exclude-caches \
  133. --exclude 'home/*/.cache/*' \
  134. --exclude 'var/tmp/*' \
  135. \
  136. '{hostname}-{now}' \
  137. /etc \
  138. /home \
  139. /root \
  140. /var
  141. backup_exit=$?
  142. info "Pruning repository"
  143. # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
  144. # archives of THIS machine. The '{hostname}-*' globbing is very important to
  145. # limit prune's operation to this machine's archives and not apply to
  146. # other machines' archives also:
  147. borg prune \
  148. --list \
  149. --match-archives 'sh:{hostname}-*' \
  150. --show-rc \
  151. --keep-daily 7 \
  152. --keep-weekly 4 \
  153. --keep-monthly 6
  154. prune_exit=$?
  155. # actually free repo disk space by compacting segments
  156. info "Compacting repository"
  157. borg compact
  158. compact_exit=$?
  159. # use highest exit code as global exit code
  160. global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
  161. global_exit=$(( compact_exit > global_exit ? compact_exit : global_exit ))
  162. if [ ${global_exit} -eq 0 ]; then
  163. info "Backup, Prune, and Compact finished successfully"
  164. elif [ ${global_exit} -eq 1 ]; then
  165. info "Backup, Prune, and/or Compact finished with warnings"
  166. else
  167. info "Backup, Prune, and/or Compact finished with errors"
  168. fi
  169. exit ${global_exit}
  170. Pitfalls with shell variables and environment variables
  171. -------------------------------------------------------
  172. This applies to all environment variables you want Borg to see, not just
  173. ``BORG_PASSPHRASE``. TL;DR: always ``export`` your variable,
  174. and use single quotes if you're unsure of the details of your shell's expansion
  175. behavior. E.g.::
  176. export BORG_PASSPHRASE='complicated & long'
  177. This is because ``export`` exposes variables to subprocesses, which Borg may be
  178. one of. More on ``export`` can be found in the "ENVIRONMENT" section of the
  179. bash(1) man page.
  180. Beware of how ``sudo`` interacts with environment variables. For example, you
  181. may be surprised that the following ``export`` has no effect on your command::
  182. export BORG_PASSPHRASE='complicated & long'
  183. sudo ./yourborgwrapper.sh # still prompts for password
  184. For more information, refer to the sudo(8) man page and ``env_keep`` in
  185. the sudoers(5) man page.
  186. .. Tip::
  187. To debug what your borg process sees, find its PID
  188. (``ps aux|grep borg``) and then look into ``/proc/<PID>/environ``.
  189. .. passphrase_notes:
  190. Passphrase notes
  191. ----------------
  192. If you use encryption (or authentication), Borg will ask you interactively
  193. for a passphrase to encrypt/decrypt the keyfile / repokey.
  194. A passphrase should be a single line of text. Any trailing linefeed will be
  195. stripped.
  196. Do not use empty passphrases, as these can be trivially guessed, which does not
  197. leave any encrypted data secure.
  198. Avoid passphrases containing non-ASCII characters.
  199. Borg can process any unicode text, but problems may arise at input due to text
  200. encoding or differing keyboard layouts, so best just avoid non-ASCII stuff.
  201. See: https://xkcd.com/936/
  202. If you want to automate, you can supply the passphrase
  203. directly or indirectly with the use of environment variables.
  204. Supply a passphrase directly::
  205. # use this passphrase (use safe permissions on the script!):
  206. export BORG_PASSPHRASE='my super secret passphrase'
  207. Or delegate to an external program to supply the passphrase::
  208. # use the "pass" password manager to get the passphrase:
  209. export BORG_PASSCOMMAND='pass show backup'
  210. # use GPG to get the passphrase contained in a gpg-encrypted file:
  211. export BORG_PASSCOMMAND='gpg --decrypt borg-passphrase.gpg'
  212. Or read the passphrase from an open file descriptor::
  213. export BORG_PASSPHRASE_FD=42
  214. Using hardware crypto devices (like Nitrokey, Yubikey and others) is not
  215. directly supported by borg, but you can use these indirectly.
  216. E.g. if your crypto device supports GPG and borg calls ``gpg`` via
  217. ``BORG_PASSCOMMAND``, it should just work.
  218. .. backup_compression:
  219. Backup compression
  220. ------------------
  221. The default is lz4 (very fast, but low compression ratio), but other methods are
  222. supported for different situations.
  223. You can use zstd for a wide range from high speed (and relatively low
  224. compression) using N=1 to high compression (and lower speed) using N=22.
  225. zstd is a modern compression algorithm and might be preferable over zlib and
  226. lzma.::
  227. $ borg create --compression zstd,N arch ~
  228. Other options are:
  229. If you have a fast repo storage and you want minimum CPU usage, no compression::
  230. $ borg create --compression none arch ~
  231. If you have a less fast repo storage and you want a bit more compression (N=0..9,
  232. 0 means no compression, 9 means high compression):
  233. ::
  234. $ borg create --compression zlib,N arch ~
  235. If you have a very slow repo storage and you want high compression (N=0..9, 0 means
  236. low compression, 9 means high compression):
  237. ::
  238. $ borg create --compression lzma,N arch ~
  239. You'll need to experiment a bit to find the best compression for your use case.
  240. Keep an eye on CPU load and throughput.
  241. .. _encrypted_repos:
  242. Repository encryption
  243. ---------------------
  244. You can choose the repository encryption mode at repository creation time::
  245. $ borg rcreate --encryption=MODE
  246. For a list of available encryption MODEs and their descriptions, please refer
  247. to :ref:`borg_rcreate`.
  248. If you use encryption, all data is encrypted on the client before being written
  249. to the repository.
  250. This means that an attacker who manages to compromise the host containing an
  251. encrypted repository will not be able to access any of the data, even while the
  252. backup is being made.
  253. Key material is stored in encrypted form and can be only decrypted by providing
  254. the correct passphrase.
  255. For automated backups the passphrase can be specified using the
  256. `BORG_PASSPHRASE` environment variable.
  257. .. note:: Be careful about how you set that environment, see
  258. :ref:`this note about password environments <password_env>`
  259. for more information.
  260. .. warning:: The repository data is totally inaccessible without the key
  261. and the key passphrase.
  262. Make a backup copy of the key file (``keyfile`` mode) or repo config
  263. file (``repokey`` mode) and keep it at a safe place, so you still have
  264. the key in case it gets corrupted or lost. Also keep your passphrase
  265. at a safe place. You can make backups using :ref:`borg_key_export`
  266. subcommand.
  267. If you want to print a backup of your key to paper use the ``--paper``
  268. option of this command and print the result, or print this `template`_
  269. if you need a version with QR-Code.
  270. A backup inside of the backup that is encrypted with that key/passphrase
  271. won't help you with that, of course.
  272. .. _template: paperkey.html
  273. .. _remote_repos:
  274. Remote repositories
  275. -------------------
  276. Borg can initialize and access repositories on remote hosts if the
  277. host is accessible using SSH. This is fastest and easiest when Borg
  278. is installed on the remote host, in which case the following syntax is used::
  279. $ borg -r ssh://user@hostname:port/path/to/repo rcreate ...
  280. Note: please see the usage chapter for a full documentation of repo URLs.
  281. Remote operations over SSH can be automated with SSH keys. You can restrict the
  282. use of the SSH keypair by prepending a forced command to the SSH public key in
  283. the remote server's `authorized_keys` file. This example will start Borg
  284. in server mode and limit it to a specific filesystem path::
  285. command="borg serve --restrict-to-path /path/to/repo",restrict ssh-rsa AAAAB3[...]
  286. If it is not possible to install Borg on the remote host,
  287. it is still possible to use the remote host to store a repository by
  288. mounting the remote filesystem, for example, using sshfs::
  289. $ sshfs user@hostname:/path/to /path/to
  290. $ borg -r /path/to/repo rcreate ...
  291. $ fusermount -u /path/to
  292. You can also use other remote filesystems in a similar way. Just be careful,
  293. not all filesystems out there are really stable and working good enough to
  294. be acceptable for backup usage.
  295. Restoring a backup
  296. ------------------
  297. Please note that we describe only the most basic commands and options
  298. here. Refer to the command reference to see more.
  299. To restore, work **on the same machine as the same user**
  300. that was used to create the backups of the wanted files. Doing so
  301. avoids issues such as:
  302. - confusion relating to paths
  303. - mapping of user/group names to user/group IDs
  304. - permissions
  305. You likely already have a working borg setup there, including perhaps:
  306. - an environment variable for the key passphrase (for encrypted repos),
  307. - a keyfile for the repo (not needed for repokey mode),
  308. - a ssh key for the repo server (not needed for locally mounted repos),
  309. - a valid borg cache for that repo (quicker than cache rebuild).
  310. The **user** might be:
  311. - root (if full backups, backups including system stuff or multiple
  312. users' files were made)
  313. - some specific user using sudo to execute borg as root
  314. - some specific user (if backups of that user's files were made)
  315. A borg **backup repository** can be either:
  316. - in a local directory (like e.g. a locally mounted USB disk)
  317. - on a remote backup server machine that is reachable via ssh (client/server)
  318. If the repository is encrypted, you will also need the **key** and the **passphrase**
  319. (which is protecting the key).
  320. The **key** can be located:
  321. - in the repository (**repokey** mode).
  322. Easy, this will usually "just work".
  323. - in the home directory of the user who made the backup (**keyfile** mode).
  324. This may cause a bit more effort:
  325. - if you have just lost that home directory and you first need to restore the
  326. borg key (e.g. from the separate backup you made of it or from another
  327. user or machine accessing the same repository).
  328. - if you first must find out the correct machine / user / home directory
  329. (where the borg client was run to make the backups).
  330. The **passphrase** for the key has been either:
  331. - entered interactively at backup time
  332. (not practical if backup is automated / unattended).
  333. - acquired via some environment variable driven mechanism in the backup script
  334. (look there for BORG_PASSPHRASE, BORG_PASSCOMMAND, etc. and just do it like
  335. that).
  336. There are **2 ways to restore** files from a borg backup repository:
  337. - **borg mount** - use this if:
  338. - you don't know exactly which files you want to restore
  339. - you don't know which archive contains the files (in the state) you want
  340. - you need to look into files / directories before deciding what you want
  341. - you need a relatively low volume of data restored
  342. - you don't care for restoring stuff that FUSE mount does not implement yet
  343. (like special fs flags, ACLs)
  344. - you have a client with good resources (RAM, CPU, temporary disk space)
  345. - you would rather use some filemanager to restore (copy) files than borg
  346. extract shell commands
  347. - **borg extract** - use this if:
  348. - you know precisely what you want (repo, archive, path)
  349. - you need a high volume of files restored (best speed)
  350. - you want a as-complete-as-it-gets reproduction of file metadata
  351. (like special fs flags, ACLs)
  352. - you have a client with low resources (RAM, CPU, temp. disk space)
  353. Example with **borg mount**:
  354. ::
  355. # open a new, separate terminal (this terminal will be blocked until umount)
  356. # now we find out the archive names we have in the repo:
  357. borg rlist
  358. # mount one archive from a borg repo:
  359. borg mount -a myserver-system-2019-08-11 /mnt/borg
  360. # alternatively, mount all archives from a borg repo (slower):
  361. borg mount /mnt/borg
  362. # it may take a while until you will see stuff in /mnt/borg.
  363. # now use another terminal or file browser and look into /mnt/borg.
  364. # when finished, umount to unlock the repo and unblock the terminal:
  365. borg umount /mnt/borg
  366. Example with **borg extract**:
  367. ::
  368. # borg extract always extracts into current directory and that directory
  369. # should be empty (borg does not support transforming a non-empty dir to
  370. # the state as present in your backup archive).
  371. mkdir borg_restore
  372. cd borg_restore
  373. # now we find out the archive names we have in the repo:
  374. borg rlist
  375. # we could find out the archive contents, esp. the path layout:
  376. borg list myserver-system-2019-08-11
  377. # we extract only some specific path (note: no leading / !):
  378. borg extract myserver-system-2019-08-11 path/to/extract
  379. # alternatively, we could fully extract the archive:
  380. borg extract myserver-system-2019-08-11
  381. # now move the files to the correct place...
  382. Difference when using a **remote borg backup server**:
  383. It is basically all the same as with the local repository, but you need to
  384. refer to the repo using a ``ssh://`` URL.
  385. In the given example, ``borg`` is the user name used to log into the machine
  386. ``backup.example.org`` which runs ssh on port ``2222`` and has the borg repo
  387. in ``/path/to/repo``.
  388. Instead of giving a FQDN or a hostname, you can also give an IP address.
  389. As usual, you either need a password to log in or the backup server might
  390. have authentication set up via ssh ``authorized_keys`` (which is likely the
  391. case if unattended, automated backups were done).
  392. ::
  393. borg -r ssh://borg@backup.example.org:2222/path/to/repo mount /mnt/borg
  394. # or
  395. borg -r ssh://borg@backup.example.org:2222/path/to/repo extract archive