1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_restic_archiver.git synced 2024-08-16 10:09:49 +02:00

Merge pull request #15 from roles-ansible/archive

update linting, adding pidfile and improve pruning
This commit is contained in:
L3D 2022-03-27 22:43:21 +02:00 committed by GitHub
commit 61b41f9651
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 14 deletions

View file

@ -17,10 +17,6 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Lint Ansible Playbook - name: Lint Ansible Playbook
uses: ansible/ansible-lint-action@master uses: ansible/ansible-lint-action@v6
with: with:
targets: "." targets: "."
# [required]
# Paths to ansible files (i.e., playbooks, tasks, handlers etc..)
args: ""
# [optional]

View file

@ -15,6 +15,6 @@ jobs:
- name: checkout - name: checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: galaxy - name: galaxy
uses: robertdebock/galaxy-action@1.1.1 uses: robertdebock/galaxy-action@1.1.0
with: with:
galaxy_api_key: ${{ secrets.galaxy_api_key }} galaxy_api_key: ${{ secrets.galaxy_api_key }}

View file

@ -28,6 +28,14 @@ restic_archiver__keep_weekly: 8
restic_archiver__keep_monthly: 16 restic_archiver__keep_monthly: 16
restic_archiver__keep_yearly: 12 restic_archiver__keep_yearly: 12
restic_archiver__archive_keep: 5
restic_archiver__archive_keep_hourly: 16
restic_archiver__archive_keep_daily: 14
restic_archiver__archive_keep_weekly: 8
restic_archiver__archive_keep_monthly: 16
restic_archiver__archive_keep_yearly: 12
# owner and user of all restic stuff # owner and user of all restic stuff
restic_archiver__owner: 'root' restic_archiver__owner: 'root'
restic_archiver__group: 'root' restic_archiver__group: 'root'

View file

@ -2,13 +2,13 @@
# tasks file for skeleton # tasks file for skeleton
- name: reformat dict if necessary - name: reformat dict if necessary
set_fact: ansible.builtin.set_fact:
restic_archiver__backups: "{{ restic_archiver__backups|dict2items|json_query('[*].value') }}" restic_archiver__backups: "{{ restic_archiver__backups|dict2items|json_query('[*].value') }}"
when: when:
- restic_archiver__backups | type_debug == "dict" - restic_archiver__backups | type_debug == "dict"
- name: Create backup credentials - name: Create backup credentials
template: ansible.builtin.template:
src: restic_access_Linux.j2 src: restic_access_Linux.j2
dest: '{{ restic_script_dir }}/access-{{ item.name }}.sh' dest: '{{ restic_script_dir }}/access-{{ item.name }}.sh'
mode: '0700' mode: '0700'
@ -23,7 +23,7 @@
- item.repo in restic_repos - item.repo in restic_repos
- name: Create backup script - name: Create backup script
template: ansible.builtin.template:
src: restic_script_Linux.j2 src: restic_script_Linux.j2
dest: '{{ restic_script_dir }}/backup-{{ item.name }}.sh' dest: '{{ restic_script_dir }}/backup-{{ item.name }}.sh'
mode: '0700' mode: '0700'
@ -38,7 +38,7 @@
- item.repo in restic_repos - item.repo in restic_repos
- name: Setup CRON jobs - name: Setup CRON jobs
cron: ansible.builtin.cron:
name: 'do1jlr.restic_archiver {{ item.name }}' name: 'do1jlr.restic_archiver {{ item.name }}'
job: 'CRON=true {{ restic_script_dir }}/backup-{{ item.name }}.sh' job: 'CRON=true {{ restic_script_dir }}/backup-{{ item.name }}.sh'
minute: '{{ item.schedule_minute | default("*") }}' minute: '{{ item.schedule_minute | default("*") }}'

View file

