From 10968400dfee31a2967305ced64cea64736e4b58 Mon Sep 17 00:00:00 2001 From: Krakinou Date: Sun, 16 Dec 2018 17:54:14 +0100 Subject: [PATCH] backup & restore script - init --- .../metadata.db => conf/metadata.db.empty | Bin scripts/backup | 21 +++++--- scripts/install | 14 ++++-- scripts/restore | 46 ++++++++++++------ scripts/upgrade | 41 +++++----------- 5 files changed, 68 insertions(+), 54 deletions(-) rename src/calibre/metadata.db => conf/metadata.db.empty (100%) diff --git a/src/calibre/metadata.db b/conf/metadata.db.empty similarity index 100% rename from src/calibre/metadata.db rename to conf/metadata.db.empty diff --git a/scripts/backup b/scripts/backup index 91625dc..c9de6cd 100755 --- a/scripts/backup +++ b/scripts/backup @@ -13,10 +13,6 @@ source /usr/share/yunohost/helpers # MANAGE SCRIPT FAILURE #================================================= -ynh_clean_setup () { - ### Remove this function if there's nothing to clean before calling the remove script. - true -} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -27,8 +23,9 @@ ynh_abort_if_errors app=$YNH_APP_INSTANCE_NAME final_path=$(ynh_app_setting_get $app final_path) +path_url=$(ynh_app_setting_get $app path) domain=$(ynh_app_setting_get $app domain) -db_name=$(ynh_app_setting_get $app db_name) +calibre_dir=$(ynh_app_setting_get $app calibre_dir) #================================================= # STANDARD BACKUP STEPS @@ -59,8 +56,18 @@ ynh_backup "/etc/logrotate.d/$app" ynh_backup "/etc/systemd/system/$app.service" + #================================================= -# BACKUP A CRON FILE +# BACKUP THE DATA DIRECTORY #================================================= -ynh_backup "/etc/cron.d/$app" +backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) +# If backup_core_only have any value in the settings.yml file, do not backup the data directory +if [ -z $backup_core_only ] +then + ynh_backup "calibre_dir" +else + echo "Data dir will not be saved, because backup_core_only is set." >&2 +fi + + diff --git a/scripts/install b/scripts/install index 1475d83..c9d3b9b 100755 --- a/scripts/install +++ b/scripts/install @@ -130,13 +130,12 @@ if [ ! -e "$calibre_dir" ]; then fi #Check if metadata.db file exists. If not create it (empty library) if [ ! -e "$calibre_dir"/metadata.db ]; then - cp -a ../src/calibre/. $calibre_dir + cp -a ../conf/metadata.db.empty $calibre_dir/metadata.db chown $app:$app $calibre_dir/* fi - # Set permissions to app files -chown -R $app:$app $final_path +chown -R root: $final_path #================================================= @@ -153,6 +152,7 @@ chown -R $app:$app /var/log/$app #================================================= # SET SQLITE DATABASE SETTINGS #================================================= + ynh_print_info "Setting up database and settings" #we need to start and stop the service so that initial app.db file is created and that we can set default data systemctl start $app @@ -173,6 +173,14 @@ sqlite3 $final_path/app.db "UPDATE user SET password='$(python ../conf/generate_ ynh_print_ON + +#================================================= +# STORE THE CHECKSUM OF THE CONFIG FILE +#================================================= + +# Calculate and store the config file checksum into the app settings +ynh_store_file_checksum "${final_path}/app.db" + #================================================= # ADVERTISE SERVICE IN ADMIN PANEL #================================================= diff --git a/scripts/restore b/scripts/restore index 4de9d79..1adbb31 100755 --- a/scripts/restore +++ b/scripts/restore @@ -13,10 +13,6 @@ source /usr/share/yunohost/helpers # MANAGE SCRIPT FAILURE #================================================= -ynh_clean_setup () { - #### Remove this function if there's nothing to clean before calling the remove script. - true -} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -29,7 +25,7 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get $app domain) path_url=$(ynh_app_setting_get $app path) final_path=$(ynh_app_setting_get $app final_path) -db_name=$(ynh_app_setting_get $app db_name) +calibre_dir=$(ynh_app_setting_get $app calibre_dir) #================================================= # CHECK IF THE APP CAN BE RESTORED @@ -67,6 +63,7 @@ ynh_system_user_create $app # Restore permissions on app files chown -R root: $final_path +chown calibreweb: $final_path/app.db #================================================= # SPECIFIC RESTORATION @@ -75,7 +72,7 @@ chown -R root: $final_path #================================================= # Define and install dependencies -ynh_install_app_dependencies deb1 deb2 +ynh_install_app_dependencies $pkg_dependencies #================================================= # ADVERTISE SERVICE IN ADMIN PANEL @@ -90,11 +87,6 @@ yunohost service add $app --log "/var/log/$app/APP.log" ynh_restore_file "/etc/systemd/system/$app.service" systemctl enable $app.service -#================================================= -# RESTORE THE CRON FILE -#================================================= - -ynh_restore_file "/etc/cron.d/$app" #================================================= # RESTORE THE LOGROTATE CONFIGURATION @@ -103,10 +95,34 @@ ynh_restore_file "/etc/cron.d/$app" ynh_restore_file "/etc/logrotate.d/$app" #================================================= -# GENERIC FINALIZATION -#================================================= -# RELOAD NGINX AND PHP-FPM +# RESTORE THE DATA DIRECTORY +#================================================= + + +# The data directory will be restored only if it exists in the backup archive +# So only if it was backup previously. +if [ -d "$YNH_BACKUP_DIR/apps/$app/backup/$calibre_dir" ] +then + ynh_restore_file "$calibre_dir" +else + if [ ! -e "$calibre_dir" ]; then + ynh_print_info "Create calibre library folder $calibre_dir" + mkdir -p $calibre_dir + chown -R $app:$app $calibre_dir + fi +#Check if metadata.db file exists. If not create it (empty library) + if [ ! -e "$calibre_dir"/metadata.db ]; then + cp -a ../conf/metadata.db.empty $calibre_dir/metadata.db + chown $app:$app $calibre_dir/* + fi +fi +# Remove the option backup_core_only if it's in the settings.yml file +ynh_app_setting_delete $app backup_core_only + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX #================================================= -systemctl reload php5-fpm systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index 2c4b518..3f5a906 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -21,32 +21,8 @@ admin=$(ynh_app_setting_get $app admin) is_public=$(ynh_app_setting_get $app is_public) final_path=$(ynh_app_setting_get $app final_path) language=$(ynh_app_setting_get $app language) -db_name=$(ynh_app_setting_get $app db_name) +calibre_dir=$(ynh_app_setting_get $app calibre_dir) -#================================================= -# ENSURE DOWNWARD COMPATIBILITY -#================================================= - -# Fix is_public as a boolean value -if [ "$is_public" = "Yes" ]; then - ynh_app_setting_set $app is_public 1 - is_public=1 -elif [ "$is_public" = "No" ]; then - ynh_app_setting_set $app is_public 0 - is_public=0 -fi - -# If db_name doesn't exist, create it -if [ -z $db_name ]; then - db_name=$(ynh_sanitize_dbid $app) - ynh_app_setting_set $app db_name $db_name -fi - -# If final_path doesn't exist, create it -if [ -z $final_path ]; then - final_path=/var/www/$app - ynh_app_setting_set $app final_path $final_path -fi #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP @@ -75,7 +51,12 @@ path_url=$(ynh_normalize_url_path $path_url) #================================================= # Download, check integrity, uncompress and patch the source from app.src -ynh_setup_source "$final_path" +##as long as v1 is not reached, sha256sum will change without notice so not usable, so we use cp -a instead. +#source are directly in the app so far +#ynh_setup_source "$final_path" + +cp -a ../src/calibreweb/. $final_path + #================================================= # NGINX CONFIGURATION @@ -88,7 +69,8 @@ ynh_add_nginx_config # UPGRADE DEPENDENCIES #================================================= -ynh_install_app_dependencies deb1 deb2 +ynh_install_app_dependencies $pkg_dependencies +pip install --target $final_path/vendor -r $final_path/requirements.txt #================================================= # CREATE DEDICATED USER @@ -106,9 +88,9 @@ ynh_system_user_create $app ### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. -ynh_backup_if_checksum_is_different "$final_path/CONFIG_FILE" +ynh_backup_if_checksum_is_different "$final_path/app.db" # Recalculate and store the checksum of the file for the next upgrade. -ynh_store_file_checksum "$final_path/CONFIG_FILE" +ynh_store_file_checksum "$final_path/app.db" #================================================= # SETUP LOGROTATE @@ -132,6 +114,7 @@ ynh_add_systemd_config # Set permissions on app files chown -R root: $final_path +chown -R $app: $final_path/app.db #================================================= # SETUP SSOWAT