From 6a73e4120e7d0654b05b82cd819c57e9b57c46e3 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Tue, 8 Jun 2021 00:33:55 -0400 Subject: [PATCH] Email rsnapshot backup reports --- dev/host_vars/rsnapshot.yml | 10 ++++++++++ roles/base/tasks/mail.yml | 19 ++++++++++++++++++ roles/base/tasks/main.yml | 4 ++++ roles/base/templates/msmtprc.j2 | 17 ++++++++++++++++ roles/rsnapshot/defaults/main.yml | 1 + roles/rsnapshot/tasks/main.yml | 20 ++++++++++++++++++- .../templates/rsnapshot-report.sh.j2 | 4 ++++ 7 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 roles/base/tasks/mail.yml create mode 100644 roles/base/templates/msmtprc.j2 create mode 100644 roles/rsnapshot/templates/rsnapshot-report.sh.j2 diff --git a/dev/host_vars/rsnapshot.yml b/dev/host_vars/rsnapshot.yml index 5317ecb..89fc4a5 100644 --- a/dev/host_vars/rsnapshot.yml +++ b/dev/host_vars/rsnapshot.yml @@ -2,6 +2,13 @@ allow_reboot: false manage_network: false +#mail: +# host: smtp.gmail.com +# from: automated@example.com +# user: automated +# password: password123 +# rootalias: personal@example.com + # rsnapshot rsnapshot: - name: localhost @@ -18,6 +25,9 @@ rsnapshot: minute: "1-59/2" # every odd minute #minute: 0 #hour: 0 + #report: + # to: personal@example.com + # subject: Localhost Backup Report backups: - name: config source: /etc/ diff --git a/roles/base/tasks/mail.yml b/roles/base/tasks/mail.yml new file mode 100644 index 0000000..57c3c16 --- /dev/null +++ b/roles/base/tasks/mail.yml @@ -0,0 +1,19 @@ +- name: Install msmtp + apt: + name: "{{ item }}" + state: present + loop: + - msmtp + - msmtp-mta + - mailutils + +- name: Install msmtp configuration + template: + src: msmtprc.j2 + dest: /root/.msmtprc + mode: 0700 + +- name: Install /etc/aliases + copy: + dest: /etc/aliases + content: "root: {{ mail.rootalias }}" diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml index 1df652e..f1cc1f5 100644 --- a/roles/base/tasks/main.yml +++ b/roles/base/tasks/main.yml @@ -8,6 +8,10 @@ tags: network when: manage_network +- import_tasks: mail.yml + tags: mail + when: mail is defined + - import_tasks: ddclient.yml tags: ddclient when: ddclient is defined diff --git a/roles/base/templates/msmtprc.j2 b/roles/base/templates/msmtprc.j2 new file mode 100644 index 0000000..f31332a --- /dev/null +++ b/roles/base/templates/msmtprc.j2 @@ -0,0 +1,17 @@ +# Set default values for all accounts. +defaults +port 587 +tls on +tls_trust_file /etc/ssl/certs/ca-certificates.crt +aliases /etc/aliases +logfile ~/.msmtp.log + +account main +host {{ mail.host }} +from {{ mail.from }} +auth on +user {{ mail.user }} +password {{ mail.password }} + +# Set a default account +account default : main diff --git a/roles/rsnapshot/defaults/main.yml b/roles/rsnapshot/defaults/main.yml index d31cb77..f972bc5 100644 --- a/roles/rsnapshot/defaults/main.yml +++ b/roles/rsnapshot/defaults/main.yml @@ -1,3 +1,4 @@ rsnapshot_confdir: /etc/rsnapshot.d rsnapshot_logdir: /var/log/rsnapshot rsnapshot_root: /srv/backups/{{ item.name }} +rsnapshot_reportlog: "{{ rsnapshot_logdir }}/{{ item.name }}.log" diff --git a/roles/rsnapshot/tasks/main.yml b/roles/rsnapshot/tasks/main.yml index f8dbd7a..63de8a8 100644 --- a/roles/rsnapshot/tasks/main.yml +++ b/roles/rsnapshot/tasks/main.yml @@ -40,7 +40,7 @@ - name: Install rsnapshot crons cron: name: "{{ item.1.interval }} rsnapshot of {{ item.0.name }}" - job: "/usr/bin/rsnapshot -c {{ rsnapshot_confdir }}/{{ item.0.name }}.conf {{ item.1.interval }}" + job: "/usr/bin/rsnapshot -c {{ rsnapshot_confdir }}/{{ item.0.name }}.conf {{ item.1.interval }} >/dev/null" user: "root" minute: "{{ item.1.minute | default('*') }}" hour: "{{ item.1.hour | default('*') }}" @@ -51,3 +51,21 @@ with_subelements: - "{{ rsnapshot }}" - cron + +- name: Install rsnapshot report script + template: + src: rsnapshot-report.sh.j2 + dest: /usr/local/bin/rsnapshot-report + mode: '0750' + +- name: Install rsnapshot report crons + cron: + name: "{{ item.name }} rsnapshot report email" + job: "/usr/local/bin/rsnapshot-report {{ rsnapshot_reportlog }} + | mail -s '{{ item.report.subject | default('Backup Report') }}' {{ item.report.to }}" + user: "root" + minute: '0' + hour: '0' + cron_file: "rsnapshot-{{ item.name }}" + loop: "{{ rsnapshot }}" + when: item.report is defined diff --git a/roles/rsnapshot/templates/rsnapshot-report.sh.j2 b/roles/rsnapshot/templates/rsnapshot-report.sh.j2 new file mode 100644 index 0000000..933ab08 --- /dev/null +++ b/roles/rsnapshot/templates/rsnapshot-report.sh.j2 @@ -0,0 +1,4 @@ +#/bin/bash +LOG_LOCATION={{ rsnapshot_logdir }}/*.log +[ $# -gt 0 ] && LOG_LOCATION=$1 +grep -e ^\\[$(date --date="yesterday" '+%F').*\/usr\/bin\/rsnapshot $LOG_LOCATION