Install and configure borgbackup on a Hetzner Stgoragebox
  • Puppet 92.9%
  • Pascal 3.8%
  • Shell 3.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-24 07:31:10 +00:00
data Release v4.2.0 2026-11-24 07:31:10 +00:00
files Release v4.2.0 2026-11-24 07:31:10 +00:00
manifests Release v4.2.0 2026-11-24 07:31:10 +00:00
templates Release v4.2.0 2026-11-24 07:31:10 +00:00
types Release v4.2.0 2026-11-24 07:31:10 +00:00
.editorconfig Release v4.1.1 2026-11-22 09:14:44 +00:00
.mdl_style.rb Release v4.1.1 2026-11-22 09:14:44 +00:00
.mdlrc Release v4.1.1 2026-11-22 09:14:44 +00:00
.puppet-lint.rc Release v4.1.1 2026-11-22 09:14:44 +00:00
.yamllint Release v4.1.1 2026-11-22 09:14:44 +00:00
hiera.yaml Release v4.1.1 2026-11-22 09:14:44 +00:00
LICENSE Release v4.1.1 2026-11-22 09:14:44 +00:00
metadata.json Release v4.2.0 2026-11-24 07:31:10 +00:00
README.md Release v4.2.0 2026-11-24 07:31:10 +00:00

borgbackup

Introduction

Install and configure BorgBackup for automated, encrypted backups to a remote repository that is reached over SSH, such as a Hetzner Storage Box.

The module places the SSH key of the node, pins the host key of the repository host, initialises the repository on the first run, and schedules a daily backup with a systemd timer. The backup script can run a script first, for example a database dump, prunes old archives according to the retention policy and sends an email when a backup fails.

Installation

Prerequisites

  • Debian 13 (Trixie).
  • A repository host that is reachable over SSH and accepts the public key of the node, see SSH keys.
  • A mail transfer agent and the mail command, for the notifications.
  • The directory /root/.ssh, which this module does not create.
  • puppetlabs/stdlib 9.0 or newer, installed on the Puppet server from the Debian package puppet-module-puppetlabs-stdlib.

Add the module to the Puppetfile of the control repository, with the latest release tag in place of vVERSION. In a clone of the module, git tag --sort=-version:refname lists the newest tag first.

mod 'borgbackup',
  :git => 'https://git.desktux.nl/public/borgbackup.git',
  :tag => 'vVERSION'

Then assign the class borgbackup to the nodes, through the Hiera classes array or a profile.

Usage

Configuration

The module is disabled by default. Enable it and point it at the repository in hieradata/common.yaml:

borgbackup::email_to: 'admin@example.org'
borgbackup::enabled: true
borgbackup::repo_host: 'u123456.your-storagebox.de'
borgbackup::repo_host_key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...'
borgbackup::repo_user: 'u123456'

The passphrase of the repository goes in hieradata/secrets/common.eyaml, or in the file of a node when every repository has its own:

borgbackup::encryption_passphrase: >
  ENC[PKCS7,...]

SSH keys

The SSH private key that reaches the repository is a secret in Hiera. For one key that all nodes share, it goes in hieradata/secrets/common.eyaml:

borgbackup::ssh_private_key: >
  ENC[PKCS7,...]

Create a key pair without a passphrase, encrypt the private key file as a whole and register the public key on the repository host:

ssh-keygen -t ed25519 -N '' -C 'borgbackup@example.org' -f borgbackup-key

With a shared key, a compromised node can read and delete the backups of every other node. To isolate the nodes, give each one a key of its own in hieradata/secrets/nodes/<certname>.eyaml. That only helps when the repository host restricts each key to the repository of its node. A Hetzner Storage Box does not apply command= restrictions in authorized_keys, and offers a sub-account per node instead, with its own borgbackup::repo_host and borgbackup::repo_user.

Every node prunes and compacts its own repository, so it has to be able to delete there, and an append-only repository does not fit. The protection against a node that deletes its archives, or those of others, is a copy that no node can reach: a snapshot on the repository host. A Hetzner Storage Box takes snapshots on a schedule, which are read-only over SSH. Schedule them after the backups have finished.

Host key

Borg only connects to a host that presents the key in borgbackup::repo_host_key. Get the key from the host and compare its fingerprint with the one the provider publishes, before putting it in Hiera:

ssh-keyscan -t ed25519 -p 23 u123456.your-storagebox.de > host-key
ssh-keygen -l -f host-key

The parameter takes the key type and the key, without the host name. When the provider replaces the host key, every backup fails with "Host key verification failed" until the parameter is updated.

Remote Borg version

Without borgbackup::remote_path, the repository host runs its default Borg, which can be an older series than the client. A Hetzner Storage Box offers several versions side by side and recommends choosing one. Pick the series of the client:

borg --version
borgbackup::remote_path: 'borg-1.4'

Check the value again before a Debian upgrade brings a new Borg series.

Backup paths and exclude patterns

The module backs up /etc, /home, /opt and /srv by default. Paths set in Hiera are added to that list, at every level of the hierarchy, so a node only names its additional paths in hieradata/nodes/<certname>.yaml:

