Table of Contents

Borgmatic

Borgmatic is a software to automate Borg backups.

Deduplication

You have to use the same repository for deduplication to work. So if you'd like to deduplicate blocks for all your workstations, they have to connect to the same repository.

Backup per Application

Borgmatic can be configured to back up different services (generally meaning particular directories on your system) with different configurations. Configurations are given to Borgmatic using .yaml files and can be specified as a launch option. A sample configuration file is as follows:

/etc/borgmatic/example.yaml
location:
    source_directories:
        - /path/to/source
    repositories:
        - backup:/path/to/backup

storage:
    encryption_passphrase: "example password"
    compression: lz4
    remote_rate_limit: 10000
    archive_name_format: '{hostname}-my_service-{now:%Y-%m-%dT%H:%M:%S}'

retention:
    prefix: "{hostname}-my_service-"
    keep_daily: 7
    keep_weekly: 4
    keep_monthly: 12
    keep_yearly: 2

consistency:
    checks:
        - disabled

Automating backups with Systemd

Systemd can be used to run Borgmatic on a schedule. This will require a two-part setup, using a systemd service and corresponding timer. This is configured using an instantiated service for flexibility as follows:

/etc/systemd/system/borgmatic@.service
[Unit]
Description=backup %i with borgmatic
Wants=network-online.target
After=network-online.target
ConditionACPower=true
 
[Service]
Type=oneshot
 
# Lower CPU and I/O priority.
Nice=19
CPUSchedulingPolicy=batch
IOSchedulingClass=best-effort
IOSchedulingPriority=7
IOWeight=100
 
Restart=no
# Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that
# doesn't support this (pre-240 or so), you may have to remove this option.
LogRateLimitIntervalSec=0
 
# Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
# dbus-user-session to be installed.
ExecStart=systemd-inhibit --who="borgmatic-%i" --why="Prevent interrupting backup of %I" /your/borgmatic/path-c /etc/borgmatic/%i.yaml --syslog-verbosity 1

Multiple timer rules can be created to run at different intervals if desired. The following timer will run a daily backup:

/etc/systemd/system/borgmatic-nightly@.timer
[Unit]
Description=%j backups for %i
 
[Timer]
Unit=borgmatic@%i.service
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=15min
 
[Install]
WantedBy=timers.target

backing up Docker Compose stacks

/etc/systemd/system/borgmatic-compose@.service
[Unit]
Description=backup of docker-compose service %i with borgmatic
Wants=network-online.target
After=network-online.target
ConditionACPower=true
 
[Service]
ExecStartPre=systemctl stop docker-compose@%i.service
Type=oneshot
# Lower CPU and I/O priority.
Nice=19
CPUSchedulingPolicy=batch
IOSchedulingClass=best-effort
IOSchedulingPriority=7
IOWeight=100
 
Restart=no
# Prevent rate limiting of borgmatic log events. If you are using an older version of systemd that
# doesn't support this (pre-240 or so), you may have to remove this option.
LogRateLimitIntervalSec=0
 
# Delay start to prevent backups running during boot. Note that systemd-inhibit requires dbus and
# dbus-user-session to be installed.
ExecStart=systemd-inhibit --who="borgmatic-%i" --why="Prevent interrupting backup of %I" /usr/local/bin/borgmatic -c /etc/borgmatic/%i.yaml --syslog-verbosity 1
 
ExecStopPost=/usr/local/bin/restart-compose-after-backup.sh %i

You have to use a script to restart the compose setup when the service is finished:

/usr/local/bin/restart-compose-after-backup.sh
#!/bin/bash
systemctl -q is-enabled "docker-compose@$1.service" && systemctl start "docker-compose@$1.service"
exit 0

And the corresponding systemd timer looks like that:

/etc/systemd/system/borgmatic-compose-nightly@.timer
[Unit]
Description=%j backups for docker-compose service %i
 
[Timer]
Unit=borgmatic-compose@%i.service
OnCalendar=*-*-* 03:15:00
RandomizedDelaySec=15min
 
[Install]
WantedBy=timers.target