Skip to content

Ansible Role linuxfabrik.lfops.duplicity

This role configures daily file-based backups using duplicity. Currently, this role is focused on using OpenStack Object Storage ("Swift") as the storage backend.

Note that this role does not support running with --check, as it first creates a GPG-Key which is required for the rest of the role.

Available since LFOps 1.0.0.

How the Role Behaves

  • The role ships duba, a Linuxfabrik Python wrapper for duplicity. It runs a massively parallel backup to a Swift storage backend, using min(processor count, 6) + 1 duplicity processes. Fewer backups run at the same time if the temp dir (TMPDIR, default /tmp) lacks the space duplicity requires to start each of them (860 MB, 660 MB with duplicity 3.0.0, 460 MB before), and duba logs that it throttled itself. Its configuration file is /etc/duba/duba.json.
  • To start a backup, call duba (or duba --config=/etc/duba/duba.json --command=backup). See duba --help for details.
  • The password of the backend (duplicity__swift_login["password"] or duplicity__sftp_password) is not in /etc/duba/duba.json, but stored as the systemd credential duba-login.json. With systemd 251 and newer (RHEL 9 and newer, Debian 12 and newer, Ubuntu 24.04 and newer), it is encrypted in /etc/credstore.encrypted/ with the host key /var/lib/systemd/credential.secret, which the default backup does not include, and duba.service decrypts it to /run/credentials/duba.service/ while it runs. On RHEL 8 and Ubuntu 22.04, it stays in plain text in /etc/credstore/, readable by root only. Started by hand, duba decrypts the credential itself, so it has to run as root. The password does not show up in the process list.
  • On a host with a TPM2 chip, the encrypted password is also bound to the Secure Boot state. After a firmware or Secure Boot update, duba fails until the role runs again.
  • To restore a file or directory, call duba restore <path> with the path as it was on the host. duba finds the backup that holds it from the backup sources, including the split of sources with divide: true, and restores it below --target (default: a new directory /var/tmp/duba-restore-<random>), never over the live data. The directories duba creates there are accessible by root only, since the restored files keep their owner and mode, but not the protection of their parent directories on the host. --time restores an older state (duplicity time format, e.g. 3D or 2026-10-01), --dry-run only shows which backup holds the path and the duplicity command. A source with divide: true cannot be restored as a whole, restore its entries one by one. duba warns if the target has less free space than the local copy of the path, or if the temp dir (TMPDIR, default /tmp) has less free space than duplicity requires to start a restore (460 MB, 260 MB before duplicity 3.0.1). Then duplicity aborts with exit code 35.
  • To run any other duplicity command (collection-status, list-current-files, verify, restore, ...), use duba exec -- <duplicity arguments>. It runs duplicity with the environment of the backup, including the password, without putting it into your shell. If duplicity__archive_dir is set, duba exec passes it to duplicity, so duplicity uses the local cache of the backup instead of downloading all manifests into a new one in /root/.cache/duplicity. Example: duba exec -- restore --path-to-restore=hosts swift://<container>/etc /tmp/restore/hosts.

Dependent Roles

Any LFOps playbook that installs this role runs these for you. Optional ones can be disabled via the playbook's skip variables.

  • On RHEL-compatible systems, the EPEL repository must be enabled (role: linuxfabrik.lfops.repo_epel). On Rocky 9+, the CRB ("Code Ready Builder") repository must also be enabled (role: linuxfabrik.lfops.repo_baseos) so python3-virtualenv can be installed. On Debian and Ubuntu, no additional repositories are required.
  • duplicity, python-swiftclient and python-keystoneclient must be installed into a Python 3 virtual environment in /opt/python-venv/duplicity (role: linuxfabrik.lfops.python_venv).

Attention

Make sure the virtual environment is not writable by other users to prevent privilege escalation. This is also done by the linuxfabrik.lfops.python_venv role.

Requirements

  • At least 1024 MiB free in /tmp, for the backup volumes duplicity builds there. A host where duba is not installed yet aborts otherwise.

Manual steps:

  • Either configure journald to persist your logs and do the rotating, or use logrotated.

Tags

duplicity

  • Installs and configures duplicity.
  • Triggers: none.

duplicity:configure

  • Deploys the configuration for duplicity, including the duba script.
  • Triggers: none.

duplicity:dump

  • Manages the daily backup schedule (deploys and enables the duba systemd timer).
  • Triggers: none.

duplicity:state

  • Manages the state of the daily systemd timer.
  • Triggers: none.

Mandatory Role Variables

duplicity__gpg_encrypt_master_key

  • The long key ID of the master GPG key. Obtain it using gpg --list-keys --keyid-format=long (after importing the key) or gpg /path/to/keyfile.
  • Type: String.

