2019-02-04 07:20:59 +01:00
---
2020-08-21 23:27:47 +02:00
title: How to set up backups
eleventyNavigation:
2022-05-20 20:11:35 +02:00
key: 📥 Set up backups
2020-08-21 23:27:47 +02:00
parent: How-to guides
order: 0
2019-02-04 07:20:59 +01:00
---
## Installation
2023-09-18 07:46:33 +02:00
### Prerequisites
2023-09-05 01:25:10 +02:00
First, [install
2019-12-13 20:42:17 +01:00
Borg](https://borgbackup.readthedocs.io/en/stable/installation.html), at least
2020-06-17 20:42:40 +02:00
version 1.1. borgmatic does not install Borg automatically so as to avoid
conflicts with existing Borg installations.
2019-02-04 07:20:59 +01:00
2023-09-05 01:25:10 +02:00
Then, [install pipx ](https://pypa.github.io/pipx/installation/ ) as the root
user (with `sudo` ) to make installing borgmatic easy without impacting other
Python applications on your system. If you have trouble installing pipx with
pip, then you can install a system package instead. E.g. on Ubuntu or Debian,
run:
2019-02-04 07:20:59 +01:00
2023-09-18 07:46:33 +02:00
```bash
2023-09-05 01:25:10 +02:00
sudo apt update
sudo apt install pipx
2019-02-04 07:20:59 +01:00
```
2023-09-18 07:46:33 +02:00
### Root install
If you want to run borgmatic on a schedule with privileged access to your
files, then you should install borgmatic as the root user by running the
following commands:
2019-11-07 20:05:41 +01:00
```bash
2023-09-18 07:46:33 +02:00
sudo pipx ensurepath
2023-09-05 01:25:10 +02:00
sudo pipx install borgmatic
2019-11-07 20:05:41 +01:00
```
2019-05-12 01:14:30 +02:00
2023-09-18 07:46:33 +02:00
Check whether this worked with:
2020-05-19 23:19:39 +02:00
```bash
2023-09-18 07:46:33 +02:00
sudo su -
borgmatic --version
2020-05-19 23:19:39 +02:00
```
If borgmatic is properly installed, that should output your borgmatic version.
2023-09-18 07:46:33 +02:00
And if you'd also like `sudo borgmatic` to work, keep reading!
### Non-root install
If you only want to run borgmatic as a non-root user (without privileged file
access) *or* you want to make `sudo borgmatic` work so borgmatic runs as root,
then install borgmatic as a non-root user by running the following commands as
that user:
2020-05-19 23:19:39 +02:00
2023-09-18 07:46:33 +02:00
```bash
pipx ensurepath
pipx install borgmatic
```
This should work even if you've also installed borgmatic as the root user.
Check whether this worked with:
```bash
borgmatic --version
```
If borgmatic is properly installed, that should output your borgmatic version.
You can also try `sudo borgmatic --version` if you intend to run borgmatic
with `sudo` .
2020-05-19 23:19:39 +02:00
2019-02-04 07:20:59 +01:00
### Other ways to install
2020-05-19 23:19:39 +02:00
Besides the approaches described above, there are several other options for
2020-05-19 05:38:43 +02:00
installing borgmatic:
2019-02-04 07:20:59 +01:00
2023-04-17 00:41:17 +02:00
* [container image with scheduled backups ](https://hub.docker.com/r/b3vis/borgmatic/ ) (+ Docker Compose files)
* [container image with multi-arch and Docker CLI support ](https://hub.docker.com/r/modem7/borgmatic-docker/ )
2019-02-04 07:20:59 +01:00
* [Debian ](https://tracker.debian.org/pkg/borgmatic )
* [Ubuntu ](https://launchpad.net/ubuntu/+source/borgmatic )
2019-11-13 23:59:49 +01:00
* [Fedora official ](https://bodhi.fedoraproject.org/updates/?search=borgmatic )
* [Fedora unofficial ](https://copr.fedorainfracloud.org/coprs/heffer/borgmatic/ )
2023-05-13 13:22:47 +02:00
* [Gentoo ](https://packages.gentoo.org/packages/app-backup/borgmatic )
2023-06-05 07:21:16 +02:00
* [Arch Linux ](https://archlinux.org/packages/extra/any/borgmatic/ )
2020-03-11 05:10:02 +01:00
* [Alpine Linux ](https://pkgs.alpinelinux.org/packages?name=borgmatic )
2023-03-28 08:43:39 +02:00
* [OpenBSD ](https://openports.pl/path/sysutils/borgmatic )
2019-02-04 07:20:59 +01:00
* [openSUSE ](https://software.opensuse.org/package/borgmatic )
2022-06-01 17:56:40 +02:00
* [macOS (via Homebrew) ](https://formulae.brew.sh/formula/borgmatic )
2023-02-15 10:47:59 +01:00
* [macOS (via MacPorts) ](https://ports.macports.org/port/borgmatic/ )
2023-03-19 17:02:47 +01:00
* [NixOS ](https://search.nixos.org/packages?show=borgmatic&sort=relevance&type=packages&query=borgmatic )
2021-06-20 04:04:22 +02:00
* [Ansible role ](https://github.com/borgbase/ansible-role-borgbackup )
2023-04-15 18:13:13 +02:00
* [Unraid ](https://unraid.net/community/apps?q=borgmatic#r )
2019-02-04 07:20:59 +01:00
2019-05-19 05:59:50 +02:00
## Hosting providers
2021-04-19 02:28:11 +02:00
Need somewhere to store your encrypted off-site backups? The following hosting
2021-04-19 03:03:43 +02:00
providers include specific support for Borg/borgmatic—and fund borgmatic
2023-07-03 07:14:36 +02:00
development and hosting when you use these referral links to sign up:
2019-05-19 05:59:50 +02:00
2019-05-30 00:35:04 +02:00
< ul >
< li class = "referral" > < a href = "https://www.borgbase.com/?utm_source=borgmatic" > BorgBase< / a > : Borg hosting service with support for monitoring, 2FA, and append-only repos< / li >
2023-07-03 07:14:36 +02:00
< li class = "referral" > < a href = "https://hetzner.cloud/?ref=v9dOJ98Ic9I8" > Hetzner< / a > : A "storage box" that includes support for Borg< / li >
2020-11-17 23:04:24 +01:00
< / ul >
2023-07-03 07:14:36 +02:00
Additionally, rsync.net has a compatible storage offering, but does not fund
borgmatic development or hosting.
2021-04-19 03:03:43 +02:00
2022-05-26 19:27:53 +02:00
2019-02-04 07:20:59 +01:00
## Configuration
After you install borgmatic, generate a sample configuration file:
2023-06-21 21:19:49 +02:00
```bash
sudo borgmatic config generate
```
< span class = "minilink minilink-addedin" > Prior to version 1.7.15< / span >
2023-06-21 22:14:54 +02:00
Generate a configuration file with this command instead:
2023-06-21 21:19:49 +02:00
2019-02-04 07:20:59 +01:00
```bash
sudo generate-borgmatic-config
```
2023-06-21 21:19:49 +02:00
If neither command is found, then borgmatic may be installed in a location
that's not in your system `PATH` (see above). Try looking in `~/.local/bin/` .
2019-02-04 07:20:59 +01:00
2023-06-21 21:19:49 +02:00
The command generates a sample configuration file at
`/etc/borgmatic/config.yaml` by default. If you'd like to use another path,
use the `--destination` flag, for instance: `--destination
~/.config/borgmatic/config.yaml`.
2020-01-22 18:26:58 +01:00
You should edit the configuration file to suit your needs, as the generated
values are only representative. All options are optional except where
2023-07-12 04:42:14 +02:00
indicated, so feel free to ignore anything you don't need. Be sure to use
spaces rather than tabs for indentation; YAML does not allow tabs.
< span class = "minilink minilink-addedin" > Prior to version 1.8.0< / span > The
configuration file was organized into distinct sections, each with a section
name like `location:` or `storage:` . So in older versions of borgmatic, take
care that if you uncomment a particular option, also uncomment its containing
section name—or else borgmatic won't recognize the option.
2019-03-05 00:15:49 +01:00
2020-01-22 18:26:58 +01:00
You can get the same sample configuration file from the [configuration
2019-11-06 18:31:00 +01:00
reference](https://torsion.org/borgmatic/docs/reference/configuration/), the
authoritative set of all configuration options. This is handy if borgmatic has
2020-01-22 18:26:58 +01:00
added new options since you originally created your configuration file. Also
check out how to [upgrade your
2019-11-06 18:31:00 +01:00
configuration](https://torsion.org/borgmatic/docs/how-to/upgrade/#upgrading-your-configuration).
2019-02-04 07:20:59 +01:00
### Encryption
2020-11-26 03:36:23 +01:00
If you encrypt your Borg repository with a passphrase or a key file, you'll
either need to set the borgmatic `encryption_passphrase` configuration
2019-02-04 07:20:59 +01:00
variable or set the `BORG_PASSPHRASE` environment variable. See the
[repository encryption
2019-09-14 23:30:28 +02:00
section](https://borgbackup.readthedocs.io/en/stable/quickstart.html#repository-encryption)
2019-02-04 07:20:59 +01:00
of the Borg Quick Start for more info.
2023-04-01 18:40:32 +02:00
Alternatively, you can specify the passphrase programmatically by setting
2019-02-04 07:20:59 +01:00
either the borgmatic `encryption_passcommand` configuration variable or the
`BORG_PASSCOMMAND` environment variable. See the [Borg Security
FAQ](http://borgbackup.readthedocs.io/en/stable/faq.html#how-can-i-specify-the-encryption-passphrase-programmatically)
for more info.
2020-07-18 01:00:50 +02:00
### Redundancy
If you'd like to configure your backups to go to multiple different
repositories, see the documentation on how to [make backups
redundant](https://torsion.org/borgmatic/docs/how-to/make-backups-redundant/).
2019-05-11 23:05:16 +02:00
### Validation
If you'd like to validate that your borgmatic configuration is valid, the
following command is available for that:
2023-06-23 19:11:41 +02:00
```bash
sudo borgmatic config validate
```
< span class = "minilink minilink-addedin" > Prior to version 1.7.15< / span >
Validate a configuration file with this command instead:
2019-05-11 23:05:16 +02:00
```bash
sudo validate-borgmatic-config
```
2023-04-11 19:49:09 +02:00
You'll need to specify your configuration file with `--config` if it's not in
a default location.
2019-05-11 23:05:16 +02:00
This command's exit status (`$?` in Bash) is zero when configuration is valid
and non-zero otherwise.
Validating configuration can be useful if you generate your configuration
2019-06-12 06:35:43 +02:00
files via configuration management, or you want to double check that your hand
edits are valid.
2019-03-05 00:15:49 +01:00
2022-08-12 23:53:20 +02:00
## Repository creation
2019-02-04 07:20:59 +01:00
2022-08-12 23:53:20 +02:00
Before you can create backups with borgmatic, you first need to create a Borg
repository so you have a destination for your backup archives. (But skip this
step if you already have a Borg repository.) To create a repository, run a
command like the following with Borg 1.x:
2019-02-04 07:20:59 +01:00
```bash
2020-04-17 18:46:50 +02:00
sudo borgmatic init --encryption repokey
2019-02-04 07:20:59 +01:00
```
2022-08-19 18:44:31 +02:00
< span class = "minilink minilink-addedin" > New in borgmatic version 1.7.0< / span >
2022-08-12 23:53:20 +02:00
Or, with Borg 2.x:
```bash
sudo borgmatic rcreate --encryption repokey-aes-ocb
```
2019-06-24 01:42:23 +02:00
2022-08-18 18:59:48 +02:00
(Note that `repokey-chacha20-poly1305` may be faster than `repokey-aes-ocb` on
certain platforms like ARM64.)
2019-02-04 07:20:59 +01:00
This uses the borgmatic configuration file you created above to determine
2023-07-19 08:27:45 +02:00
which local or remote repository to create and encrypts it with the
2019-02-04 07:20:59 +01:00
encryption passphrase specified there if one is provided. Read about [Borg
encryption
2022-08-12 23:53:20 +02:00
modes](https://borgbackup.readthedocs.io/en/stable/usage/init.html#encryption-mode-tldr)
2019-02-04 07:20:59 +01:00
for the menu of available encryption modes.
Also, optionally check out the [Borg Quick
2019-09-14 23:30:28 +02:00
Start](https://borgbackup.readthedocs.org/en/stable/quickstart.html) for more
2022-08-12 23:53:20 +02:00
background about repository creation.
2019-02-04 07:20:59 +01:00
2022-08-12 23:53:20 +02:00
Note that borgmatic skips repository creation if the repository already
2019-02-04 07:20:59 +01:00
exists. This supports use cases like ensuring a repository exists prior to
performing a backup.
If the repository is on a remote host, make sure that your local user has
key-based SSH access to the desired user account on the remote host.
## Backups
2022-08-12 23:53:20 +02:00
Now that you've configured borgmatic and created a repository, it's a good
idea to test that borgmatic is working. So to run borgmatic and start a
2019-02-04 07:20:59 +01:00
backup, you can invoke it like this:
```bash
2022-08-21 23:25:16 +02:00
sudo borgmatic create --verbosity 1 --list --stats
2019-02-04 07:20:59 +01:00
```
2022-08-21 23:25:16 +02:00
(No borgmatic `--list` flag? Try `--files` instead, leave it out, or upgrade
borgmatic!)
2020-05-18 17:43:32 +02:00
2022-07-01 01:54:22 +02:00
The `--verbosity` flag makes borgmatic show the steps it's performing. The
2022-08-21 23:25:16 +02:00
`--list` flag lists each file that's new or changed since the last backup. And
`--stats` shows summary information about the created archive. All of these
flags are optional.
2022-07-01 01:54:22 +02:00
As the command runs, you should eyeball the output to see if it matches your
expectations based on your configuration.
2019-02-04 07:20:59 +01:00
2020-01-22 18:26:58 +01:00
If you'd like to specify an alternate configuration file path, use the
2022-07-01 01:54:22 +02:00
`--config` flag.
See `borgmatic --help` and `borgmatic create --help` for more information.
2020-01-22 18:26:58 +01:00
2019-02-04 07:20:59 +01:00
2022-06-30 06:32:00 +02:00
## Default actions
If you omit `create` and other actions, borgmatic runs through a set of
default actions: `prune` any old backups as per the configured retention
2022-12-23 23:12:48 +01:00
policy, `compact` segments to free up space (with Borg 1.2+, borgmatic
1.5.23+), `create` a backup, *and* `check` backups for consistency problems
due to things like file damage. For instance:
2022-06-30 06:32:00 +02:00
```bash
2022-08-21 23:25:16 +02:00
sudo borgmatic --verbosity 1 --list --stats
2022-06-30 06:32:00 +02:00
```
2019-02-04 07:20:59 +01:00
## Autopilot
Running backups manually is good for validating your configuration, but I'm
guessing that you want to run borgmatic automatically, say once a day. To do
that, you can configure a separate job runner to invoke it periodically.
### cron
If you're using cron, download the [sample cron
2023-04-20 06:43:08 +02:00
file](https://projects.torsion.org/borgmatic-collective/borgmatic/src/main/sample/cron/borgmatic).
2019-02-04 07:20:59 +01:00
Then, from the directory where you downloaded it:
```bash
sudo mv borgmatic /etc/cron.d/borgmatic
sudo chmod +x /etc/cron.d/borgmatic
```
2021-10-11 20:02:08 +02:00
If borgmatic is installed at a different location than
`/root/.local/bin/borgmatic` , edit the cron file with the correct path. You
can also modify the cron file if you'd like to run borgmatic more or less
frequently.
2019-02-04 07:20:59 +01:00
### systemd
2021-06-30 06:38:53 +02:00
If you're using systemd instead of cron to run jobs, you can still configure
borgmatic to run automatically.
(If you installed borgmatic from [Other ways to
install](https://torsion.org/borgmatic/docs/how-to/set-up-backups/#other-ways-to-install),
you may already have borgmatic systemd service and timer files. If so, you may
be able to skip some of the steps below.)
First, download the [sample systemd service
2023-04-20 06:43:08 +02:00
file](https://projects.torsion.org/borgmatic-collective/borgmatic/raw/branch/main/sample/systemd/borgmatic.service)
2019-02-04 07:20:59 +01:00
and the [sample systemd timer
2023-04-20 06:43:08 +02:00
file](https://projects.torsion.org/borgmatic-collective/borgmatic/raw/branch/main/sample/systemd/borgmatic.timer).
2021-06-30 06:38:53 +02:00
2019-02-04 07:20:59 +01:00
Then, from the directory where you downloaded them:
```bash
sudo mv borgmatic.service borgmatic.timer /etc/systemd/system/
2020-01-04 22:37:56 +01:00
sudo systemctl enable --now borgmatic.timer
2019-02-04 07:20:59 +01:00
```
2020-08-22 15:41:25 +02:00
Review the security settings in the service file and update them as needed.
If `ProtectSystem=strict` is enabled and local repositories are used, then
the repository path must be added to the `ReadWritePaths` list.
2019-02-04 07:20:59 +01:00
Feel free to modify the timer file based on how frequently you'd like
borgmatic to run.
2020-04-27 01:10:52 +02:00
### launchd in macOS
If you run borgmatic in macOS with launchd, you may encounter permissions
issues when reading files to backup. If that happens to you, you may be
interested in an [unofficial work-around for Full Disk
2021-09-14 20:32:01 +02:00
Access](https://projects.torsion.org/borgmatic-collective/borgmatic/issues/293).
2020-04-27 01:10:52 +02:00
2020-06-02 21:53:08 +02:00
2022-05-26 19:27:53 +02:00
## Niceties
### Shell completion
2023-04-28 23:02:06 +02:00
borgmatic includes a shell completion script (currently only for Bash and Fish) to
2022-05-26 19:27:53 +02:00
support tab-completing borgmatic command-line actions and flags. Depending on
2023-04-28 23:02:06 +02:00
how you installed borgmatic, this may be enabled by default.
#### Bash
If completions aren't enabled, start by installing the `bash-completion` Linux package or the
2022-06-01 19:57:23 +02:00
[`bash-completion@2` ](https://formulae.brew.sh/formula/bash-completion@2 )
macOS Homebrew formula. Then, install the shell completion script globally:
2022-05-26 19:27:53 +02:00
```bash
2022-06-01 17:56:40 +02:00
sudo su -c "borgmatic --bash-completion > $(pkg-config --variable=completionsdir bash-completion)/borgmatic"
2022-05-26 19:27:53 +02:00
```
2022-06-01 19:57:23 +02:00
If you don't have `pkg-config` installed, you can try the following path
instead:
```bash
sudo su -c "borgmatic --bash-completion > /usr/share/bash-completion/completions/borgmatic"
```
2022-08-22 06:48:37 +02:00
Or, if you'd like to install the script for only the current user:
2022-05-26 19:27:53 +02:00
```bash
mkdir --parents ~/.local/share/bash-completion/completions
borgmatic --bash-completion > ~/.local/share/bash-completion/completions/borgmatic
```
2022-06-01 19:57:23 +02:00
Finally, restart your shell (`exit` and open a new shell) so the completions
take effect.
2022-05-26 19:27:53 +02:00
2023-05-04 22:27:57 +02:00
#### fish
2023-04-28 23:02:06 +02:00
To add completions for fish, install the completions file globally:
```fish
borgmatic --fish-completion | sudo tee /usr/share/fish/vendor_completions.d/borgmatic.fish
source /usr/share/fish/vendor_completions.d/borgmatic.fish
```
2022-05-26 19:27:53 +02:00
### Colored output
2019-05-12 11:37:15 +02:00
2022-05-26 19:27:53 +02:00
borgmatic produces colored terminal output by default. It is disabled when a
2020-01-05 00:50:41 +01:00
non-interactive terminal is detected (like a cron job), or when you use the
`--json` flag. Otherwise, you can disable it by passing the `--no-color` flag,
setting the environment variable `PY_COLORS=False` , or setting the `color`
option to `false` in the `output` section of configuration.
2019-02-04 07:20:59 +01:00
2020-06-02 21:53:08 +02:00
2019-02-05 05:53:47 +01:00
## Troubleshooting
2019-07-01 01:58:01 +02:00
### "found character that cannot start any token" error
If you run borgmatic and see an error looking something like this, it probably
means you've used tabs instead of spaces:
2019-07-01 02:09:34 +02:00
```
2019-07-01 01:58:01 +02:00
test.yaml: Error parsing configuration file
2019-07-01 02:23:09 +02:00
An error occurred while parsing a configuration file at config.yaml:
2019-07-01 01:58:01 +02:00
while scanning for the next token
found character that cannot start any token
2019-07-01 02:23:09 +02:00
in "config.yaml", line 230, column 1
2019-07-01 01:58:01 +02:00
```
2019-10-15 19:49:14 +02:00
YAML does not allow tabs. So to fix this, replace any tabs in your
2019-07-01 01:58:01 +02:00
configuration file with the requisite number of spaces.
2019-02-05 05:53:47 +01:00
### libyaml compilation errors
borgmatic depends on a Python YAML library (ruamel.yaml) that will optionally
use a C YAML library (libyaml) if present. But if it's not installed, then
when installing or upgrading borgmatic, you may see errors about compiling the
YAML library. If so, not to worry. borgmatic should install and function
correctly even without the C YAML library. And borgmatic won't be any faster
with the C library present, so you don't need to go out of your way to install
it.