From 6721b67d46fedd06cadbbf1213012fb764d19d2b Mon Sep 17 00:00:00 2001 From: Matthias Leutenegger Date: Wed, 14 Aug 2019 16:00:22 +0200 Subject: [PATCH] Adds backup script to execute manual Backup --- README.md | 10 ++++++- tasks/configure.yml | 14 +++++++++ templates/restic_script.j2 | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 templates/restic_script.j2 diff --git a/README.md b/README.md index c6a2a59..ca92489 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,20 @@ Available variables: | Name | Required | Description | | ---------------- |:-----------------------------:| ----------------------------------------------------------------------------------------------------------------------- | | `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 | | `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_filename` | no | The filename used in the repository. | | `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 none diff --git a/tasks/configure.yml b/tasks/configure.yml index 7a70dac..eb71d6e 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -14,3 +14,17 @@ when: - item.value.init is defined - 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 \ No newline at end of file diff --git a/templates/restic_script.j2 b/templates/restic_script.j2 new file mode 100644 index 0000000..fca2410 --- /dev/null +++ b/templates/restic_script.j2 @@ -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) }}