Skip to content

Commit

Permalink
Merge pull request #9 from hadret/post-pre-commands-support
Browse files Browse the repository at this point in the history
Post & pre commands support
  • Loading branch information
hadret authored Feb 11, 2022
2 parents 1f497a7 + afd1586 commit e915950
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ dist: focal
env:
matrix:
- MOLECULE_DISTRO: ubuntu2004
- MOLECULE_DISTRO: ubuntu1804
- MOLECULE_DISTRO: debian11
- MOLECULE_DISTRO: rockylinux8

install:
- pip install ansible docker molecule-docker
Expand Down
8 changes: 5 additions & 3 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ restic_repos: []
# aws_access_key_id: "AWS_ACCESS_KEY_ID"
# aws_secret_access_key: "AWS_SECRET_ACCESS_KEY"
# jobs:
# - command: 'restic backup /srv'
# - command: 'restic-s3-example backup /srv'
# at: '0 1 * * *'
# pre_command: 'mysqldump --all-databases > /srv/my.sql'
# retention_time: '17 5 * * *'
# retention:
# last: 2
Expand All @@ -36,8 +37,9 @@ restic_repos: []
# b2_account_id: "B2_ACCOUNT_ID"
# b2_account_key: "B2_ACCOUNT_KEY"
# jobs:
# - command: 'restic backup /var'
# - command: 'restic-backblaze-example backup /var'
# at: '0 4 * * *'
# - command: 'restic backup /home'
# post_command: 'systemctl restart some-app'
# - command: 'restic-backblaze-example backup /home'
# at: '0 3 * * *'
# user: 'restic'
11 changes: 7 additions & 4 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ galaxy_info:
author: donat-b, Pawel Krupa (paulfantom), Filip Chabik (hadret)
description: Restic installation for Linux & FreeBSD.
license: MIT
min_ansible_version: 2.7
min_ansible_version: 2.8
platforms:
- name: EL
versions:
- 7
- 8
- name: Debian
versions:
- buster
- bullseye
- name: Ubuntu
versions:
- xenial
- bionic
- focal
- name: FreeBSD
versions:
- 11.4
- 12.1
- 12.2
- 12.3
- 13.0
galaxy_tags:
- restic
Expand Down
1 change: 1 addition & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
become: true
pre_tasks:
- apt: update_cache=yes
when: ansible_os_family == 'Debian'
roles:
- role: ansible-role-restic
8 changes: 4 additions & 4 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

- name: deploy password files
lineinfile:
path: "{{ restic_password_file_path }}/.restic-{{ item.name }}"
path: "{{ restic_password_file_path }}/.restic-{{ item.name | trim | replace(' ', '-') }}"
line: "{{ item.password }}"
mode: "0440"
owner: "root"
Expand All @@ -93,7 +93,7 @@
- name: deploy wrapper scripts
template:
src: "restic-wrapper.sh.j2"
dest: "{{ restic_install_path }}/restic-{{ item.name }}"
dest: "{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }}"
mode: "0770"
owner: "root"
group: "{{ restic_group }}"
Expand All @@ -104,7 +104,7 @@
- name: deploy cron script
template:
src: "restic.cron.j2"
dest: "/etc/cron.d/restic-{{ item.name }}"
dest: "/etc/cron.d/restic-{{ item.name | trim | replace(' ', '-') }}"
mode: "0640"
with_items: "{{ restic_repos }}"
no_log: "{{ restic_ansible_nolog|default(item.no_log | default(true)) }}"
Expand All @@ -113,7 +113,7 @@
- name: deploy prometheus exporter script
template:
src: "restic-exporter.sh.j2"
dest: "{{ restic_install_path }}/restic-{{ item.name }}-exporter"
dest: "{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }}-exporter"
mode: "0770"
owner: "root"
group: "{{ restic_group }}"
Expand Down
4 changes: 2 additions & 2 deletions templates/restic-exporter.sh.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
# {{ ansible_managed }}

