diff --git a/roles/ynh_backup/README-FR.md b/roles/ynh_backup/README-FR.md index 3a383e8..e36ffae 100644 --- a/roles/ynh_backup/README-FR.md +++ b/roles/ynh_backup/README-FR.md @@ -32,6 +32,8 @@ ynh_backup: apps: True src_script: "templates/ynh_backup.sh.j2" dest_script: "/usr/bin" + +number_days_to_keep: "2" ``` - `ynh_backup.scheduled` : active la fonctionnalité de sauvegarde des applications YunoHost, mettez la valeur à `True`. @@ -39,6 +41,7 @@ ynh_backup: - `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 : . - `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`. +- `number_days_to_keep` : **obligatoire**. Détermine le nombre de jours à garder pour le système de purge. ### Sauvegardes distantes avec BorgBackup diff --git a/roles/ynh_backup/README.md b/roles/ynh_backup/README.md index 2c332bd..5ce8238 100644 --- a/roles/ynh_backup/README.md +++ b/roles/ynh_backup/README.md @@ -39,6 +39,7 @@ ynh_backup: - `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: . - `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`. +- `number_days_to_keep` : **mandatory**. Determines the number of days to keep for the purging system. ### remote backups with YunoHost BorgBackup diff --git a/roles/ynh_backup/defaults/main.yml b/roles/ynh_backup/defaults/main.yml index 99c2dc4..fa8dd2f 100644 --- a/roles/ynh_backup/defaults/main.yml +++ b/roles/ynh_backup/defaults/main.yml @@ -29,6 +29,8 @@ ynh_backup: # system: True # apps: True +number_days_to_keep: "2" + # Variables for YunoHost BorgBackup ynh_borg_backup_scheduled: False borg_source_directories: diff --git a/roles/ynh_backup/templates/ynh_backup.sh.j2 b/roles/ynh_backup/templates/ynh_backup.sh.j2 index a4a7d40..8021f9c 100644 --- a/roles/ynh_backup/templates/ynh_backup.sh.j2 +++ b/roles/ynh_backup/templates/ynh_backup.sh.j2 @@ -1,8 +1,33 @@ -#!/bin/sh +#!/bin/bash +today="$(date +%Y%m%d)" +number_to_keep="{{ number_days_to_keep }}" +old_backup_list="$(yunohost backup list --output-as plain | head -n -"$number_to_keep")" # Afficher toutes les lignes de sauvegardes à part les deux dernières. -yunohost backup create {% if ynh_backup.system %}--system{% endif %}{% if ynh_backup.apps %} --apps{% endif %}{% if ynh_backup.directory %} --output-directory {{ ynh_backup.directory }}/backup_$(date +%Y%m%d_%H%M) {% endif %} +_good() { + echo "SUCCESS: ${1}" && exit 0 +} -{% if ynh_backup.system is false and ynh_backup.apps is false %} - echo "Read the readme to know more about vars ynh_backup.system and ynh_backup.apps" - exit 1 -{% endif %} +_fail() { + echo "ERROR: ${1}" && exit 1 +} + +_create_ynh_backup() { + echo "Backing up $today YunoHost data now." + yunohost backup create {% if ynh_backup.system %}--system{% endif %}{% if ynh_backup.apps %} --apps{% endif %}{% if ynh_backup.directory %} --output-directory {{ ynh_backup.directory }}/backup_"$today" {% endif %} || _fail "can't create a backup" + +} + +_prune_old_backup() { + if [ -n "$old_backup_list" ]; then + for backup in $old_backup_list; do + echo "Backup $backup is 2 days old or more. Purging it now." + yunohost backup delete "$backup" + done + _good "Purging of old backups completed." + else + _good "There is no old backup to be purged." + fi +} + +_create_ynh_backup +_prune_old_backup