linux:borg
Differences
This shows you the differences between two versions of the page.
| linux:borg [2026/08/20 07:56] – created 127.0.0.1 | linux:borg [2026/08/24 14:52] (current) – [Inspecting] odefta | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Borg Backup - essential commands ====== | ||
| + | |||
| + | Deduplicating, | ||
| + | |||
| + | Two things that trip everyone up on day one: | ||
| + | |||
| + | * Archive paths are stored **without the leading slash** — you extract '' | ||
| + | * '' | ||
| + | |||
| + | ===== Setup ===== | ||
| + | |||
| + | Point '' | ||
| + | |||
| + | <code bash> | ||
| + | export BORG_REPO=/ | ||
| + | export BORG_REPO=ssh:// | ||
| + | </ | ||
| + | |||
| + | Create the repository. The encryption mode is fixed **at creation** and cannot be changed later: | ||
| + | |||
| + | <code bash> | ||
| + | borg init --encryption=repokey-blake2 :: # key in repo, unlocked by passphrase | ||
| + | borg init --encryption=keyfile-blake2 :: # key stays on this machine only | ||
| + | borg init --encryption=none :: # no key, no passphrase | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | borg key export :: borg-key.txt | ||
| + | borg key export --paper :: # printable version | ||
| + | </ | ||
| + | |||
| + | > Losing the key or passphrase of an encrypted repository means losing the backup. There is no recovery. | ||
| + | |||
| + | ===== Creating archives ===== | ||
| + | |||
| + | <code bash> | ||
| + | borg create --stats --progress ::' | ||
| + | </ | ||
| + | |||
| + | Placeholders usable in archive names: '' | ||
| + | |||
| + | Realistic invocation: | ||
| + | |||
| + | <code bash> | ||
| + | borg create | ||
| + | --one-file-system | ||
| + | --compression zstd, | ||
| + | --exclude-from / | ||
| + | --exclude-caches | ||
| + | --stats | ||
| + | ::' | ||
| + | / /boot /home /data | ||
| + | </ | ||
| + | |||
| + | Preview what would be archived, without writing anything: | ||
| + | |||
| + | <code bash> | ||
| + | borg create --dry-run --list ::test / | head -50 | ||
| + | </ | ||
| + | |||
| + | Back up the output of a command instead of a file: | ||
| + | |||
| + | <code bash> | ||
| + | mysqldump --all-databases | borg create :: | ||
| + | </ | ||
| + | |||
| + | ^ Flag ^ Why you want it ^ | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | ===== Inspecting ===== | ||
| + | |||
| + | <code bash> | ||
| + | borg list # archives in the repository | ||
| + | borg list / | ||
| + | borg list :: | ||
| + | borg list :: | ||
| + | borg info :: | ||
| + | borg info # totals for the whole repository | ||
| + | </ | ||
| + | |||
| + | Custom output, useful for scripting: | ||
| + | |||
| + | <code bash> | ||
| + | borg list --format ' | ||
| + | borg list ::arch --format ' | ||
| + | borg list --json | jq -r ' | ||
| + | </ | ||
| + | |||
| + | What changed between two archives — the second one takes **no** repository prefix: | ||
| + | |||
| + | <code bash> | ||
| + | borg diff ::monday tuesday | ||
| + | borg diff ::monday tuesday etc/ | ||
| + | </ | ||
| + | |||
| + | ===== Restoring ===== | ||
| + | |||
| + | Paths are relative and extraction writes into the **current directory**, | ||
| + | |||
| + | <code bash> | ||
| + | cd / | ||
| + | borg extract :: | ||
| + | borg extract :: | ||
| + | borg extract :: | ||
| + | borg extract --dry-run --list :: | ||
| + | borg extract --strip-components 2 ::arch var/ | ||
| + | </ | ||
| + | |||
| + | Single file to stdout: | ||
| + | |||
| + | <code bash> | ||
| + | borg extract --stdout :: | ||
| + | </ | ||
| + | |||
| + | Browse the archive like a normal folder — needs '' | ||
| + | |||
| + | <code bash> | ||
| + | borg mount :: | ||
| + | borg mount :: / | ||
| + | ls / | ||
| + | borg umount / | ||
| + | </ | ||
| + | |||
| + | Export straight to tar, no intermediate extraction: | ||
| + | |||
| + | <code bash> | ||
| + | borg export-tar :: | ||
| + | borg export-tar :: | ||
| + | </ | ||
| + | |||
| + | ===== Retention ===== | ||
| + | |||
| + | '' | ||
| + | |||
| + | <code bash> | ||
| + | borg prune --list --dry-run --keep-daily 7 --keep-weekly 4 --keep-monthly 12 | ||
| + | borg prune --list | ||
| + | borg compact | ||
| + | </ | ||
| + | |||
| + | Always dry-run first. Restrict to one host if several share the repository: | ||
| + | |||
| + | <code bash> | ||
| + | borg prune --glob-archives ' | ||
| + | </ | ||
| + | |||
| + | ^ Rule ^ Meaning ^ | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | > An archive matched by **no** rule is deleted. Running prune with no keep rule at all deletes everything. | ||
| + | |||
| + | ===== Integrity ===== | ||
| + | |||
| + | <code bash> | ||
| + | borg check # structural: repository + archive metadata | ||
| + | borg check --verify-data | ||
| + | borg check --repository-only | ||
| + | </ | ||
| + | |||
| + | Rough guidance: structural weekly, '' | ||
| + | |||
| + | <code bash> | ||
| + | borg check --repair | ||
| + | </ | ||
| + | |||
| + | ===== Housekeeping and troubleshooting ===== | ||
| + | |||
| + | <code bash> | ||
| + | borg delete :: | ||
| + | borg delete --glob-archives ' | ||
| + | borg delete :: # the whole repository (asks for confirmation) | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | borg break-lock :: # stale lock after a crash - check nothing runs first | ||
| + | borg delete --cache-only :: # rebuild the local cache | ||
| + | borg with-lock :: cp -a repo /elsewhere | ||
| + | </ | ||
| + | |||
| + | Retroactively drop files from existing archives (rewrites them): | ||
| + | |||
| + | <code bash> | ||
| + | borg recreate --list --dry-run --exclude ' | ||
| + | borg recreate --recompress --compression zstd,10 :: | ||
| + | </ | ||
| + | |||
| + | ^ Message ^ Cause ^ | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | ===== Environment variables ===== | ||
| + | |||
| + | ^ Variable ^ Use ^ | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | > Under **systemd**, | ||
| + | |||
| + | ===== Cron / systemd skeleton ===== | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | set -uo pipefail | ||
| + | export BORG_REPO=/ | ||
| + | export BORG_PASSCOMMAND=' | ||
| + | |||
| + | borg create --one-file-system --compression zstd,3 \ | ||
| + | --exclude-from / | ||
| + | ::' | ||
| + | rc=$? | ||
| + | |||
| + | borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly 12 | ||
| + | borg compact | ||
| + | |||
| + | # 0 = ok, 1 = warnings (archive is still valid), 2 = error | ||
| + | [ $rc -ge 2 ] && exit 1 | ||
| + | exit 0 | ||
| + | </ | ||
| + | |||
| + | Exit codes: **0** success, **1** warning (archive written and usable), **2** error. | ||
| + | |||
| + | ===== See also ===== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[linux: | ||
linux/borg.txt · Last modified: by odefta