duplicity__gpg_encrypt_master_key_block

  • The ASCII-armored public master GPG key. Obtain it using gpg --armor --export $GPG_KEY. This key is imported on the server and is used in addition to the server's own local GPG key to encrypt the backups. This means that the backups can be restored using either the master or the server's local private key (which is pretty cool in case of a desaster recovery). Be aware of the empty line between -----BEGIN PGP PUBLIC KEY BLOCK----- and your public key block.
  • Type: String.

duplicity__swift_login

  • The Swift username and password. Usually, this is given by the provider of the Swift Storage.
  • Type: Dictionary.

  • Subkeys:

    • username:

      • Mandatory. The Swift username.
      • Type: String.
    • password:

      • Mandatory. The Swift password.
      • Type: String.

Example:

# mandatory
duplicity__gpg_encrypt_master_key_block: |-
  -----BEGIN PGP PUBLIC KEY BLOCK-----

  6ec3d2aed2a54122817ca02b43a7e340kgKEdlbmVyYXRlZCBieSBBbnNpYmxlLi
  ...
  -----END PGP PUBLIC KEY BLOCK-----
duplicity__gpg_encrypt_master_key: 'LLZGH2BITI2LRLJCLFWEAJQ93N6MWTKBARQDMYX5'
duplicity__swift_login:
  username: 'SBI-MF827483'
  password: 'linuxfabrik'

Optional Role Variables

duplicity__archive_dir

  • The directory for duplicity's local metadata cache (--archive-dir). Duplicity creates one subdirectory per backup URL in it, named after the MD5 hash of the URL. duba passes it to both the backup and the cleanup. Empty means the duplicity default, ~/.cache/duplicity. Use it to keep the cache of a separate duba setup (for example long-term backups to changing targets) apart, so that it can be deleted without touching the cache of the daily backup.
  • Type: String.
  • Default: ''

duplicity__backup_backend

  • The backup backend being used. Possible options: swift, sftp.
  • Type: String.
  • Default: 'swift'

duplicity__backup_dest

  • The backup destination. This will be used in combination with the backup source path to create the target URL for duplicity.
  • Type: String.
  • Default: 'swift://{{ duplicity__backup_dest_container | regex_replace("/$", "") }}'

duplicity__backup_dest_container

  • The Swift container. This can be used to separate backups on the destination. By default, this will be used in duplicity__backup_dest.
  • Type: String.
  • Default: '{{ ansible_nodename }}'

duplicity__backup_full_if_older_than

  • After how long a full backup instead of a incremental one should be done. Time Formats: s, m, h, D, W, M, or Y.
  • Type: String.
  • Default: '30D'

duplicity__backup_retention_time

  • The retention time of the backups. Time Formats: s, m, h, D, W, M, or Y. duba removes a backup chain (a full backup and its incrementals) only once its newest backup is older than this, so with the defaults and one backup a day the oldest backup you can restore is between 31 and 61 days old. duba only cleans up the paths that exist on the host when it runs: the backup chains of a removed directory, of an entry removed below a divide: true source, or of a source set to state: 'absent' stay in the storage until they are deleted by hand there.
  • Type: String.
  • Default: '30D'

duplicity__backup_sources__host_var / duplicity__backup_sources__group_var

  • List of dictionaries with directories to backup. duba skips paths that do not exist on the host, so the default covers the data of every application LFOps deploys. Databases are covered by the dumps their roles write below /backup, not by their data directories, since a copy of the live database files cannot be restored.
  • Type: List of dictionaries.
  • Default:

    • /backup
    • /data (Nextcloud and Moodle data)
    • /etc
    • /home
    • /opt
    • /root
    • /srv (Shiny Server apps)
    • /var/lib/aide (the AIDE database, see the aide role)
    • /var/lib/grafana (dashboards, users, API tokens and alerting in grafana.db)
    • /var/lib/icinga2 (the Icinga2 CA and the node certificates)
    • /var/lib/shiny-server
    • /var/lib/turn (the coturn user database)
    • /var/mail and /var/spool/mail (local mailboxes; one of the two is a symlink, depending on the distribution)
    • /var/named (BIND zone files, journals of dynamic zones, DNSSEC keys)
    • /var/solr/data (Apache Solr cores)
    • /var/spool/cron
    • /var/www (Grav, Moodle, Nextcloud, WordPress), without the repository mirrors in /var/www/html/github-repos and /var/www/html/reposync-repos

    To keep a large directory out of the backup on a host, for example /data on a KVM host storing its VM images there, set it to state: 'absent'.

  • Subkeys:

    • path:

      • Mandatory. Path to the folder to be backed up.
      • Type: String.
    • divide:

      • Optional. Whether to split a large directory at its first level to perform parallel backups. Imagine a computer with 4 processor cores and the folder data containing 100 files and folders. If divide is set to true, duba will start and control 5 duplicate processes at once to speed up the backup process by almost a factor of 5.
      • Type: Bool.
      • Default: false
    • excludes:

      • Optional. List of patterns that should not be included in the backup for this path. With divide: true, each first-level entry gets only the absolute patterns that can match below it, so /data/archive excludes the entry archive without breaking the backups of the other entries.
      • Type: List of strings.
      • Default: []
    • state:

      • Optional. Either present or absent.
      • Type: String.
      • Default: 'present'

