From 4963c360b87661e4ae348143962235452bf96fd9 Mon Sep 17 00:00:00 2001
From: Tom Teichler <tom.teichler@teckids.org>
Date: Tue, 14 Apr 2020 23:12:07 +0200
Subject: [PATCH] Re-add django-dbbackup

---
 aleksis/core/settings.py | 11 +++++++++++
 aleksis/core/tasks.py    | 11 +++++++++++
 pyproject.toml           |  1 +
 3 files changed, 23 insertions(+)

diff --git a/aleksis/core/settings.py b/aleksis/core/settings.py
index cf2acb349..e0f652eca 100644
--- a/aleksis/core/settings.py
+++ b/aleksis/core/settings.py
@@ -55,6 +55,7 @@ INSTALLED_APPS = [
     "django.contrib.humanize",
     "polymorphic",
     "django_global_request",
+    "dbbackup",
     "settings_context_processor",
     "sass_processor",
     "easyaudit",
@@ -407,6 +408,16 @@ MAINTENANCE_MODE_STATE_FILE_PATH = _settings.get(
     "maintenance.statefile", "maintenance_mode_state.txt"
 )
 
+DBBACKUP_STORAGE = _settings.get("backup.storage", "django.core.files.storage.FileSystemStorage")
+DBBACKUP_STORAGE_OPTIONS = {"location": _settings.get("backup.location", "/var/backups/aleksis")}
+DBBACKUP_CLEANUP_KEEP = _settings.get("backup.database.keep", 10)
+DBBACKUP_CLEANUP_KEEP_MEDIA = _settings.get("backup.media.keep", 10)
+DBBACKUP_GPG_RECIPIENT = _settings.get("backup.gpg_recipient", None)
+DBBACKUP_COMPRESS_DB = _settings.get("backup.database.compress", True)
+DBBACKUP_ENCRYPT_DB = _settings.get("backup.database.encrypt", DBBACKUP_GPG_RECIPIENT is not None)
+DBBACKUP_COMPRESS_MEDIA = _settings.get("backup.media.compress", True)
+DBBACKUP_ENCRYPT_MEDIA = _settings.get("backup.media.encrypt", DBBACKUP_GPG_RECIPIENT is not None)
+
 IMPERSONATE = {"USE_HTTP_REFERER": True, "REQUIRE_SUPERUSER": True, "ALLOW_SUPERUSER": True}
 
 DJANGO_TABLES2_TEMPLATE = "django_tables2/materialize.html"
diff --git a/aleksis/core/tasks.py b/aleksis/core/tasks.py
index a59f829e8..2c4c40a8f 100644
--- a/aleksis/core/tasks.py
+++ b/aleksis/core/tasks.py
@@ -1,3 +1,5 @@
+from django.core import management
+
 from .util.core_helpers import celery_optional
 from .util.notifications import send_notification as _send_notification
 
@@ -5,3 +7,12 @@ from .util.notifications import send_notification as _send_notification
 @celery_optional
 def send_notification(notification: int, resend: bool = False) -> None:
     _send_notification(notification, resend)
+
+
+@celery_optional
+def backup_data() -> None:
+    db_options = "-z " * settings.DBBACKUP_COMPRESS_DB + "-e" * settings.DBBACKUP_ENCRYPT_DB
+    media_options = "-z " * settings.DBBACKUP_COMPRESS_MEDIA + "-e" * settings.DBBACKUP_ENCRYPT_MEDIA
+
+    management.call_command("dbbackup", db_options)
+    management.call_command("mediabackup", media_options)
diff --git a/pyproject.toml b/pyproject.toml
index 7bc7ff40a..5ce4997be 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -68,6 +68,7 @@ django-otp = "0.7.5"
 django-colorfield = "^0.2.1"
 django-bleach = "^0.6.1"
 django-memoize = "^2.2.1"
+django-dbbackup = "^3.3.0"
 
 [tool.poetry.extras]
 ldap = ["django-auth-ldap"]
-- 
GitLab