quickstart.rst 22 KB

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