@ -41,6 +41,44 @@
--prune {% endif %} --prune {% endif %}
{%- endmacro %} {%- endmacro %}
{% macro retention_archive_pattern(repo) -%}
{% if repo.keep_last is defined and repo.archive_keep_last != None -%}
--keep-last {{ repo.archive_keep_last }}
{%- else -%}
--keep-last {{ restic_archiver__archive_keep }}
{%- endif %} \
{% if repo.keep_hourly is defined and repo.archive_keep_hourly != None -%}
--keep-hourly {{ repo.archive_keep_hourly }}
{%- else -%}
--keep-hourly {{ restic_archiver__archive_keep_hourly }}
{%- endif %} \
{% if repo.keep_daily is defined and repo.archive_keep_daily != None -%}
--keep-daily {{ repo.archive_keep_daily }}
{%- else -%}
--keep-daily {{ restic_archiver__archive_keep_daily }}
{%- endif %} \
{% if repo.keep_weekly is defined and repo.archive_keep_weekly != None -%}
--keep-weekly {{ repo.archive_keep_weekly }}
{%- else -%}
--keep-weekly {{ restic_archiver__archive_keep_weekly }}
{%- endif %} \
{% if repo.keep_monthly is defined and repo.archive_keep_monthly != None -%}
--keep-monthly {{ repo.archive_keep_monthly }}
{%- else -%}
--keep-monthly {{ restic_archiver__archive_keep_monthly }}
{%- endif %} \
{% if repo.keep_yearly is defined and repo.archive_keep_yearly != None -%}
--keep-yearly {{ repo.archive_keep_yearly }}
{%- else -%}
--keep-yearly {{ restic_archiver__archive_keep_yearly }}
{%- endif -%}
{% if repo.keep_within is defined and repo.archive_keep_within != None %} \
--keep-within {{ repo.archive_keep_within }} {% endif -%}
{%- if repo.prune|default(restic_archiver__prune) %} \
--prune {% endif %}
{%- endmacro %}
{# {#
START DOING STUFF INSIDE THE RESTIC REPO FOR LOOP START DOING STUFF INSIDE THE RESTIC REPO FOR LOOP
#} #}
@ -65,7 +103,7 @@ START DOING STUFF INSIDE THE RESTIC REPO FOR LOOP
echo "init $BACKUP_NAME" echo "init $BACKUP_NAME"
restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }} restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }}
echo "restic forget done" echo "restic forget done"
{%- if repo.prune|default(restic_archiver__prune) %} {% if repo.prune|default(restic_archiver__prune) %}
restic {{ restic_archiver__default_opt }} prune restic {{ restic_archiver__default_opt }} prune
echo "restic prune done" echo "restic prune done"
{% endif %} {% endif %}
@ -137,8 +175,8 @@ EOT
export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
BACKUP_NAME="{{ repo.name }}_archive" BACKUP_NAME="{{ repo.name }}_archive"
restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }} restic {{ restic_archiver__default_opt }} forget {{ retention_archive_pattern(repo) }}
{%- if repo.prune|default(restic_archiver__prune) %} {% if repo.prune|default(restic_archiver__prune) %}
restic {{ restic_archiver__default_opt }} prune restic {{ restic_archiver__default_opt }} prune
echo "restic ARCHIVE prune done" echo "restic ARCHIVE prune done"
{% endif %} {% endif %}

View file

@ -2,6 +2,21 @@
# {{ ansible_managed }} # {{ ansible_managed }}
# This file is to cleanup your backup archive and move some snapshots to a external storage. # This file is to cleanup your backup archive and move some snapshots to a external storage.
pid="/var/run/restic_archiver.pid"
trap "rm -f $pid" SIGSEGV
trap "rm -f $pid" SIGINT
if [ -e $pid ]; then
{% if restic_archiver__mail_on_error | bool %}
mail -s "[ERROR] running restic backup" {{ restic_archiver__mailaddress }} <<< 'Another instance of our huge backup script is already running. We canceled running it twice at {{ ansible_hostname }}'
{% endif %}
echo "Another version of this script is already running. CANCELING"
exit # pid file exists, another instance is running, so now we politely exit
else
echo $$ > $pid # pid file doesn't exit, create one and go on
fi
{% if restic_archiver__cache_config | bool %} {% if restic_archiver__cache_config | bool %}
{% include 'includes/restic_cache.sh.j2' %} {% include 'includes/restic_cache.sh.j2' %}
{% endif %} {% endif %}
@ -33,4 +48,7 @@ sync
{% if restic_archiver__umount_after_usage %} {% if restic_archiver__umount_after_usage %}
umount {{ restic_archiver__mount_disk }} umount {{ restic_archiver__mount_disk }}
{% endif %} {% endif %}
rm -f $pid # remove pid file just before exiting
echo "restic archiver done" echo "restic archiver done"
exit

View file

@ -1,3 +1,3 @@
--- ---
playbook_version_number: 50 # should be int playbook_version_number: 54 # should be int
playbook_version_path: 'role-restic_archiver_roles-ansible_github.com.version' playbook_version_path: 'role-restic_archiver_roles-ansible_github.com.version'