diff --git a/scripts/backup b/scripts/backup index 15f6e80..8dd3e4a 100644 --- a/scripts/backup +++ b/scripts/backup @@ -8,6 +8,7 @@ #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_mongo_db source /usr/share/yunohost/helpers #================================================= @@ -61,7 +62,7 @@ ynh_backup --src_path="/etc/systemd/system/$app.service" #================================================= ynh_print_info --message="Backing up the MongoDB database..." -mongodump --quiet --db="$db_name" --out="./dump" +ynh_mongo_dump_db --database="$db_name" > ./dump.bson #================================================= # END OF SCRIPT diff --git a/scripts/install b/scripts/install index e0d60b2..7e947e8 100755 --- a/scripts/install +++ b/scripts/install @@ -9,6 +9,7 @@ source _common.sh source ynh_detect_arch__2 source ynh_add_config +source ynh_mongo_db source /usr/share/yunohost/helpers #================================================= @@ -97,7 +98,9 @@ systemctl start $mongodb_servicename # Registering db name db_name=$(ynh_sanitize_dbid --db_name=$app) +db_user=$db_name ynh_app_setting_set --app=$app --key=db_name --value=$db_name +ynh_mongo_setup_db --db_user=$db_user --db_name=$db_name #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE diff --git a/scripts/remove b/scripts/remove index 2c0e2ea..6781b33 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_mongo_db source /usr/share/yunohost/helpers #================================================= @@ -49,7 +50,7 @@ ynh_remove_systemd_config ynh_script_progression --message="Removing the MongoDB database..." # Remove a database if it exists, along with the associated user -mongo $db_name --eval "db.dropDatabase()" +ynh_mongo_remove_db --db_user=$db_user --db_name=$db_name #================================================= # REMOVE DEPENDENCIES diff --git a/scripts/restore b/scripts/restore index 53252a9..ee64abd 100644 --- a/scripts/restore +++ b/scripts/restore @@ -8,6 +8,7 @@ #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_mongo_db source /usr/share/yunohost/helpers #================================================= @@ -31,6 +32,7 @@ domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) db_name=$(ynh_app_setting_get --app=$app --key=db_name) +db_user=$db_name #================================================= # CHECK IF THE APP CAN BE RESTORED @@ -103,7 +105,9 @@ ynh_script_progression --message="Restoring the MongoDB database..." # Start mongodb systemctl enable $mongodb_servicename systemctl start $mongodb_servicename -mongorestore --quiet --db="$db_name" "./dump/$db_name" +db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) +ynh_mongo_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd +ynh_mongo_restore_db --database="$db_name" < ./dump.bson #================================================= # RESTORE SYSTEMD diff --git a/scripts/ynh_mongo_db b/scripts/ynh_mongo_db new file mode 100644 index 0000000..a3d0b80 --- /dev/null +++ b/scripts/ynh_mongo_db @@ -0,0 +1,246 @@ +#!/bin/bash + +# Evaluate a mongo command +# +# example: ynh_mongo_eval_as --command='db.getMongo().getDBNames().indexOf("wekan")' +# example: ynh_mongo_eval_as --command="db.getMongo().getDBNames().indexOf(\"wekan\")" +# +# usage: ynh_mongo_eval_as [--user=user] [--password=password] [--database=database] [--host=host] [--port=port] --command="command" +# | arg: -u, --user= - the user name to connect as +# | arg: -p, --password= - the user password +# | arg: -d, --database= - the database to connect to +# | arg: -h, --host= - the host to connect to +# | arg: -P, --port= - the port to connect to +# | arg: -c, --command= - the command to evaluate +# +# +ynh_mongo_eval_as() { + # Declare an array to define the options of this helper. + local legacy_args=updhPc + local -A args_array=( [u]=user= [p]=password= [d]=database= [h]=host= [P]=port= [c]=command= ) + local user + local password + local database + local host + local port + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + user="${user:-}" + password="${password:-}" + database="${database:-}" + host="${host:-}" + port="${port:-}" + + # If user is provided + if [ -n "$user" ] + then + user="--username=$user" + + # If password is provided + if [ -n "$password" ] + then + password="--password=$password" + fi + + # If database is provided + if [ -n "$database" ] + then + database="--authenticationDatabase=$database" + fi + else + password="" + database="" + fi + + # If host is provided + if [ -n "$host" ] + then + host="--host=$host" + fi + + # If host is provided + if [ -n "$port" ] + then + port="--port=$port" + fi + + mongo --quiet $user $password $database $host $port --eval="$command" +} + +# Create a database and grant optionnaly privilegies to a user +# +# [internal] +# +# usage: ynh_mongo_create_db db_name [db_user] +# | arg: db_name - the database name to create +# | arg: db_user - the user to grant privilegies +# +# +ynh_mongo_create_db() { + local db_name=$1 + local db_user=$2 + + # Set the user as admin of the db + ynh_mongo_eval_as --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);' +} + +# Drop a database +# +# [internal] +# +# If you intend to drop the database *and* the associated user, +# consider using ynh_mongo_remove_db instead. +# +# usage: ynh_mongo_drop_db db +# | arg: db - the database name to drop +# +# +ynh_mongo_drop_db() { + ynh_mongo_eval_as --database="${1}" --command='db.runCommand({dropDatabase: 1})' +} + +# Dump a database +# +# example: ynh_mongo_dump_db --database=wekan > ./dump.bson +# +# usage: ynh_mongo_dump_db --database=database +# | arg: -d, --database= - the database name to dump +# | ret: the mongodump output +# +# +ynh_mongo_dump_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongodump --quiet --db="$database" --archive +} + +# Create a user +# +# [internal] +# +# usage: ynh_mongo_create_user user pwd [host] +# | arg: user - the user name to create +# | arg: pwd - the password to identify user by +# +# +ynh_mongo_create_user() { + ynh_mongo_eval_as --command='db.createUser( { user: "'${1}'", pwd: "'${2}'", roles: [ "readWrite" ] } );' +} + +# Check if a mongo database exists +# +# usage: ynh_mongo_database_exists --database=database +# | arg: -d, --database= - the database for which to check existence +# | exit: Return 1 if the database doesn't exist, 0 otherwise +# +# +ynh_mongo_database_exists() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=([d]=database=) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if [ $(ynh_mongo_eval_as --command='db.getMongo().getDBNames().indexOf("'${database}'")') -lt 0 ] + then + return 0 + else + return 1 + fi +} + +# Restore a database +# +# example: ynh_mongo_restore_db --database=wekan < ./dump.bson +# +# usage: ynh_mongo_restore_db --database=database +# | arg: -d, --database= - the database name to restore +# +# +ynh_mongo_restore_db() { + # Declare an array to define the options of this helper. + local legacy_args=d + local -A args_array=( [d]=database= ) + local database + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + mongorestore --quiet --db="$database" --archive +} + +# Drop a user +# +# [internal] +# +# usage: ynh_mongo_drop_user user +# | arg: user - the user name to drop +# +# +ynh_mongo_drop_user() { + ynh_mongo_eval_as --command='db.dropUser("'${1}'", {w: "majority", wtimeout: 5000})' +} + +# Create a database, an user and its password. Then store the password in the app's config +# +# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd] +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated +# +# After executing this helper, the password of the created database will be available in $db_pwd +# It will also be stored as "mongopwd" into the app settings. +# +# +ynh_mongo_setup_db() { + # Declare an array to define the options of this helper. + local legacy_args=unp + local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) + local db_user + local db_name + local db_pwd="" + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local new_db_pwd=$(ynh_string_random) # Generate a random password + # If $db_pwd is not provided, use new_db_pwd instead for db_pwd + db_pwd="${db_pwd:-$new_db_pwd}" + + ynh_mongo_create_user "$db_user" "$db_pwd" # Create the user + + ynh_mongo_create_db "$db_name" "$db_user" # Create the database + ynh_app_setting_set --app=$app --key=mongopwd --value=$db_pwd # Store the password in the app's config +} + +# Remove a database if it exists, and the associated user +# +# usage: ynh_mongo_remove_db --db_user=user --db_name=name +# | arg: -u, --db_user= - Owner of the database +# | arg: -n, --db_name= - Name of the database +# +# +ynh_mongo_remove_db() { + # Declare an array to define the options of this helper. + local legacy_args=un + local -A args_array=( [u]=db_user= [n]=db_name= ) + local db_user + local db_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_mongo_database_exists --database=$db_name + then # Check if the database exists + ynh_mongo_drop_db $db_name # Remove the database + else + ynh_print_warn --message="Database $db_name not found" + fi + + # Remove mongo user if it exists + ynh_mongo_drop_user $db_user +}