From 41a1d1d0ebe4e082c15e026b488a57e740587c1f Mon Sep 17 00:00:00 2001 From: Arthur BOUDREAULT <arthur@lydra.fr> Date: Tue, 18 Jan 2022 17:01:10 +0100 Subject: [PATCH] feat: add template script --- README-FR.md | 27 ++++++++++++--------------- README.md | 25 +++++++++++-------------- defaults/main.yml | 4 +++- tasks/backup.yml | 20 +++++++++++++++----- templates/ynh_apps_backup.sh.j2 | 7 +++++++ 5 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 templates/ynh_apps_backup.sh.j2 diff --git a/README-FR.md b/README-FR.md index ae8320e..472fb75 100644 --- a/README-FR.md +++ b/README-FR.md @@ -123,7 +123,7 @@ Il est possible de compléter l'installation des applications par l'ajout de tem Pour activer cette fonctionnalité, définissez la variable `post_install` qui correspond à la liste des fichiers de post-installation de votre application. Cette tâche utilisant le module template, vous pouvez tout à fait utiliser vos propres variables et les appeler dans vos fichiers de template. Pour en savoir sur ce module, cliquez [ici](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html). -- `src` est obligatoire. Il s'agit du répertoire où le fichier de template se situe sur la machine qui execute Ansible. +- `src` est obligatoire. Il s'agit du répertoire où le fichier de template se situe sur la machine qui exécute Ansible. - `dest` est obligatoire. Il s'agit du répertoire où le fichier de template va être stocké. - `type` est obligatoire : - Si vous précisez comme valeur `script` alors le fichier de template aura pour droits 740. Il sera exécuté après son transfert sur le serveur Yunohost (généralement dans `/tmp/`) puis il sera supprimé. @@ -134,25 +134,22 @@ Pour `owner` et `group`, par défaut le fichier va prendre comme utilisateur pro ### Gestion des sauvegardes ```yml -- name: Create cron task to perform YNH apps 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') }}" - user: root - job: "{% if ynh_apps_backup.directory is string %} - yunohost backup create --apps --output-directory {{ ynh_apps_backup.directory }}_$(date +%Y%m%d_%H%M%S) - {% else %} - yunohost backup create --apps - {% endif %}" - cron_file: ynh_apps_backup_cron +ynh_apps_backup: + scheduled: "true" + directory: "/data/backup" + scheduled_hour: "*" + scheduled_minute: "*/30" + scheduled_weekday: "*" + scheduled_month: "*" + src_script: "templates/ynh_apps_backup.sh.j2" + dest_script: "/data/backup/backup_ynh_apps.sh" ``` 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` : Pour activer 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`: Permet de modifier quand la tâche cron se déclenche. 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` est obligatoire. Il s'agit du répertoire où le fichier de template se situe sur la machine qui exécute Ansible. +- `dest_script` est obligatoire. Il s'agit du répertoire où le fichier de template va être stocké. ## Dépendances diff --git a/README.md b/README.md index 56e8165..0c80072 100644 --- a/README.md +++ b/README.md @@ -134,26 +134,23 @@ For `owner` and `group`, by default the file will take as owner the name of the ### Backups management ```yml -- name: Create cron task to perform YNH apps 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') }}" - user: root - job: "{% if ynh_apps_backup.directory is string %} - yunohost backup create --apps --output-directory {{ ynh_apps_backup.directory }}_$(date +%Y%m%d_%H%M%S) - {% else %} - yunohost backup create --apps - {% endif %}" - cron_file: ynh_apps_backup_cron +ynh_apps_backup: + scheduled: "true" + directory: "/data/backup" + scheduled_hour: "*" + scheduled_minute: "*/30" + scheduled_weekday: "*" + scheduled_month: "*" + src_script: "templates/ynh_apps_backup.sh.j2" + dest_script: "/data/backup/backup_ynh_apps.sh" ``` 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`: Allows you to change when the cron job is triggered. 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` is mandatory. This is the directory where the template file is located on the machine running Ansible. +- `dest_script` is required. This is the directory where the template file will be stored. ## Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index 8410b63..d773c28 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -63,7 +63,7 @@ ynh_apps: null # owner: ttrss # Only provide if different from app name # group: ttrss # Only provide if different from www-data -# Variables for backup +# Variables for apps backup ynh_apps_backup: scheduled: "false" # directory: "/data/backup" @@ -71,3 +71,5 @@ ynh_apps_backup: # scheduled_minute: "0" # scheduled_weekday: "*" # scheduled_month: "*" + # src_script: "templates/ynh_apps_backup.sh.j2" + # dest_script: "/data/backup/backup_ynh_apps.sh" diff --git a/tasks/backup.yml b/tasks/backup.yml index 15e732e..c454fc2 100644 --- a/tasks/backup.yml +++ b/tasks/backup.yml @@ -18,6 +18,20 @@ # # #-----------------------------------------------------------------------------# +- name: Create backup folder + ansible.builtin.file: + path: "{{ ynh_apps_backup.directory | default('/home/yunohost.backup/') }}" + state: directory + mode: '0750' + +- name: Create apps backup script + ansible.builtin.template: + src: "{{ ynh_apps_backup.src_script }}" + dest: "{{ ynh_apps_backup.dest_script }}" + owner: root + group: root + mode: '0740' + - name: Create cron task to perform YNH apps backup ansible.builtin.cron: name: "auto-backup apps to {{ ynh_apps_backup.directory | default('/home/yunohost.backup/archives') }}" @@ -26,9 +40,5 @@ hour: "{{ ynh_apps_backup.scheduled_hour | default('1') }}" minute: "{{ ynh_apps_backup.scheduled_minute | default('0') }}" user: root - job: "{% if ynh_apps_backup.directory is string %} - yunohost backup create --apps --output-directory {{ ynh_apps_backup.directory }}_$(date +%Y%m%d_%H%M%S) - {% else %} - yunohost backup create --apps - {% endif %}" + job: "{{ ynh_apps_backup.dest_script }}" cron_file: ynh_apps_backup_cron diff --git a/templates/ynh_apps_backup.sh.j2 b/templates/ynh_apps_backup.sh.j2 new file mode 100644 index 0000000..e973978 --- /dev/null +++ b/templates/ynh_apps_backup.sh.j2 @@ -0,0 +1,7 @@ +#!/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%S) +{% else %} +yunohost backup create --apps +{% endif %} -- GitLab