From aedcc123d6b9e25e34fc1faae472e0d4df56446f Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <arthur@lydra.fr>
Date: Mon, 18 Jul 2022 15:19:59 +0200
Subject: [PATCH] refactor(ynh_backup): refactor YunoHost backup tasks and
 readme to be more explicit w/ what we expect from user

---
 roles/ynh_backup/README-FR.md      | 11 ++++++-----
 roles/ynh_backup/README.md         | 11 ++++++-----
 roles/ynh_backup/defaults/main.yml | 14 +++++++-------
 roles/ynh_backup/tasks/backup.yml  |  5 +++++
 4 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/roles/ynh_backup/README-FR.md b/roles/ynh_backup/README-FR.md
index 3a383e8..224788a 100644
--- a/roles/ynh_backup/README-FR.md
+++ b/roles/ynh_backup/README-FR.md
@@ -34,11 +34,12 @@ ynh_backup:
   dest_script: "/usr/bin"
 ```
 
-- `ynh_backup.scheduled` : active la fonctionnalité de sauvegarde des applications YunoHost, mettez la valeur à `True`.
-- `ynh_backup.directory` : le dossier de sauvegarde par défaut est `/home/yunohost.backup/archives` cependant vous pouvez tout à fait choisir de sauvegarder les backups dans un autre dossier grâce à cette variable. Dans ce cas, de manière à pouvoir restaurer les backups depuis l'interface web, YunoHost créé automatiquement un lien symbolique de l'archive créée vers son dossier par défaut.
-- `ynh_backup.scheduled_[hour|minute|weekday|month]`: modifie la planification de la tâche cron. Par défaut, elle se déclenchera tous les jours de l'année à 1 heure du matin. Pour plus d'informations concernant les réglages horaires cron, cet outil peut être utile : <https://crontab.guru/>.
-- `ynh_backup.system` : **obligatoire**. Activez la sauvegarde du système YunoHost en mettant la valeur à `True`.
-- `ynh_backup.apps` : **obligatoire**. Activez la sauvegarde des applications YunoHost en mettant la valeur à `True`.
+- `ynh_backup.scheduled` : active la fonctionnalité de sauvegarde des applications YunoHost en mettant la valeur à `True`.
+- `ynh_backup.directory` : le dossier de sauvegarde par défaut est `/home/yunohost.backup/archives`. Vous pouvez choisir de sauvegarder les backups dans un autre dossier grâce à cette variable. Dans ce cas, de manière à pouvoir restaurer les backups depuis l'interface web, YunoHost créé automatiquement un lien symbolique de l'archive créée vers son dossier par défaut.
+- `ynh_backup.scheduled_[hour|minute|weekday|month]`: modifie la planification de la tâche cron. Par défaut, elle se déclenchera tous les jours de l'année à 3 heure du matin. Pour plus d'informations concernant les réglages horaires cron, cet outil peut être utile : <https://crontab.guru/>.
+- `ynh_backup.system` : Désactivez la sauvegarde du système YunoHost en mettant la valeur à `False`, la valeur par défaut est à `True`.
+- `ynh_backup.apps` : Désactivez la sauvegarde des applications YunoHost en mettant la valeur à `False`, la valeur par défaut est à `True`.
+- ⚠️ Attention, à partir du moment où vous activez la fonctionnalité de sauvegarde locale `ynh_backup.scheduled`, vous ne pouvez pas désactiver les sauvegardes système **et** applications. Si vous mettez `ynh_backup.system` **et** `ynh_backup.apps` à `False`, le rôle tombera en erreur.
 
 ### Sauvegardes distantes avec BorgBackup
 
diff --git a/roles/ynh_backup/README.md b/roles/ynh_backup/README.md
index 2c332bd..2fe92ed 100644
--- a/roles/ynh_backup/README.md
+++ b/roles/ynh_backup/README.md
@@ -34,11 +34,12 @@ ynh_backup:
   dest_script: "/usr/bin"
 ```
 
-- `ynh_backup.scheduled`: to enable the YunoHost applications backup feature, set the value to `True`.
-- `ynh_backup.directory`: the default backup folder is `/home/yunohost.backup/archives` however you can choose to save the backups in another folder with this variable. In this case, in order to be able to restore the backups from the web interface, YunoHost automatically creates a symbolic link from the created archive to its default folder.
-- `ynh_backup.scheduled_[hour|minute|weekday|month]`: modifies the scheduling of the cron task. By default, it will run every day of the year at 1am. For more information about cron time settings, this tool can be useful: <https://crontab.guru/>.
-- `ynh_backup.system`: **mandatory**. Enables automatic backup of the YunoHost system by setting the value to `True`.
-- `ynh_backup.apps`: **mandatory**. Enables automatic backup of YunoHost applications by setting the value to `True`.
+- `ynh_backup.scheduled`: Enable the YunoHost applications backup feature by setting the value to `True`.
+- `ynh_backup.directory`: the default backup folder is `/home/yunohost.backup/archives`. You can choose to save the backups in another folder with this variable. In this case, in order to be able to restore the backups from the web interface, YunoHost automatically creates a symbolic link from the created archive to its default folder.
+- `ynh_backup.scheduled_[hour|minute|weekday|month]`: modifies the scheduling of the cron task. By default, it will run every day of the year at 3am. For more information about cron time settings, this tool can be useful: <https://crontab.guru/>.
+- `ynh_backup.system`: Disable YunoHost system backup by setting the value to `False`, the default value is `True`.
+- `ynh_backup.apps`: Disable backup of YunoHost applications by setting the value to `False`, the default is `True`.
+- ⚠️ Beware, once you enable the local backup feature `ynh_backup.scheduled`, you cannot disable system **and** application backups. If you set `ynh_backup.system` **and** `ynh_backup.apps` to `False`, the role will fail.
 
 ### remote backups with YunoHost BorgBackup
 
diff --git a/roles/ynh_backup/defaults/main.yml b/roles/ynh_backup/defaults/main.yml
index 99c2dc4..dd9f812 100644
--- a/roles/ynh_backup/defaults/main.yml
+++ b/roles/ynh_backup/defaults/main.yml
@@ -21,13 +21,13 @@
 # Variables for local YunoHost backups
 ynh_backup:
   scheduled: False
-  # directory: "/data/backup"
-  # scheduled_hour: 3
-  # scheduled_minute: "0"
-  # scheduled_weekday: "*"
-  # scheduled_month: "*"
-  # system: True
-  # apps: True
+  directory: "/data/backup"
+  scheduled_hour: "3"
+  scheduled_minute: "0"
+  scheduled_weekday: "*"
+  scheduled_month: "*"
+  system: True
+  apps: True
 
 # Variables for YunoHost BorgBackup
 ynh_borg_backup_scheduled: False
diff --git a/roles/ynh_backup/tasks/backup.yml b/roles/ynh_backup/tasks/backup.yml
index 688d36a..9dbc153 100644
--- a/roles/ynh_backup/tasks/backup.yml
+++ b/roles/ynh_backup/tasks/backup.yml
@@ -17,6 +17,11 @@
 # along with this program. If not, see <http://www.gnu.org/licenses/>.        #
 #                                                                             #
 #-----------------------------------------------------------------------------#
+- name: Fail if variables are not used correctly
+  ansible.builtin.fail:
+    msg: You need to define variable ynh_backup.apps and / or ynh_backup.system to True
+  when: ynh_backup.apps is false and ynh_backup.system is false
+  tags: backup
 
 - name: Create backup folder if doesn't already exist
   ansible.builtin.file:
-- 
GitLab