From 80baa38a8a5029f852c77d9546b17b50d25478a9 Mon Sep 17 00:00:00 2001 From: L3D Date: Mon, 14 Dec 2020 15:50:39 +0100 Subject: [PATCH] implement logrotate --- README.md | 3 +++ defaults/main.yml | 3 +++ files/logrotate.conf | 8 ++++++++ tasks/cron.yml | 28 ++++++++++++++++++++++++++ tasks/logs.yml | 27 +++++++++++++++++++++++++ tasks/main.yml | 17 +++------------- tasks/template.yml | 48 -------------------------------------------- vars/main.yml | 2 +- 8 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 files/logrotate.conf create mode 100644 tasks/cron.yml create mode 100644 tasks/logs.yml delete mode 100644 tasks/template.yml diff --git a/README.md b/README.md index 20cc665..99f6360 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,9 @@ restic_archiver__umount_after_usage: false restic_archiver__package: - cron +restic_archiver__log_output: true +restic_archiver__logrotate: true + # version check for this playbook (true is recomended) submodules_versioncheck: false ``` diff --git a/defaults/main.yml b/defaults/main.yml index 5d2764c..4cb447d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -46,5 +46,8 @@ restic_archiver__umount_after_usage: false restic_archiver__package: - cron +restic_archiver__log_output: true +restic_archiver__logrotate: true + # version check for this playbook (true is recomended) submodules_versioncheck: false diff --git a/files/logrotate.conf b/files/logrotate.conf new file mode 100644 index 0000000..47e7ace --- /dev/null +++ b/files/logrotate.conf @@ -0,0 +1,8 @@ +/var/log/restic/restic_archiver.log { + rotate 182 + daily + compress + delaycompress + missingok + notifempty +} diff --git a/tasks/cron.yml b/tasks/cron.yml new file mode 100644 index 0000000..b036558 --- /dev/null +++ b/tasks/cron.yml @@ -0,0 +1,28 @@ +--- +- name: install cron + become: true + package: + name: "{{ restic_archiver__package }}" + state: present + +- name: setup cronjob for restic with logging + become: true + cron: + name: "archive restic backup with ansible_role_restic_archiver >> /var/log/restic/restic_archiver.log" + job: /opt/restic-backup.sh + cron_file: /etc/crontab + hour: "{{ restic_archiver__hour }}" + minute: "{{ restic_archiver__minute }}" + user: "{{ restic_archiver__owner }}" + when: restic_archiver__log_output + +- name: setup cronjob for restic without logging + become: true + cron: + name: "archive restic backup with ansible_role_restic_archiver" + job: /opt/restic-backup.sh + cron_file: /etc/crontab + hour: "{{ restic_archiver__hour }}" + minute: "{{ restic_archiver__minute }}" + user: "{{ restic_archiver__owner }}" + when: not restic_archiver__log_output diff --git a/tasks/logs.yml b/tasks/logs.yml new file mode 100644 index 0000000..9416a6d --- /dev/null +++ b/tasks/logs.yml @@ -0,0 +1,27 @@ +--- +- name: create /var/log/restic/ + become: true + file: + path: /var/log/restic + state: directory + recurse: true + group: "{{ restic_archiver__owner }}" + owner: "{{ restic_archiver__group }}" + mode: 0750 + +- name: install logrotate + become: true + package: + name: logrotate + state: present + when: restic_archiver__logrotate | bool + +- name: create logrotate config + become: true + copy: + src: files/logrotate.conf + dest: /etc/logrotate.d/restic + group: "{{ restic_archiver__owner }}" + owner: "{{ restic_archiver__group }}" + mode: 0644 + when: restic_archiver__logrotate | bool diff --git a/tasks/main.yml b/tasks/main.yml index e68fbac..a22e761 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,18 +11,7 @@ mode: 0700 become: true -- name: install cron - become: true - package: - name: "{{ restic_archiver__package }}" - state: present +- include_tasks: logs.yml + when: restic_archiver__log_output | bool -- name: setup cronjob for restic - become: true - cron: - name: "archive restic backup with ansible_role_restic_archiver" - job: /opt/restic-backup.sh - cron_file: /etc/crontab - hour: "{{ restic_archiver__hour }}" - minute: "{{ restic_archiver__minute }}" - user: "{{ restic_archiver__owner }}" +- include_tasks: cron.yml diff --git a/tasks/template.yml b/tasks/template.yml deleted file mode 100644 index 3e4d706..0000000 --- a/tasks/template.yml +++ /dev/null @@ -1,48 +0,0 @@ ---- -#- name: Create backup credentials -# template: -# src: restic_access_Linux.j2 -# dest: '{{ restic_script_dir }}/access-{{ item.name }}.sh' -# mode: '0700' -# owner: '{{ restic_dir_owner }}' -# group: '{{ restic_dir_group }}' -# no_log: true -# with_items: '{{ restic_archiver__backups }}' -# when: -# - item.name is defined -# - item.src is defined or item.stdin is defined -# - item.src is defined or item.stdin and item.stdin_cmd is defined -# - item.repo in restic_repos -# -#- name: Create backup script -# template: -# src: restic_script_Linux.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_archiver__backups }}' -# when: -# - item.name is defined -## - item.src is defined or item.stdin is defined - # - item.src is defined or item.stdin and item.stdin_cmd is defined - # - item.repo in restic_repos -# -#- name: Setup CRON jobs -# cron: -# name: 'do1jlr.restic_archiver {{ item.name }}' - ## job: 'CRON=true {{ restic_script_dir }}/backup-{{ item.name }}.sh' - # minute: '{{ item.schedule_minute | default("*") }}' - # hour: '{{ item.schedule_hour | default("*") }}' - # weekday: '{{ item.schedule_weekday | default("*") }}' -# month: '{{ item.schedule_month | default("*") }}' -# cron_file: '/etc/crontab' -# state: present -# become: true -# no_log: true -## with_items: '{{ restic_archiver__backups }}' - # when: - # - restic_create_cron - # - item.name is defined - # - item.scheduled | default(false) diff --git a/vars/main.yml b/vars/main.yml index 535cc6f..3f71bac 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,3 @@ --- -playbook_version_number: 28 # should be over ninethousand +playbook_version_number: 29 # should be over ninethousand playbook_version_path: 'role-restic_archiver_roles-ansible_github.com.version'