From 30ca76248a061f6cfcc0cd6607cb9a5708b731be Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <arthur@lydra.fr>
Date: Thu, 27 Jan 2022 16:14:12 +0100
Subject: [PATCH] refactor: restructuring role into collection #5

---
 roles/ynh_backup/README-FR.md                 | 20 ++++++++-----
 roles/ynh_backup/README.md                    | 20 ++++++++-----
 roles/ynh_backup/defaults/main.yml            |  6 ++--
 roles/ynh_backup/tasks/backup.yml             | 29 +++++++++----------
 .../templates/ynh_apps_backup.sh.j2           |  7 -----
 roles/ynh_backup/templates/ynh_backup.sh.j2   | 15 ++++++++++
 roles/ynh_config/README.md                    |  4 +--
 7 files changed, 59 insertions(+), 42 deletions(-)
 delete mode 100644 roles/ynh_backup/templates/ynh_apps_backup.sh.j2
 create mode 100644 roles/ynh_backup/templates/ynh_backup.sh.j2

diff --git a/roles/ynh_backup/README-FR.md b/roles/ynh_backup/README-FR.md
index 8f61799..44854a1 100644
--- a/roles/ynh_backup/README-FR.md
+++ b/roles/ynh_backup/README-FR.md
@@ -25,24 +25,28 @@ Les variables par défaut sont disponibles dans `default/main.yml` cependant il
 ### Gestion des sauvegardes
 
 ```yml
-ynh_apps_backup:
-  scheduled: "true"
+ynh_backup:
+  scheduled: True
   directory: "/data/backup"
   scheduled_hour: "*"
   scheduled_minute: "*/30"
   scheduled_weekday: "*"
   scheduled_month: "*"
-  src_script: "templates/ynh_apps_backup.sh.j2"
+  system: True
+  apps: True
+  src_script: "templates/ynh_backup.sh.j2"
   dest_script: "/usr/bin"
 ```
 
 La tâche backup va permettre de sauvegarder les applications Yunohost ainsi que leurs données grâce à la mise en place d'une tâche cron.
 
-- `ynh_apps_backup.scheduled` : active la fonctionnalité de sauvegarde des applications Yunohost, mettez la valeur à `True`.
-- `ynh_apps_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_apps_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/>.
-- `src_script`: il s'agit du chemin absolu où le fichier de template se situe sur la machine qui exécute Ansible. Par défaut, il sera stocké dans `templates/ynh_apps_backup.sh.j2`.
-- `dest_script`: il s'agit du répertoire où le fichier de template va être stocké. Par défaut, il sera stocké dans `/usr/local/bin/`. Le script s'appelle `ynh_apps_backup.sh`.
+- `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` : est obligatoire. Activez la sauvegarde du système Yunohost en mettant la valeur à `True`.
+- `ynh_backup.apps` : est obligatoire. Activez la sauvegarde des applications Yunohost en mettant la valeur à `True`.
+- `src_script`: il s'agit du chemin absolu où le fichier de template se situe sur la machine qui exécute Ansible. Par défaut, il sera stocké dans `templates/ynh_backup.sh.j2`.
+- `dest_script`: il s'agit du répertoire où le fichier de template va être stocké. Par défaut, il sera stocké dans `/usr/local/bin/`. Le script s'appelle `ynh_backup.sh`.
 
 ## Dépendances
 
diff --git a/roles/ynh_backup/README.md b/roles/ynh_backup/README.md
index 5afc853..a1730f8 100644
--- a/roles/ynh_backup/README.md
+++ b/roles/ynh_backup/README.md
@@ -25,24 +25,28 @@ Default variables are available in `default/main.yml` however it is necessary to
 ### Backups management
 
 ```yml
-ynh_apps_backup:
-  scheduled: "true"
+ynh_backup:
+  scheduled: True
   directory: "/data/backup"
   scheduled_hour: "*"
   scheduled_minute: "*/30"
   scheduled_weekday: "*"
   scheduled_month: "*"
