From 17c14c004e821291effa243ae1690d974a768cea Mon Sep 17 00:00:00 2001 From: Rafi59 Date: Mon, 3 Jul 2017 09:26:31 +0200 Subject: [PATCH] [enh] Add helper --- scripts/_common.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/scripts/_common.sh b/scripts/_common.sh index 02dd131..e82e1cc 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -191,6 +191,41 @@ ynh_remove_logrotate () { sudo rm "/etc/logrotate.d/$app" fi } +# Verify the checksum and backup the file if it's different +# This helper is primarily meant to allow to easily backup personalised/manually +# modified config files. +# +# $app should be defined when calling this helper +# +# usage: ynh_backup_if_checksum_is_different file [compress] +# | arg: file - The file on which the checksum test will be perfomed. +# | arg: compress - 1 to compress the backup instead of a simple copy +# A compression is needed for a file which will be analyzed even if its name is different. +# +# | ret: Return the name a the backup file, or nothing +ynh_backup_if_checksum_is_different () { + local file=$1 + local compress_backup=${2:-0} # If $2 is empty, compress_backup will set at 0 + local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' + local checksum_value=$(ynh_app_setting_get $app $checksum_setting_name) + if [ -n "$checksum_value" ] + then # Proceed only if a value was stored into the app settings + if ! echo "$checksum_value $file" | sudo md5sum -c --status + then # If the checksum is now different + backup_file="$file.backup.$(date '+%d.%m.%y_%Hh%M,%Ss')" + if [ $compress_backup -eq 1 ] + then + sudo tar --create --gzip --file "$backup_file.tar.gz" "$file" # Backup the current file and compress + backup_file="$backup_file.tar.gz" + else + sudo cp -a "$file" "$backup_file" # Backup the current file + fi + echo "File $file has been manually modified since the installation or last upgrade. So it has been duplicated in $backup_file" >&2 + echo "$backup_file" # Return the name of the backup file + fi + fi +} + # Create a dedicated php-fpm config final_path=$DESTDIR