borgbackup::backup_paths:
  - '/var/www'
  - '/var/lib/myapp'

A default path cannot be removed from the list.

Exclude patterns work the same way. The patterns set in Hiera are added to the defaults in data/common.yaml:

borgbackup::exclude_patterns:
  - '/var/cache'
  - '**/*.log'

Retention

The module keeps 14 daily archives by default. A value of 0 switches a period off:

borgbackup::retention_daily: 7
borgbackup::retention_weekly: 4
borgbackup::retention_monthly: 6
borgbackup::retention_yearly: 2

Schedule

The backup starts daily at 01:00, in the time zone of the node, with a random delay of up to 30 minutes. When the node is down or reboots before the backup has started, the backup starts as soon as the node is up again (Persistent=true in the timer). Keep the schedule clear of planned reboots, which interrupt a running backup, and of snapshots of the remote repository, which only protect backups that have finished:

borgbackup::backup_hour: 0
borgbackup::backup_minute: 30
borgbackup::backup_random_delay: 900

Script before the backup

A script can run before the backup, for example to dump databases into a directory that is part of the backup. When the script fails or is missing, the backup is still made, with whatever the script left behind earlier. The run then ends as failed and sends the failure email:

borgbackup::pre_backup_script: '/usr/local/bin/database-backup.sh'

Notifications

borgbackup::email_to: 'admin@example.org'
borgbackup::email_from: "backup@%{facts.networking.fqdn}"
borgbackup::email_on_failure: true

Parameters

borgbackup::backup_hour

  • Type: Integer[0, 23]
  • Default: 1
  • Usage: borgbackup::backup_hour: 3

Hour of the day the backup starts.

borgbackup::backup_minute

  • Type: Integer[0, 59]
  • Default: 0
  • Usage: borgbackup::backup_minute: 30

Minute of the hour the backup starts.

borgbackup::backup_paths

  • Type: Array[Stdlib::Absolutepath]
  • Default: ['/etc', '/home', '/opt', '/srv']
  • Usage: borgbackup::backup_paths: ['/var/www']

Paths to back up. The values of every Hiera level are added to the defaults, and a default cannot be removed.

borgbackup::backup_random_delay

  • Type: Integer[0]
  • Default: 1800
  • Usage: borgbackup::backup_random_delay: 900

Maximum random delay in seconds before the backup starts, so that not all nodes start at the same moment.

borgbackup::compression

  • Type: String[1]
  • Default: 'auto,lzma'
  • Usage: borgbackup::compression: 'auto,zstd'

Compression algorithm, as accepted by borg create --compression.

borgbackup::email_from

  • Type: Stdlib::Email
  • Default: root@<fqdn>
  • Usage: borgbackup::email_from: 'backup@example.org'

Sender address of the notifications.

borgbackup::email_on_failure

  • Type: Boolean
  • Default: true
  • Usage: borgbackup::email_on_failure: false

Send an email when a backup fails.

borgbackup::email_to

  • Type: Optional[Stdlib::Email]
  • Default: undef, required when email_on_failure is true
  • Usage: borgbackup::email_to: 'admin@example.org'

Recipient of the notifications.

borgbackup::enabled

  • Type: Boolean
  • Default: false
  • Usage: borgbackup::enabled: true

Install and configure BorgBackup. When false, the module removes the package, the timer and every file it placed.

borgbackup::encryption_mode

  • Type: Borgbackup::Encryption_mode
  • Default: 'repokey-blake2'
  • Usage: borgbackup::encryption_mode: 'repokey-blake2'

Encryption mode used when the repository is initialised, one of the modes of borg init. Changing it has no effect on an existing repository.

borgbackup::encryption_passphrase

  • Type: Optional[Sensitive[String[1]]]
  • Default: undef, required when enabled is true
  • Usage: borgbackup::encryption_passphrase: ENC[PKCS7,...]

Passphrase of the repository. The module wraps the value from Hiera in Sensitive.

borgbackup::exclude_patterns

  • Type: Array[String[1]]
  • Default: caches, the trash and temporary files, see data/common.yaml
  • Usage: borgbackup::exclude_patterns: ['**/*.log']

Patterns to exclude from the backup. The values of every Hiera level are added to the defaults.

borgbackup::pre_backup_script

  • Type: Optional[Stdlib::Absolutepath]
  • Default: undef
  • Usage: borgbackup::pre_backup_script: '/usr/local/bin/database-backup.sh'

Script to run before the backup, for example a database dump.

borgbackup::remote_path

  • Type: Optional[Borgbackup::Remote_path]
  • Default: undef
  • Usage: borgbackup::remote_path: 'borg-1.4'

Borg executable on the repository host, written to borg-env as BORG_REMOTE_PATH. Letters, digits and ., _, /, - only. With undef, the repository host runs its default Borg. See Remote Borg version.

borgbackup::repo_host

  • Type: Optional[Stdlib::Host]
  • Default: undef, required when enabled is true
  • Usage: borgbackup::repo_host: 'u123456.your-storagebox.de'

Host of the backup repository.

borgbackup::repo_host_key

  • Type: Optional[Borgbackup::Host_key]
  • Default: undef, required when enabled is true
  • Usage: borgbackup::repo_host_key: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...'

