localdbs_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")
for db_name in $dbs_to_drop# Iterate through the list of database(s) to remove. Note: this would fail in case databases names would contain space character or characters such as "*". IFS parsing method would then be required.
for db_name in $dbs_to_dump# Iterate through the list of database(s) to dump. Note: this would fail in case databases names would contain space character or characters such as "*". IFS parsing method would then be required.
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
# | arg: -u, --db_user= - the PostgreSQL role/user which will own the restored databases. If not existing, it will be created.
# | arg: -p, --db_user_pwd= - the password associated to the PostgreSQL role/user which will own the databases. If not existing, it will be generated and saved to the app's config file.
#
# SQL dump files to restore must be named according to this format "app_id-db_name-dump.sql" and be located in the app folder
# The filename format requirement is made so to match the files dumped with ynh_psql_dump_all_user_dbs --user=user --app=app (with both parameters specified).
#
# Requires YunoHost version 2.7.13 or higher.
ynh_psql_restore_all_app_dbs_dumps(){
# Declare an array to define the options of this helper.
locallegacy_args=up
local -A args_array=([u]=db_user=[p]=db_user_pwd=)
local db_user
local db_user_pwd
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if[ -z "$app"];then
ynh_die --message="No global app_ID variable defined in the script"
fi
ynh_psql_test_if_first_run # Make sure PSQL is installed
local filename
for filename in *-dump.sql # Loop among all files ending with "-dump.sql" in the current folder
do
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.
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)"
continue
else
db_user_pwd="${db_user_pwd:-}"
if[ -z "$db_user_pwd"];then
db_user_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)# Try to retrieve db_user_pwd from the app's settings. It may prove empty during the first loop, but will get populated before the second loop by ynh_psql_setup_db() below.
fi
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_user_pwd# Check that the db_user exists or create it generating a random password and then create an empty database named $db_name.
ynh_psql_execute_file_as_root --file="./${filename}" --database=$db_name# Restore the dabatase from the corresponding dump file
ynh_print_info --message="Restored database $db_name, owned by PostgreSQL user $db_user"