-  src_script: "templates/ynh_apps_backup.sh.j2"
+  system: True
+  apps: True
+  src_script: "templates/ynh_backup.sh.j2"
   dest_script: "/usr/bin"
 ```
 
 The backup task will allow to backup Yunohost applications and their data by setting up a cron job.
 
-- `ynh_apps_backup.scheduled`: to enable the Yunohost applications backup feature, set the value to `True`.
-- `ynh_apps_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_apps_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/>.
-- `src_script`: this is the absolute path where the template file is located on the machine running Ansible. By default is will be stored in `templates/ynh_apps_backup.sh.j2`.
-- `dest_script`: this is the directory where the template file will be stored. By default is will be stored in `/usr/local/bin`. The script is named `ynh_apps_backup.sh`.
+- `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` : is mandatory. Enables automatic backup of the Yunohost system by setting the value to `True`.
+- `ynh_backup.apps` : est obligatoire. Enables automatic backup of Yunohost applications by setting the value to `True`.
+- `src_script`: this is the absolute path where the template file is located on the machine running Ansible. By default is will be stored in `templates/ynh_backup.sh.j2`.
+- `dest_script`: this is the directory where the template file will be stored. By default is will be stored in `/usr/local/bin`. The script is named `ynh_backup.sh`.
 
 ## Dependencies
 
diff --git a/roles/ynh_backup/defaults/main.yml b/roles/ynh_backup/defaults/main.yml
index 2ccce9a..a9b418f 100644
--- a/roles/ynh_backup/defaults/main.yml
+++ b/roles/ynh_backup/defaults/main.yml
@@ -19,12 +19,14 @@
 #-----------------------------------------------------------------------------#
 
 # Variables for apps backup
-ynh_apps_backup:
+ynh_backup:
   scheduled: False
   # directory: "/data/backup"
   # scheduled_hour: 3
   # scheduled_minute: "0"
   # scheduled_weekday: "*"
   # scheduled_month: "*"
-  # src_script: "templates/ynh_apps_backup.sh.j2"
+  # system: True
+  # apps: True
+  # src_script: "templates/ynh_backup.sh.j2"
   # dest_script: "/usr/local/bin/"
diff --git a/roles/ynh_backup/tasks/backup.yml b/roles/ynh_backup/tasks/backup.yml
index 183b512..e3f1f0f 100644
--- a/roles/ynh_backup/tasks/backup.yml
+++ b/roles/ynh_backup/tasks/backup.yml
@@ -20,26 +20,25 @@
 
 - name: Create backup folder
   ansible.builtin.file:
-    path: "{{ ynh_apps_backup.directory | default('/home/yunohost.backup/archives') }}"
+    path: "{{ ynh_backup.directory | default('/home/yunohost.backup/archives') }}"
     state: directory
     mode: '0750'
-  tags: test
-- name: Create apps backup script
+
+- name: Create backup script
   ansible.builtin.template:
-    src: "{{ ynh_apps_backup.src_script | default('templates/ynh_apps_backup.sh.j2') }}"
-    dest: "{{ ynh_apps_backup.dest_script | default('/usr/local/bin/') }}ynh_apps_backup.sh"
+    src: "{{ ynh_backup.src_script | default('templates/ynh_backup.sh.j2') }}"
+    dest: "{{ ynh_backup.dest_script | default('/usr/local/bin/') }}ynh_backup.sh"
     owner: root
     group: root
     mode: '0740'
-  tags: test
-- name: Create cron task to perform YNH apps backup
+
+- name: Create cron task to perform YNH backup
   ansible.builtin.cron:
