mirror of
https://github.com/roles-ansible/ansible_role_restic_archiver.git
synced 2024-08-16 10:09:49 +02:00
Adds backup script to execute manual Backup
This commit is contained in:
parent
fc5ef9190e
commit
6721b67d46
3 changed files with 83 additions and 1 deletions
10
README.md
10
README.md
|
@ -80,12 +80,20 @@ Available variables:
|
||||||
| Name | Required | Description |
|
| Name | Required | Description |
|
||||||
| ---------------- |:-----------------------------:| ----------------------------------------------------------------------------------------------------------------------- |
|
| ---------------- |:-----------------------------:| ----------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `name` | yes | The name of this backup. Used together with pruning and needs to be unique. |
|
| `name` | yes | The name of this backup. Used together with pruning and needs to be unique. |
|
||||||
|
| `repo` | yes | The name of the repository to backup to. |
|
||||||
| `src` | yes | The source directory or file |
|
| `src` | yes | The source directory or file |
|
||||||
| `stdin` | no | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? |
|
| `stdin` | no | Is this backup created from a [stdin](https://restic.readthedocs.io/en/stable/040_backup.html#reading-data-from-stdin)? |
|
||||||
| `stdin_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. |
|
| `stdin_cmd` | no (yes if `stdin` == `true`) | The command to produce the stdin. |
|
||||||
| `stdin_filename` | no | The filename used in the repository. |
|
| `stdin_filename` | no | The filename used in the repository. |
|
||||||
| `tags` | no | Array of default tags |
|
| `tags` | no | Array of default tags |
|
||||||
| `keep-last` | no | If set, only keeps the last n snapshots. |
|
| `keep_last` | no | If set, only keeps the last n snapshots. |
|
||||||
|
| `keep_hourly` | no | If set, only keeps the last n hourly snapshots. |
|
||||||
|
| `keep_daily` | no | If set, only keeps the last n daily snapshots. |
|
||||||
|
| `keep_weekly ` | no | If set, only keeps the last n weekly snapshots. |
|
||||||
|
| `keep_monthly` | no | If set, only keeps the last n monthly snapshots. |
|
||||||
|
| `keep_yearly ` | no | If set, only keeps the last n yearly snapshots. |
|
||||||
|
| `keep_within` | no | If set, only keeps snapshots in this time period. |
|
||||||
|
| `keep_tag` | no | If set, keep snapshots with this tags. |
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
none
|
none
|
||||||
|
|
|
@ -14,3 +14,17 @@
|
||||||
when:
|
when:
|
||||||
- item.value.init is defined
|
- item.value.init is defined
|
||||||
- item.value.init == true
|
- item.value.init == true
|
||||||
|
|
||||||
|
- name: Create backup script
|
||||||
|
template:
|
||||||
|
src: restic_script.j2
|
||||||
|
dest: '{{ restic_script_dir }}/backup-{{ item.name }}.sh'
|
||||||
|
mode: '0700'
|
||||||
|
owner: '{{ restic_dir_owner }}'
|
||||||
|
group: '{{ restic_dir_group }}'
|
||||||
|
no_log: True
|
||||||
|
with_items: '{{ restic_backups }}'
|
||||||
|
when:
|
||||||
|
- item.name is defined
|
||||||
|
- item.src is defined or item.stdin is defined and item.stdin == true and item.stdin_cmd is defined
|
||||||
|
- item.repo in restic_repos
|
60
templates/restic_script.j2
Normal file
60
templates/restic_script.j2
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
# {{ ansible_managed }}
|
||||||
|
# Backup script for {{ item.src|default('stdin') }}
|
||||||
|
# Use this file to create a Backup and prune existing data.
|
||||||
|
set -euxo pipefail
|
||||||
|
|
||||||
|
export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }}
|
||||||
|
export RESTIC_PASSWORD={{ restic_repos[item.repo].password }}
|
||||||
|
BACKUP_NAME={{ item.name }}
|
||||||
|
{% if item.src is defined %}
|
||||||
|
BACKUP_SOURCE={{ item.src }}
|
||||||
|
{% endif %}
|
||||||
|
{#
|
||||||
|
Define Tags
|
||||||
|
#}
|
||||||
|
{% macro tags(tags) -%}
|
||||||
|
{% if tags is defined %}{% for tag in tags %} --tag {{ tag }}{% endfor %}{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
{#
|
||||||
|
Define Hostname
|
||||||
|
#}
|
||||||
|
{% macro hostname(h) -%}
|
||||||
|
{% if h is defined %} --hostname {{ h }}{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
{#
|
||||||
|
Define stdin filename
|
||||||
|
#}
|
||||||
|
{% macro stdin_filename(n) -%}
|
||||||
|
{% if n is defined %} --stdin-filename {{ n }}{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
{#
|
||||||
|
Define path
|
||||||
|
#}
|
||||||
|
{% macro path(repo) -%}
|
||||||
|
{% if repo.src is defined %}{{ repo.src }}{% else %}$HOME/{{ repo.stdin_filename }}{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
{#
|
||||||
|
Define retention pattern
|
||||||
|
#}
|
||||||
|
{% macro retention_pattern(repo) -%}
|
||||||
|
{% if repo.keep_last is defined %}--keep-last {{ item.keep_last }}{% endif %} \
|
||||||
|
{% if repo.keep_hourly is defined %}--keep-hourly {{ item.keep_hourly }}{% endif %} \
|
||||||
|
{% if repo.keep_daily is defined %}--keep-daily {{ item.keep_daily }}{% endif %} \
|
||||||
|
{% if repo.keep_weekly is defined %}--keep-weekly {{ item.keep_weekly }}{% endif %} \
|
||||||
|
{% if repo.keep_monthly is defined %}--keep-monthly {{ item.keep_monthly }}{% endif %} \
|
||||||
|
{% if repo.keep_yearly is defined %}--keep-yearly {{ item.keep_yearly }}{% endif %} \
|
||||||
|
{% if repo.keep_within is defined %}--keep-within {{ item.keep_within }}{% endif %} \
|
||||||
|
{% if repo.keep_tag is defined %}--keep-tag {{ item.keep_tag }}{% endif %}
|
||||||
|
{%- endmacro %}
|
||||||
|
{#
|
||||||
|
Define backup commands
|
||||||
|
#}
|
||||||
|
{% if item.stdin is defined and item.stdin == true %}
|
||||||
|
{{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup --stdin --tag manual {{ tags(item.tags) }} {{ stdin_filename(item.stdin_filename) }} $@
|
||||||
|
{% else %}
|
||||||
|
{{ restic_install_path }}/restic backup $BACKUP_SOURCE --tag manual {{ tags(item.tags) }} $@
|
||||||
|
{% endif %}
|
||||||
|
{#
|
||||||
|
Define stdin forget commands
|
||||||
|
#}
|
||||||
|
{{ restic_install_path }}/restic forget --prune --tag manual --path {{ path(item) }} {{ retention_pattern(item) }}
|
Loading…
Reference in a new issue