{{ restic_install_path }}/restic-{{ item.name | trim | quote }} stats latest --json | /usr/bin/jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' | /usr/bin/sponge {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | quote }}.prom
{{ restic_install_path }}/restic-{{ item.name | trim | quote }} snapshots latest --json | /usr/bin/jq -r 'max_by(.time) | .time | sub("[.][0-9]+"; "") | sub("Z"; "+00:00") | def parseDate(date): date | capture("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.) | "restic_last_snapshot_timestamp \(.)"' | /usr/bin/sponge -a {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | quote }}.prom
{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }} stats latest --json | /usr/bin/jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' | /usr/bin/sponge {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | replace(' ', '-') }}.prom
{{ restic_install_path }}/restic-{{ item.name | trim | replace(' ', '-') }} snapshots latest --json | /usr/bin/jq -r 'max_by(.time) | .time | sub("[.][0-9]+"; "") | sub("Z"; "+00:00") | def parseDate(date): date | capture("(?<no_tz>.*)(?<tz_sgn>[-+])(?<tz_hr>\\d{2}):(?<tz_min>\\d{2})$") | (.no_tz + "Z" | fromdateiso8601) - (.tz_sgn + "60" | tonumber) * ((.tz_hr | tonumber) * 60 + (.tz_min | tonumber)); parseDate(.) | "restic_last_snapshot_timestamp \(.)"' | /usr/bin/sponge -a {{ restic_prometheus_output_path | default('/var/lib/node_exporter/textfile_collector') }}/restic-{{ item.name | trim | replace(' ', '-') }}.prom
2 changes: 1 addition & 1 deletion templates/restic-wrapper.sh.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# {{ ansible_managed }}

export RESTIC_REPOSITORY={{ item.url | trim | quote }}
export RESTIC_PASSWORD_FILE={{ restic_password_file_path }}/.restic-{{ item.name | trim | quote }}
export RESTIC_PASSWORD_FILE={{ restic_password_file_path }}/.restic-{{ item.name | trim | replace(' ', '-') }}
{% if item.remote_credentials is defined %}
{% for k,v in item.remote_credentials.items() %}
export {{ k | upper }}={{ v | trim | quote }}
Expand Down
24 changes: 19 additions & 5 deletions templates/restic.cron.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# jinja2: lstrip_blocks: "True"
#jinja2:lstrip_blocks: True, trim_blocks: True
# vi: ft=jinja.crontab
# {{ ansible_managed }}
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:{{ restic_install_path }}

{% if item.retention is defined -%}
# Apply retention policy
{{ item.retention_time | default('17 3 * * *') }} {{ restic_user }} restic-{{ item.name }} forget --prune
{{ item.retention_time | default('17 3 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }} forget --prune
{%- if item.retention.last is defined %} --keep-last {{ item.retention.last }}{% endif -%}
{% if item.retention.hourly is defined %} --keep-hourly {{ item.retention.hourly }}{% endif -%}
{% if item.retention.daily is defined %} --keep-daily {{ item.retention.daily }}{% endif -%}
Expand All @@ -18,17 +18,31 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:{{ restic_inst

{% if item.check | default(true) -%}
# Check repository
{{ item.check_time | default('17 4 * * *') }} {{ restic_user }} restic-{{ item.name }} check{% if restic_discard_cron_stdout %} > /dev/null{% endif %}
{{ item.check_time | default('17 4 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }} check{% if restic_discard_cron_stdout %} > /dev/null{% endif %}
{%- endif %}

{% if restic_prometheus_exporter | default(false) -%}
# Dump stats for Prometheus
{{ restic_prometheus_time | default('0 0 * * *') }} {{ restic_user }} restic-{{ item.name }}-exporter{% if restic_discard_cron_stdout %} > /dev/null{% endif %}
{{ restic_prometheus_time | default('0 0 * * *') }} {{ restic_user }} restic-{{ item.name | trim | replace(' ', '-') }}-exporter{% if restic_discard_cron_stdout %} > /dev/null{% endif %}

{% endif -%}

# Do an actual backup
{# Iterate through all of the defined item.jobs #}
{% for job in item.jobs %}
{{ job.at }} {{ restic_user }} {{ job.command }}{% if restic_discard_cron_stdout %} > /dev/null{% endif %}
{#- Set job.at and job.user or restic_user and add space in the very end #}
{{ job.at }} {{ job.user | default(restic_user) }}{{ ' ' }}
{#- Setup pre_command or opening parenthesis for post_command #}
{%- if job.pre_command is defined %}({{ job.pre_command }} && {% endif %}
{%- if job.pre_command is not defined and job.post_command is defined %}({% endif %}
{#- Actual backup job #}
{{ job.command }}
{#- Set closing parenthesis for pre_command or setup post_command #}
{%- if job.pre_command is defined and job.post_command is not defined %}){% endif %}
{%- if job.post_command is defined %} && {{ job.post_command }}){% endif %}
{#- Handle discarding of the logs #}
{%- if restic_discard_cron_stdout %} > /dev/null{% endif %}
{#- Handle Prometheus on the job level #}
{#- if job.prometheus is defined and job.prometheus %} {{ item.name | trim | replace(' ', '-') }}{% endif #}

{% endfor %}

0 comments on commit e915950

Please sign in to comment.