Public SSH host key of the repository host, as key type and key. See Host key.

borgbackup::repo_path

  • Type: Stdlib::Absolutepath
  • Default: /backups/<fqdn>
  • Usage: borgbackup::repo_path: '/backups/node'

Path of the repository on the repository host, relative to the home directory of the user.

borgbackup::repo_port

  • Type: Stdlib::Port
  • Default: 23
  • Usage: borgbackup::repo_port: 23

SSH port of the repository host.

borgbackup::repo_user

  • Type: Optional[String[1]]
  • Default: undef, required when enabled is true
  • Usage: borgbackup::repo_user: 'u123456'

User on the repository host.

borgbackup::retention_daily

  • Type: Integer[0]
  • Default: 14
  • Usage: borgbackup::retention_daily: 7

Number of daily archives to keep.

borgbackup::retention_monthly

  • Type: Integer[0]
  • Default: 0
  • Usage: borgbackup::retention_monthly: 6

Number of monthly archives to keep.

borgbackup::retention_weekly

  • Type: Integer[0]
  • Default: 0
  • Usage: borgbackup::retention_weekly: 4

Number of weekly archives to keep.

borgbackup::retention_yearly

  • Type: Integer[0]
  • Default: 0
  • Usage: borgbackup::retention_yearly: 2

Number of yearly archives to keep.

borgbackup::service_name

  • Type: String[1]
  • Default: 'borg-backup'
  • Usage: borgbackup::service_name: 'borg-backup'

Name of the systemd service, without the .service suffix.

borgbackup::ssh_key_path

  • Type: Stdlib::Absolutepath
  • Default: '/root/.ssh/id_ed25519_borgbackup'
  • Usage: borgbackup::ssh_key_path: '/root/.ssh/id_ed25519_borgbackup'

Path of the SSH private key used to reach the repository.

borgbackup::ssh_private_key

  • Type: Optional[Sensitive[String[1]]]
  • Default: undef, required when enabled is true
  • Usage: borgbackup::ssh_private_key: ENC[PKCS7,...]

SSH private key used to reach the repository, as the complete content of the key file. See SSH keys. The module wraps the value from Hiera in Sensitive.

borgbackup::timer_name

  • Type: String[1]
  • Default: 'borg-backup.timer'
  • Usage: borgbackup::timer_name: 'borg-backup.timer'

Name of the systemd timer, including the .timer suffix.

Managed files

Path Purpose
/etc/borgbackup/borg-env Repository and passphrase
/etc/borgbackup/exclude-patterns.txt Exclude patterns
/etc/borgbackup/known_hosts Pinned host key
/etc/logrotate.d/borgbackup Rotation of the backup log
/etc/profile.d/borg-env.sh Environment for login shells
/etc/systemd/system/borg-backup.service Systemd service
/etc/systemd/system/borg-backup.timer Systemd timer
/root/.ssh/id_ed25519_borgbackup SSH private key
/usr/local/bin/borg-backup.sh Backup script
/usr/local/bin/borg-init-repo.sh Initialises the repository

The SSH private key carries no header, because the format does not allow one.

The scripts write to /var/log/borgbackup.log and /var/log/borgbackup-init.log.

Operations

Run these commands as root on the node. An interactive login shell loads the environment by itself; otherwise load it first:

source /etc/borgbackup/borg-env

Check the state of the backup:

systemctl status borg-backup.timer
journalctl -u borg-backup.service -n 50
tail -50 /var/log/borgbackup.log

Start a backup by hand and follow it:

systemctl start borg-backup.service
journalctl -f -u borg-backup.service

Inspect the repository and its archives:

borg info
borg check
borg list
borg info ::<archive>
borg list ::<archive>

Restore files, into the current directory or to standard output:

borg extract ::<archive> path/to/file
borg extract --stdout ::<archive> path/to/file > /tmp/restored-file

Mount the repository or one archive to browse it, and unmount it again:

borg mount :: /mnt
borg mount ::<archive> /mnt
borg umount /mnt

Upgrading from 3.x

Version 4.0.0 changes the interface of the module:

  • borgbackup::ssh_private_key is required. The key is no longer part of the module.
  • borgbackup::repo_host_key is required. The host key is no longer fetched with ssh-keyscan on the first run.
  • borgbackup::repo_host, borgbackup::repo_user and borgbackup::email_to have no default any more.

Register the new public key on the repository host before the upgrade, next to the old key. Remove the old key there once every node has made a backup with the new key. Versions before 4.0.0 contained the old private key, so it must not be left in place.

The first run after the upgrade replaces every managed file once, reloads systemd and runs the initialisation script, which finds the existing repository and does nothing. Version 4.0.0 also removed the entry that ssh-keyscan had added to /root/.ssh/known_hosts. Since 4.0.1 that is no longer done: a node that skipped 4.0.0 keeps the entry, which is harmless, because Borg only reads the pinned host key.

Removal

Set borgbackup::enabled to false. The module then stops the timer, removes the files it placed and purges the package. The repository on the remote host and the logs in /var/log are left alone.