From bdf923aafcea2c0aacfdbb80e6cd4b1140ab6f51 Mon Sep 17 00:00:00 2001 From: Josue-T Date: Thu, 15 Mar 2018 18:58:11 +0100 Subject: [PATCH] Solve issue with ynh_restore_file (#384) * Solve issue with ynh_restore_file While we use the `ynh_restore_file` if the destination already exist the `mv` fonction don't work correctly. By this commit the old directory is renamed. * Move to /home/yunohost.conf file if already exist in restoration * Remove if the file is bigger than 500Mo * Use a local variable * Fix DEST_PATH * Fix comment and typo * More precise comment regarding the behavior if DEST_PATH exists --- data/helpers.d/filesystem | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data/helpers.d/filesystem b/data/helpers.d/filesystem index d8e7dc4c7..d4146ad8f 100644 --- a/data/helpers.d/filesystem +++ b/data/helpers.d/filesystem @@ -175,6 +175,9 @@ with open(sys.argv[1], 'r') as backup_file: # the destination will be ORIGIN_PATH or if the ORIGIN_PATH doesn't exist in # the archive, the destination will be searched into backup.csv # +# If DEST_PATH already exists and is lighter than 500 Mo, a backup will be made in +# /home/yunohost.conf/backup/. Otherwise, the existing file is removed. +# # examples: # ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" # # if apps/wordpress/etc/nginx/conf.d/$domain.d/$app.conf exists, restore it into @@ -196,6 +199,20 @@ ynh_restore_file () { ARCHIVE_PATH="$YNH_BACKUP_DIR/$(_get_archive_path \"$ORIGIN_PATH\")" fi + # Move the old directory if it already exists + if [[ -e "${DEST_PATH}" ]] + then + # Check if the file/dir size is less than 500 Mo + if [[ $(du -sb ${DEST_PATH} | cut -d"/" -f1) -le "500000000" ]] + then + local backup_file="/home/yunohost.conf/backup/${DEST_PATH}.backup.$(date '+%Y%m%d.%H%M%S')" + mkdir -p "$(dirname "$backup_file")" + mv "${DEST_PATH}" "$backup_file" # Move the current file or directory + else + ynh_secure_remove ${DEST_PATH} + fi + fi + # Restore ORIGIN_PATH into DEST_PATH mkdir -p $(dirname "$DEST_PATH")