2019-02-04 07:20:59 +01:00
---
title: How to set up backups with borgmatic
---
## Installation
2019-12-13 20:42:17 +01:00
First, [install
Borg](https://borgbackup.readthedocs.io/en/stable/installation.html), at least
version 1.1.
2019-02-04 07:20:59 +01:00
2019-12-13 20:42:17 +01:00
Then, download and install borgmatic by running the following command:
2019-02-04 07:20:59 +01:00
```bash
2019-05-12 01:14:30 +02:00
sudo pip3 install --user --upgrade borgmatic
2019-02-04 07:20:59 +01:00
```
2019-05-12 01:14:30 +02:00
This is a [recommended user site
installation](https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site).
2019-11-07 20:05:41 +01:00
You will need to ensure that `/root/.local/bin` is available on your `$PATH`
so
that the borgmatic executable is available. For instance, adding this to
root's `~/.profile` or `~/.bash_profile` may do the trick:
```bash
export PATH="$PATH:~/.local/bin"
```
2019-05-12 01:14:30 +02:00
2019-02-04 07:20:59 +01:00
Note that your pip binary may have a different name than "pip3". Make sure
you're using Python 3, as borgmatic does not support Python 2.
### Other ways to install
Along with the above process, you have several other options for installing
borgmatic:
2019-11-13 23:59:49 +01:00
* [Docker image with scheduled backups ](https://hub.docker.com/r/b3vis/borgmatic/ )
2019-03-16 23:04:48 +01:00
* [Docker base image ](https://hub.docker.com/r/monachus/borgmatic/ )
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/ )
2019-10-11 23:35:07 +02:00
* [Arch Linux ](https://www.archlinux.org/packages/community/any/borgmatic/ )
2019-02-04 07:20:59 +01:00
* [OpenBSD ](http://ports.su/sysutils/borgmatic )
* [openSUSE ](https://software.opensuse.org/package/borgmatic )
2019-07-14 00:40:26 +02:00
* [stand-alone binary ](https://github.com/cmarquardt/borgmatic-binary )
2019-09-29 01:18:10 +02:00
* [virtualenv ](https://virtualenv.pypa.io/en/stable/ )
2019-02-04 07:20:59 +01:00
2019-05-19 05:59:50 +02:00
## Hosting providers
Need somewhere to store your encrypted offsite backups? The following hosting
providers include specific support for Borg/borgmatic. Using these links and
services helps support borgmatic development and hosting. (These are referral
links, but without any tracking scripts or cookies.)
2019-05-30 00:35:04 +02:00
< ul >
< li class = "referral" > < a href = "https://www.rsync.net/cgi-bin/borg.cgi?campaign=borg&adgroup=borgmatic" > rsync.net< / a > : Cloud Storage provider with full support for borg and any other SSH/SFTP tool< / li >
< 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 >
< / ul >
2019-05-19 05:59:50 +02:00
2019-02-04 07:20:59 +01:00
## Configuration
After you install borgmatic, generate a sample configuration file:
```bash
sudo generate-borgmatic-config
```
If that command is not found, then it may be installed in a location that's
2019-11-07 20:09:27 +01:00
not in your system `PATH` (see above). Try looking in `~/.local/bin/` .
2019-02-04 07:20:59 +01:00
This generates a sample configuration file at /etc/borgmatic/config.yaml (by
default). You should edit the file to suit your needs, as the values are
2019-03-05 00:15:49 +01:00
representative. All options are optional except where indicated, so feel free
2019-02-04 07:20:59 +01:00
to ignore anything you don't need.
2019-03-05 00:15:49 +01:00
Note that the configuration file is organized into distinct sections, each
with a section name like `location:` or `storage:` . So take care that if you
uncomment a particular option, also uncomment its containing section name, or
2019-07-01 01:58:01 +02:00
else borgmatic won't recognize the option. Also be sure to use spaces rather
than tabs for indentation; YAML does not allow tabs.
2019-03-05 00:15:49 +01:00
2019-02-05 07:12:33 +01:00
You can also 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
added new options
since you originally created your configuration file. Also check out how to
[upgrade your
configuration](https://torsion.org/borgmatic/docs/how-to/upgrade/#upgrading-your-configuration).
2019-02-04 07:20:59 +01:00
### Encryption
Note that if you plan to run borgmatic on a schedule with cron, and you
encrypt your Borg repository with a passphrase instead of a key file, you'll
either need to set the borgmatic `encryption_passphrase` configuration
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.
Alternatively, you can specify the passphrase programatically by setting
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.
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:
```bash
sudo validate-borgmatic-config
```
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
2019-02-04 07:20:59 +01:00
## Initialization
Before you can create backups with borgmatic, you first need to initialize 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:
```bash
2019-06-23 07:09:50 +02:00
borgmatic init --encryption repokey
2019-02-04 07:20:59 +01:00
```
2019-06-24 01:42:23 +02:00
(No borgmatic `init` action? Try the old-style `--init` flag, or upgrade
borgmatic!)
2019-02-04 07:20:59 +01:00
This uses the borgmatic configuration file you created above to determine
which local or remote repository to create, and encrypts it with the
encryption passphrase specified there if one is provided. Read about [Borg
encryption
2019-09-14 23:30:28 +02:00
modes](https://borgbackup.readthedocs.io/en/stable/usage/init.html#encryption-modes)
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
2019-02-04 07:20:59 +01:00
background about repository initialization.
Note that borgmatic skips repository initialization if the repository already
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
Now that you've configured borgmatic and initialized a repository, it's a
good idea to test that borgmatic is working. So to run borgmatic and start a
backup, you can invoke it like this:
```bash
borgmatic --verbosity 1
```
By default, this will also prune any old backups as per the configured
retention policy, and check backups for consistency problems due to things
like file damage.
The verbosity flag makes borgmatic list the files that it's archiving, which
are those that are new or changed since the last backup. Eyeball the list and
see if it matches your expectations based on the configuration.
## 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
file](https://projects.torsion.org/witten/borgmatic/src/master/sample/cron/borgmatic).
Then, from the directory where you downloaded it:
```bash
sudo mv borgmatic /etc/cron.d/borgmatic
sudo chmod +x /etc/cron.d/borgmatic
```
You can modify the cron file if you'd like to run borgmatic more or less frequently.
### systemd
If you're using systemd instead of cron to run jobs, download the [sample
systemd service
2019-06-12 02:03:40 +02:00
file](https://projects.torsion.org/witten/borgmatic/raw/branch/master/sample/systemd/borgmatic.service)
2019-02-04 07:20:59 +01:00
and the [sample systemd timer
2019-06-12 02:03:40 +02:00
file](https://projects.torsion.org/witten/borgmatic/raw/branch/master/sample/systemd/borgmatic.timer).
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/
sudo systemctl enable borgmatic.timer
sudo systemctl start borgmatic.timer
```
Feel free to modify the timer file based on how frequently you'd like
borgmatic to run.
2019-06-20 05:48:54 +02:00
## Colored output
2019-05-12 11:37:15 +02:00
2019-06-20 05:48:54 +02:00
Borgmatic produces colored terminal output by default. It is disabled when a
non-interactive terminal is detected (like a cron job). 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
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.
2019-02-04 07:20:59 +01:00
## Related documentation
2019-10-24 00:35:37 +02:00
* [Make per-application backups ](https://torsion.org/borgmatic/docs/how-to/make-per-application-backups/ )
* [Deal with very large backups ](https://torsion.org/borgmatic/docs/how-to/deal-with-very-large-backups/ )
* [Inspect your backups ](https://torsion.org/borgmatic/docs/how-to/inspect-your-backups/ )
* [Monitor your backups ](https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/ )
2019-11-06 18:31:00 +01:00
* [Upgrade borgmatic ](https://torsion.org/borgmatic/docs/how-to/upgrade/ )
2019-10-24 00:35:37 +02:00
* [borgmatic configuration reference ](https://torsion.org/borgmatic/docs/reference/configuration/ )
* [borgmatic command-line reference ](https://torsion.org/borgmatic/docs/reference/command-line/ )