-    name: "auto-backup apps to {{ ynh_apps_backup.directory | default('/home/yunohost.backup/archives') }}"
-    month: "{{ ynh_apps_backup.scheduled_month | default('*') }}"
-    weekday: "{{ ynh_apps_backup.scheduled_weekday | default('*') }}"
-    hour: "{{ ynh_apps_backup.scheduled_hour | default('1') }}"
-    minute: "{{ ynh_apps_backup.scheduled_minute | default('0') }}"
+    name: "auto-backup to {{ ynh_backup.directory | default('/home/yunohost.backup/archives') }}"
+    month: "{{ ynh_backup.scheduled_month | default('*') }}"
+    weekday: "{{ ynh_backup.scheduled_weekday | default('*') }}"
+    hour: "{{ ynh_backup.scheduled_hour | default('1') }}"
+    minute: "{{ ynh_backup.scheduled_minute | default('0') }}"
     user: root
-    job: "{{ ynh_apps_backup.dest_script | default('/usr/local/bin/') }}ynh_apps_backup.sh"
-    cron_file: ynh_apps_backup_cron
-  tags: test
+    job: "{{ ynh_backup.dest_script | default('/usr/local/bin/') }}ynh_backup.sh"
+    cron_file: ynh_backup_cron
diff --git a/roles/ynh_backup/templates/ynh_apps_backup.sh.j2 b/roles/ynh_backup/templates/ynh_apps_backup.sh.j2
deleted file mode 100644
index e31df1a..0000000
--- a/roles/ynh_backup/templates/ynh_apps_backup.sh.j2
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-
-{% if ynh_apps_backup.directory is string %}
-yunohost backup create --apps --output-directory {{ ynh_apps_backup.directory }}/backup_$(date +%Y%m%d_%H%M)
-{% else %}
-yunohost backup create --apps
-{% endif %}
diff --git a/roles/ynh_backup/templates/ynh_backup.sh.j2 b/roles/ynh_backup/templates/ynh_backup.sh.j2
new file mode 100644
index 0000000..b6f08d4
--- /dev/null
+++ b/roles/ynh_backup/templates/ynh_backup.sh.j2
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+{% if ynh_backup.system and ynh_backup.apps and ynh_backup.directory %}
+    yunohost backup create --output-directory {{ ynh_backup.directory }}/backup_$(date +%Y%m%d_%H%M)
+{% elif ynh_backup.system and ynh_backup.apps %}
+    yunohost backup create
+{% elif ynh_backup.system %}
+    yunohost backup create --system
+{% elif ynh_backup.apps %}
+    yunohost backup create --apps
+{% endif %}
+{% if ynh_backup.system is false and ynh_backup.apps is false %}
+    echo "Read the readme to know more about ynh_backup.system and ynh_backup.apps"
+    echo "TO BE DEFINED"
+    exit 1
diff --git a/roles/ynh_config/README.md b/roles/ynh_config/README.md
index 34af3eb..fa774db 100644
--- a/roles/ynh_config/README.md
+++ b/roles/ynh_config/README.md
@@ -52,8 +52,8 @@ A cron job can been set up to automate the check for system and application upda
 
 - `ynh_autoupdate.scheduled` : enables the cron job by setting the value to `True`.
 - `ynh_autoupdate.special_time`: it is mandatory. It allows you to specify when you want this task to be executed. Possible values: (`annually`,`daily`,`hourly`,`monthly`,`reboot`,`weekly`,`yearly`). To learn more about special times, click [here](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/cron_module.html).
-- `ynh_autoupdate.apps`: is mandatory. Enable automatic updating of Yunohost applications by setting the value to `True`.
-- `ynh_autoupdate.system`: is mandatory. Enable automatic updating of the Yunohost system by setting the value to `True`.
+- `ynh_autoupdate.apps`: is mandatory. Enables automatic updating of Yunohost applications by setting the value to `True`.
+- `ynh_autoupdate.system`: is mandatory. Enables automatic updating of the Yunohost system by setting the value to `True`.
 - `ynh_autoupdate.dest_script`: it is the path to the directory where the update script will be installed on the server. The default value is `/usr/local/bin`. The script is named `ynh_autoupdate.sh`.
 
 If available, updates are done automatically. In case of problems following an application update, you can read logs located in `/var/log/yunohost/categories/operation` . You also have the possibility to rollback to the previous version since Yunohost always makes an automatic backup of an application when it is updated. 
-- 
GitLab