From 85b1a98baeb7954c8fc3bbcaa818f9e16399661d Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Mon, 9 Mar 2020 23:20:33 +0100
Subject: [PATCH] Add rsnapshot role

---
 rsnapshot/defaults/main.yml                   |  3 ++
 .../files/list-rsnapshot-config-files.sh      |  4 ++
 rsnapshot/tasks/main.yml                      | 38 +++++++++++++++++++
 rsnapshot/templates/crontab.j2                |  7 ++++
 rsnapshot/templates/rsnapshot.conf.j2         | 22 +++++++++++
 5 files changed, 74 insertions(+)
 create mode 100644 rsnapshot/defaults/main.yml
 create mode 100644 rsnapshot/files/list-rsnapshot-config-files.sh
 create mode 100644 rsnapshot/tasks/main.yml
 create mode 100644 rsnapshot/templates/crontab.j2
 create mode 100644 rsnapshot/templates/rsnapshot.conf.j2

diff --git a/rsnapshot/defaults/main.yml b/rsnapshot/defaults/main.yml
new file mode 100644
index 0000000..47c45ef
--- /dev/null
+++ b/rsnapshot/defaults/main.yml
@@ -0,0 +1,3 @@
+---
+
+rsnapshot_enable_frequently: true
diff --git a/rsnapshot/files/list-rsnapshot-config-files.sh b/rsnapshot/files/list-rsnapshot-config-files.sh
new file mode 100644
index 0000000..487652c
--- /dev/null
+++ b/rsnapshot/files/list-rsnapshot-config-files.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+for file in /etc/rsnapshot.d/*.conf; do
+	echo "include_conf	$file"
+done
diff --git a/rsnapshot/tasks/main.yml b/rsnapshot/tasks/main.yml
new file mode 100644
index 0000000..86dfd49
--- /dev/null
+++ b/rsnapshot/tasks/main.yml
@@ -0,0 +1,38 @@
+---
+
+- name: install rsnapshot
+  apt:
+    name: rsnapshot
+    state: present
+
+- name: create drop-in directory for backup config
+  file:
+    path: /etc/rsnapshot.d
+    state: directory
+    owner: root
+    group: root
+    mode: '0755'
+
+- name: create script for rsnapshot's crazy include_conf to work
+  copy:
+    src: "list-rsnapshot-config-files.sh"
+    dest: "/usr/local/bin"
+    owner: root
+    group: root
+    mode: '0755'
+
+- name: configure rsnapshot
+  template:
+    src: rsnapshot.conf.j2
+    dest: /etc/rsnapshot.conf
+    owner: root
+    group: root
+    mode: '0644'
+
+- name: make rsnapshot run regularly
+  template:
+    src: crontab.j2
+    dest: /etc/cron.d/rsnapshot
+    owner: root
+    group: root
+    mode: '0644'
diff --git a/rsnapshot/templates/crontab.j2 b/rsnapshot/templates/crontab.j2
new file mode 100644
index 0000000..e6cfe4f
--- /dev/null
+++ b/rsnapshot/templates/crontab.j2
@@ -0,0 +1,7 @@
+{% if rsnapshot_enable_frequently %}
+5,20,35,50 * * * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf frequently
+{% endif %}
+3 * * * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf hourly
+51 23 * * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf daily
+41 23 * * 6 root /usr/bin/rsnapshot -c /etc/rsnapshot.conf weekly
+31 23 2 * * root /usr/bin/rsnapshot -c /etc/rsnapshot.conf monthly
diff --git a/rsnapshot/templates/rsnapshot.conf.j2 b/rsnapshot/templates/rsnapshot.conf.j2
new file mode 100644
index 0000000..381b296
--- /dev/null
+++ b/rsnapshot/templates/rsnapshot.conf.j2
@@ -0,0 +1,22 @@
+config_version	1.2
+snapshot_root	/var/backups
+
+include_conf	`/usr/local/bin/list-rsnapshot-config-files.sh`
+
+cmd_cp	/bin/cp
+cmd_rm	/bin/rm
+cmd_rsync	/usr/bin/rsync
+cmd_logger	/usr/bin/logger
+cmd_du	/usr/bin/du
+
+{% if rsnapshot_enable_frequently %}
+retain	frequently	4
+{% endif %}
+retain	hourly	6
+retain	daily	7
+retain	weekly	4
+retain	monthly	6
+
+verbose	2
+loglevel	4
+lockfile	/run/rsnapshot.pid
-- 
GitLab