Restic Ansible role
https://restic.net/
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .forgejo/workflows | ||
| defaults | ||
| meta | ||
| tasks | ||
| .gitignore | ||
| CHANGELOG.md | ||
| LICENSE | ||
| README.md | ||
Restic
Installs restic and configures scheduled backups via cron to local or SFTP repositories.
Requirements
- Debian/Ubuntu host
become: truein the calling playbook (installs packages, writes to/root/and/etc/)
Variables
| Variable | Default | Description |
|---|---|---|
restic_repos |
[] |
List of repository definitions. See Repository structure. |
restic_cron |
0 3 * * * |
Cron schedule for backup jobs. |
restic_cron_cleanup |
0 4 * * 0 |
Cron schedule for restic forget --prune. |
restic_keep_hourly |
0 |
Number of hourly snapshots to keep (0 = not passed). |
restic_keep_daily |
7 |
Number of daily snapshots to keep. |
restic_keep_weekly |
4 |
Number of weekly snapshots to keep. |
restic_keep_monthly |
12 |
Number of monthly snapshots to keep. |
restic_shell_env |
false |
/etc/profile.d/restic.sh: repo env of the first repo (plain restic snapshots works as root in a login shell) and a restic-<name> alias per repo. |
restic_uninstall |
false |
Removes all files written by this role. |
restic_purge |
false |
Also removes packages, logs, and local repo data. Requires restic_uninstall: true. Irreversible. |
Repository structure
Each entry in restic_repos supports the following fields:
| Field | Required | Description |
|---|---|---|
name |
Yes | Unique identifier. Used in cron filenames and password file paths. |
password |
Yes | Restic encryption password. Generate with openssl rand -hex 32. |
target |
Yes | Full restic repository path. Use sftp:<alias>:/path for SFTP or an absolute path for local repos. |
sources |
* | List of paths to back up. *At least one of sources/commands. |
commands |
* | List of {name, command, only_if?}: stdout of command saved as file name (--stdin-from-command, restic >= 0.17); a failing command leaves no snapshot; only_if = shell test that must pass first. |
host |
No | Fixed snapshot host name (--host) for backup and forget. |
retry_lock |
No | Wait this long for a locked repo (--retry-lock, restic >= 0.16), for repos shared by hosts. |
sftp_config |
No | SFTP connection settings. Omit entirely for local repositories. |
sftp_config.alias |
SFTP | SSH config Host alias. Must match the alias used in target. |
sftp_config.hostname |
SFTP | Real hostname for the SSH connection. |
sftp_config.username |
SFTP | SSH username. |
sftp_config.ssh_key |
No | SSH private key content. Omit to use an existing key or agent. |
sftp_config.port |
No | SSH port. Default 23. |
Usage
SFTP backup:
restic_repos:
- name: offsite
password: "{{ vault_restic_password }}"
target: sftp:myhost:/backups/offsite
sources:
- /docker
- /data
sftp_config:
alias: myhost
hostname: backup.example.com
username: backupuser
ssh_key: "{{ vault_restic_ssh_key }}"
Local backup:
restic_repos:
- name: local
password: "{{ vault_restic_password }}"
target: /mnt/backup/restic
sources:
- /data
Multiple repos with custom retention:
restic_repos:
- name: offsite
password: "{{ vault_restic_offsite_password }}"
target: sftp:myhost:/backups/offsite
sources:
- /docker
sftp_config:
alias: myhost
hostname: backup.example.com
username: backupuser
- name: local
password: "{{ vault_restic_local_password }}"
target: /mnt/backup/restic
sources:
- /docker
- /data
restic_keep_daily: 14
restic_keep_weekly: 8
restic_keep_monthly: 24
Uninstall
To remove all files written by this role (password files, cron jobs, SSH keys, SSH config entries):
restic_uninstall: true
To also remove the restic and cron packages, the log directory, and local repository data (irreversible):
restic_uninstall: true
restic_purge: true
Notes
- Password files are written to
/root/.restic-password-<name>with mode0600. - Backup logs land at
/var/log/restic/<name>_<sanitized_source>.log. - SFTP repository data is never removed by this role, even with
restic_purge: true.