From bf45cd7c4b1ff575ef679fdcc44ee77170fd6cb1 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Fri, 28 Jul 2017 18:53:43 +0200 Subject: [PATCH] =?UTF-8?q?New=20helpers=20ynh=5Fbackup=5Fafter=5Ffailed?= =?UTF-8?q?=5Fupgrade=20and=20ynh=5Fbackup=5Fbefore=5Fupg=E2=80=A6=20(#289?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * New helpers ynh_backup_after_failed_upgrade and ynh_backup_before_upgrade Create a backup at the beginning of upgrade script. Then, if the upgrade script failed, remove the app and restore it. The backup is kept, to provide a way to restore if the upgrade is finally a bad thing. * Implement @JimboJoe's comments * Indentation * --ignore-hooks -> --ignore-system --- data/helpers.d/utils | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/data/helpers.d/utils b/data/helpers.d/utils index 8ff9e5791..bfa6ebbf2 100644 --- a/data/helpers.d/utils +++ b/data/helpers.d/utils @@ -25,6 +25,66 @@ ynh_get_plain_key() { done } +# Restore a previous backup if the upgrade process failed +# +# usage: +# ynh_backup_before_upgrade +# ynh_clean_setup () { +# ynh_backup_after_failed_upgrade +# } +# ynh_abort_if_errors +# +ynh_backup_after_failed_upgrade () { + echo "Upgrade failed." >&2 + app_bck=${app//_/-} # Replace all '_' by '-' + + # Check if an existing backup can be found before removing and restoring the application. + if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$backup_number + then + # Remove the application then restore it + sudo yunohost app remove $app + # Restore the backup + sudo yunohost backup restore --ignore-system $app_bck-pre-upgrade$backup_number --apps $app --force + ynh_die "The app was restored to the way it was before the failed upgrade." + fi +} + +# Make a backup in case of failed upgrade +# +# usage: +# ynh_backup_before_upgrade +# ynh_clean_setup () { +# ynh_backup_after_failed_upgrade +# } +# ynh_abort_if_errors +# +ynh_backup_before_upgrade () { + backup_number=1 + old_backup_number=2 + app_bck=${app//_/-} # Replace all '_' by '-' + + # Check if a backup already exists with the prefix 1 + if sudo yunohost backup list | grep -q $app_bck-pre-upgrade1 + then + # Prefix becomes 2 to preserve the previous backup + backup_number=2 + old_backup_number=1 + fi + + # Create backup + sudo yunohost backup create --ignore-system --apps $app --name $app_bck-pre-upgrade$backup_number + if [ "$?" -eq 0 ] + then + # If the backup succeeded, remove the previous backup + if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$old_backup_number + then + # Remove the previous backup only if it exists + sudo yunohost backup delete $app_bck-pre-upgrade$old_backup_number > /dev/null + fi + else + ynh_die "Backup failed, the upgrade process was aborted." + fi + # Download, check integrity, uncompress and patch the source from app.src # # The file conf/app.src need to contains: