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, usingmin(processor count, 6) + 1duplicity 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), anddubalogs that it throttled itself. Its configuration file is/etc/duba/duba.json. - To start a backup, call
duba(orduba --config=/etc/duba/duba.json --command=backup). Seeduba --helpfor details. - The password of the backend (
duplicity__swift_login["password"]orduplicity__sftp_password) is not in/etc/duba/duba.json, but stored as the systemd credentialduba-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, andduba.servicedecrypts 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,dubadecrypts 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,
dubafails until the role runs again. - To restore a file or directory, call
duba restore <path>with the path as it was on the host.dubafinds the backup that holds it from the backup sources, including the split of sources withdivide: true, and restores it below--target(default: a new directory/var/tmp/duba-restore-<random>), never over the live data. The directoriesdubacreates 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.--timerestores an older state (duplicity time format, e.g.3Dor2026-10-01),--dry-runonly shows which backup holds the path and the duplicity command. A source withdivide: truecannot be restored as a whole, restore its entries one by one.dubawarns 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, ...), useduba exec -- <duplicity arguments>. It runs duplicity with the environment of the backup, including the password, without putting it into your shell. Ifduplicity__archive_diris set,duba execpasses 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-virtualenvcan be installed. On Debian and Ubuntu, no additional repositories are required. duplicity,python-swiftclientandpython-keystoneclientmust 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 wheredubais 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
dubascript. - Triggers: none.
duplicity:dump
- Manages the daily backup schedule (deploys and enables the
dubasystemd 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) orgpg /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.dubapasses it to both the backup and the cleanup. Empty means the duplicity default,~/.cache/duplicity. Use it to keep the cache of a separatedubasetup (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, orY. - Type: String.
- Default:
'30D'
duplicity__backup_retention_time
- The retention time of the backups. Time Formats:
s,m,h,D,W,M, orY.dubaremoves 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.dubaonly cleans up the paths that exist on the host when it runs: the backup chains of a removed directory, of an entry removed below adivide: truesource, or of a source set tostate: '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.
dubaskips 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 ingrafana.db)/var/lib/icinga2(the Icinga2 CA and the node certificates)/var/lib/shiny-server/var/lib/turn(the coturn user database)/var/mailand/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-reposand/var/www/html/reposync-repos
To keep a large directory out of the backup on a host, for example
/dataon a KVM host storing its VM images there, set it tostate: '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
datacontaining 100 files and folders. Ifdivideis set totrue,dubawill start and control 5 duplicate processes at once to speed up the backup process by almost a factor of 5. - Type: Bool.
- Default:
false
- 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
-
excludes:- Optional. List of patterns that should not be included in the backup for this
path. Withdivide: true, each first-level entry gets only the absolute patterns that can match below it, so/data/archiveexcludes the entryarchivewithout breaking the backups of the other entries. - Type: List of strings.
- Default:
[]
- Optional. List of patterns that should not be included in the backup for this
-
state:- Optional. Either
presentorabsent. - Type: String.
- Default:
'present'
- Optional. Either
-
duplicity__excludes
- List of global exclude shell patterns for
duplicity. Have a look atman duplicityfor 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
countdays before being removed or mailed to the address specified in alogrotatemail directive. If count is0, 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
OnCalendardefinition for the daily systemd timer. Have a look atman 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_blockis correct and has an empty line after the-----BEGIN PGP PUBLIC KEY BLOCK-----.