From 43d9cafec7695e6b16301193b060ff8c34e2087d Mon Sep 17 00:00:00 2001 From: oleole39 Date: Mon, 23 Jan 2023 03:45:28 +0100 Subject: [PATCH] Fix various bugs in scripts; Revert path to _common.sh for backup & restore scripts --- scripts/_common.sh | 17 +++++++++-------- scripts/backup | 4 ++-- scripts/remove | 2 +- scripts/restore | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 910eb55..3baec23 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -83,11 +83,11 @@ ynh_psql_list_user_dbs() { if ynh_psql_user_exists --user=$db_user; then # Check that the db_user exists local sql="COPY (SELECT datname FROM pg_database JOIN pg_authid ON pg_database.datdba = pg_authid.oid WHERE rolname = '${db_user}') TO STDOUT" - local dbs_list=ynh_psql_execute_as_root --sql="$sql" # Fetch database(s) associated to role $db_user - return dbs_list + local dbs_list=$(ynh_psql_execute_as_root --sql="$sql") # Fetch database(s) associated to role $db_user as a string using space as delimiter (ex: "db1 db2 db3 db4") + echo "$dbs_list" else ynh_print_err --message="User \'$db_user\' does not exist" - return "" + echo "" fi } @@ -105,10 +105,11 @@ ynh_psql_remove_all_user_dbs() { # Manage arguments with getopts ynh_handle_getopts_args "$@" - local dbs_to_drop = ynh_psql_list_user_dbs $db_user + local dbs_to_drop=$(ynh_psql_list_user_dbs $db_user) if [ -n "$dbs_to_drop" ]; then # Check that the list of database(s) is not empty + local db_name - for $db_name in $dbs_to_drop # Iterate through the list of database(s) to remove + for db_name in $dbs_to_drop # Iterate through the list of database(s) to remove do if ynh_psql_database_exists --database=$db_name; then # Check if the database exists ynh_psql_drop_db $db_name # Remove the database @@ -138,11 +139,11 @@ ynh_psql_dump_all_user_dbs() { ynh_handle_getopts_args "$@" app="${app:-}" - local dbs_to_dump = ynh_psql_list_user_dbs $db_user + local dbs_to_dump=$(ynh_psql_list_user_dbs $db_user) if [ -n "$dbs_to_dump" ]; then # Check that the list of database(s) is not empty local db_name - for $db_name in $dbs_to_dump # Iterate through the list of database(s) to dump + for db_name in $dbs_to_dump # Iterate through the list of database(s) to dump do if ynh_psql_database_exists --database=$db_name; then # Check if the database exists ynh_psql_dump_db $db_name > "$app-$db_name-dump.sql" # Dump the database to a filename format of app-db_name-dump.sql, or of db_name-dump.sql if app parameter was not supplied @@ -187,7 +188,7 @@ ynh_psql_restore_all_app_dbs_dumps(){ local db_name db_name="${filename#${app}-}" # Remove "$app-" prefix from filename string to parse db_name. Will do nothing if there is no match. db_name="${db_name%-dump.sql}" # Remove "-dump.sql" suffix from filename string to parse db_name. Will do nothing if there is no match. - db_name=ynh_sanitize_dbid --db_name="$db_name" + db_name=$(ynh_sanitize_dbid --db_name="$db_name") if [[ "${filename#${app}-}" = "$filename" || -z "$db_name" ]] ; then # Check whether app_ID is included in filename OR $db_name is empty ynh_print_warn --message="File ignored: $filename. Filename not matching expected format (appID-db_name-dump.sql)" diff --git a/scripts/backup b/scripts/backup index ce699c0..7113df3 100755 --- a/scripts/backup +++ b/scripts/backup @@ -6,7 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -source _common.sh +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #================================================= @@ -55,7 +55,7 @@ ynh_backup --src_path="/etc/php/$phpversion/fpm/pool.d/$app.conf" #================================================= ynh_print_info --message="Backing up the PostgreSQL database..." -ynh_psql_dump_all_user_dbs --user=$db_user --app=$app +ynh_psql_dump_all_user_dbs --db_user=$db_user --app=$app #================================================= # END OF SCRIPT diff --git a/scripts/remove b/scripts/remove index 7a9598d..5f9d638 100755 --- a/scripts/remove +++ b/scripts/remove @@ -26,7 +26,7 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) ynh_script_progression --message="Removing all associated PostgreSQL database(s) and role" # Remove all existing databases associated with the app's dedicated user a database if it exists -ynh_psql_remove_all_user_dbs --user=$db_user; +ynh_psql_remove_all_user_dbs --db_user=$db_user; # Remove dedicated PostgreSQL role if ynh_psql_user_exists --user=$db_user; then diff --git a/scripts/restore b/scripts/restore index c2d722e..9513c37 100755 --- a/scripts/restore +++ b/scripts/restore @@ -6,7 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -source _common.sh +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #=================================================