add restic role
This commit is contained in:
parent
818a7aaefd
commit
95b7f4115c
15
roles/restic/defaults/main.yaml
Normal file
15
roles/restic/defaults/main.yaml
Normal file
@ -0,0 +1,15 @@
|
||||
---
|
||||
restic_service_name: restic.service
|
||||
restic_service_state: started
|
||||
restic_service_enabled: yes
|
||||
|
||||
restic_arch: amd64
|
||||
restic_version: 0.9.6
|
||||
restic_url: "https://github.com/restic/restic/releases/download/v{{ restic_version }}/restic_{{ restic_version }}_linux_{{ restic_arch }}.bz2"
|
||||
restic_checksum: sha256:a88ca09d1dd051d470965667a224a2b81930c6628a0566b7b17868be40207dc8
|
||||
restic_bin_path: /usr/local/bin
|
||||
restic_etc_path: /etc/restic
|
||||
restic_path: "{{ restic_bin_path }}/restic"
|
||||
|
||||
restic_repos: []
|
||||
restic_jobs: []
|
0
roles/restic/tasks/default.yaml
Normal file
0
roles/restic/tasks/default.yaml
Normal file
28
roles/restic/tasks/job.yaml
Normal file
28
roles/restic/tasks/job.yaml
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
- name: create job config directory
|
||||
file:
|
||||
path: "{{ restic_etc_path }}/jobs/{{ item.name }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
state: directory
|
||||
|
||||
- name: create job exclude file
|
||||
template:
|
||||
src: exclude.txt.j2
|
||||
dest: "{{ restic_etc_path }}/jobs/{{ item.name }}/exclude.txt"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
|
||||
- name: create cron
|
||||
cron:
|
||||
name: "restic {{ item.name }} job"
|
||||
hour: "{{ item.cron.hour | default(omit) }}"
|
||||
minute: "{{ item.cron.minute | default(omit) }}"
|
||||
day: "{{ item.cron.day | default(omit) }}"
|
||||
month: "{{ item.cron.month | default(omit) }}"
|
||||
weekday: "{{ item.cron.weekday | default(omit) }}"
|
||||
user: "{{ item.cron.user | default('root') }}"
|
||||
state: "{{ item.cron.state | default('present') }}"
|
||||
job: ". {{ restic_etc_path }}/repos/{{ item.repo }}/env.sh && restic backup -q --exclude-file {{ restic_etc_path }}/jobs/{{ item.name }}/exclude.txt {{ item.paths | join(' ') }}"
|
74
roles/restic/tasks/main.yaml
Normal file
74
roles/restic/tasks/main.yaml
Normal file
@ -0,0 +1,74 @@
|
||||
---
|
||||
- name: gather os specific variables
|
||||
include_vars: "{{ lookup('first_found', possible_files) }}"
|
||||
vars:
|
||||
possible_files:
|
||||
files:
|
||||
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
|
||||
- "{{ ansible_distribution }}.yaml"
|
||||
- "{{ ansible_os_family }}.yaml"
|
||||
- "default.yaml"
|
||||
paths:
|
||||
- vars
|
||||
|
||||
- name: include os specific tasks
|
||||
include_tasks: "{{ lookup('first_found', possible_files) }}"
|
||||
vars:
|
||||
possible_files:
|
||||
files:
|
||||
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
|
||||
- "{{ ansible_distribution }}.yaml"
|
||||
- "{{ ansible_os_family }}.yaml"
|
||||
- "default.yaml"
|
||||
paths:
|
||||
- tasks
|
||||
|
||||
- name: "download restic {{ restic_version }}"
|
||||
get_url:
|
||||
url: "{{ restic_url }}"
|
||||
checksum: "{{ restic_checksum }}"
|
||||
dest: "{{ restic_path }}.bz2"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0400
|
||||
register: dl
|
||||
|
||||
- name: determine if restic exists
|
||||
stat:
|
||||
path: "{{ restic_path }}"
|
||||
register: st
|
||||
|
||||
- name: decompress restic
|
||||
command:
|
||||
cmd: "bunzip2 -k {{ restic_path }}.bz2"
|
||||
creates: "{{ restic_path }}"
|
||||
when: dl.changed or not st.stat.exists
|
||||
#notify:
|
||||
# - restart restic
|
||||
|
||||
- name: manage restic attributes
|
||||
file:
|
||||
path: "{{ restic_path }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
|
||||
- name: create etc tree
|
||||
file:
|
||||
path: "{{ item }}"
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
loop:
|
||||
- "{{ restic_etc_path }}"
|
||||
- "{{ restic_etc_path }}/repos"
|
||||
- "{{ restic_etc_path }}/jobs"
|
||||
|
||||
- name: manage repos
|
||||
include: repo.yaml
|
||||
loop: "{{ restic_repos | default([]) }}"
|
||||
|
||||
- name: manage jobs
|
||||
include: job.yaml
|
||||
loop: "{{ restic_jobs | default([]) }}"
|
37
roles/restic/tasks/repo.yaml
Normal file
37
roles/restic/tasks/repo.yaml
Normal file
@ -0,0 +1,37 @@
|
||||
---
|
||||
- name: get repo status
|
||||
shell:
|
||||
cmd: restic -q -r {{ item.repo }} snapshots 2> /dev/null
|
||||
ignore_errors: yes
|
||||
environment: "{{ item.environment | default({}) }}"
|
||||
register: restic_init
|
||||
|
||||
- name: init repos
|
||||
shell:
|
||||
cmd: restic -q -r {{ item.repo }} init
|
||||
environment: "{{ item.environment | default({}) }}"
|
||||
when: restic_init.rc != 0
|
||||
|
||||
- name: create repo config directory
|
||||
file:
|
||||
path: "{{ restic_etc_path }}/repos/{{ item.name }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
state: directory
|
||||
|
||||
- name: create repo env directory
|
||||
file:
|
||||
path: "{{ restic_etc_path }}/repos/{{ item.name }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0755
|
||||
state: directory
|
||||
|
||||
- name: create repo environment helper
|
||||
template:
|
||||
src: env.sh.j2
|
||||
dest: "{{ restic_etc_path }}/repos/{{ item.name }}/env.sh"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0400
|
6
roles/restic/templates/env.sh.j2
Normal file
6
roles/restic/templates/env.sh.j2
Normal file
@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
export RESTIC_REPOSITORY="{{ item.repo }}"
|
||||
{% for k, v in item.environment.items() %}
|
||||
export {{ k | upper }}="{{ v }}"
|
||||
{% endfor %}
|
3
roles/restic/templates/exclude.txt.j2
Normal file
3
roles/restic/templates/exclude.txt.j2
Normal file
@ -0,0 +1,3 @@
|
||||
{% for exclude in item.exclude %}
|
||||
{{ exclude }}
|
||||
{% endfor %}
|
0
roles/restic/vars/default.yaml
Normal file
0
roles/restic/vars/default.yaml
Normal file
Loading…
Reference in New Issue
Block a user