duplicity__excludes

  • List of global exclude shell patterns for duplicity. Have a look at man duplicity for details.
  • Type: List of strings.
  • Default: ['**/*.git*', '**/*.svn*', '**/*.temp', '**/*.tmp', '**/.cache', '**/cache', '**/log']

duplicity__loglevel

  • Set the loglevel. Possible options: error, warning, notice, info, debug.
  • Type: String.
  • Default: 'notice'

duplicity__logrotate

  • Log files are rotated count days before being removed or mailed to the address specified in a logrotate mail directive. If count is 0, old versions are removed rather than rotated. If count is -1, old logs are not removed at all (use with caution, may waste performance and disk space).
  • Type: Number.
  • Default: {{ logrotate__rotate | d(14) }}

duplicity__on_calendar

  • The OnCalendar definition for the daily systemd timer. Have a look at man systemd.time(7) for the format.
  • Type: String.
  • Default: '*-*-* {{ duplicity__on_calendar_hour }}:{{ 59 | random(seed=inventory_hostname) }}'

duplicity__on_calendar_hour

  • A shorthand to set the hour of duplicity__on_calendar.
  • Type: String.
  • Default: '23'

duplicity__sftp_password

  • Password for SSH User that is used by SFTP connection.
  • Type: String.
  • Default: unset

duplicity__swift_authurl

  • The Authentication URL for Swift. Usually, this is given by the provider of the Swift Storage.
  • Type: String.
  • Default: 'https://swiss-backup02.infomaniak.com/identity/v3'

duplicity__swift_authversion

  • The Authentication Version for Swift. Usually, this is given by the provider of the Swift Storage.
  • Type: String.
  • Default: '3'

duplicity__swift_tenantname

  • The Swift Tenantname. Usually, this is given by the provider of the Swift Storage.
  • Type: String.
  • Default: 'sb_project_{{ duplicity__swift_login["username"] }}'

duplicity__timer_enabled

  • The state of the daily systemd timer.
  • Type: Bool.
  • Default: true

Example:

# optional
duplicity__archive_dir: ''
duplicity__backup_backend: 'swift'
duplicity__backup_dest: 'swift://{{ duplicity__backup_dest_container | regex_replace("/$", "") }}'
duplicity__backup_dest_container: '{{ ansible_nodename }}'
duplicity__backup_full_if_older_than: '30D'
duplicity__backup_retention_time: '30D'
duplicity__excludes:
  - '**/*.git*'
  - '**/*.svn*'
  - '**/*.temp'
  - '**/*.tmp'
  - '**/.cache'
  - '**/cache'
  - '**/log'
duplicity__backup_sources__group_var: []
duplicity__backup_sources__host_var:
  - path: '/var/www/html'
    divide: false
    excludes:
      - '/var/www/html/nextcloud/data'
    state: 'present'
  - path: '/var/www/html/nextcloud/data'
    divide: true
    state: 'present'
  - path: '/backup'
    state: 'absent'
duplicity__loglevel: 'notice'
duplicity__logrotate: 7
duplicity__on_calendar: '*-*-* {{ duplicity__on_calendar_hour }}:{{ 45 | random(seed=inventory_hostname) }}'
duplicity__on_calendar_hour: '23'
duplicity__sftp_password: 'linuxfabrik'
duplicity__swift_authurl: 'https://swiss-backup02.infomaniak.com/identity/v3'
duplicity__swift_authversion: '3'
duplicity__swift_tenantname: 'sb_project_SBI-MF827483'
duplicity__timer_enabled: true

Troubleshooting

gpg: invalid armor header in stderr during the gpg --import /tmp/public-master-key task

  • Make sure your duplicity__gpg_encrypt_master_key_block is correct and has an empty line after the -----BEGIN PGP PUBLIC KEY BLOCK-----.

License

The Unlicense

Author Information

Linuxfabrik GmbH, Zurich