From 3390389acc58546124b4978bf97442d646ac9c2e Mon Sep 17 00:00:00 2001 From: anmol26s Date: Fri, 15 Dec 2017 04:23:33 +0530 Subject: [PATCH] Added backup and restore --- README.md | 4 +- scripts/_common.sh | 32 +----------- scripts/backup | 78 ++++++++++++++++++++++++++++ scripts/psql.sh | 25 +++++++-- scripts/restore | 123 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 37 deletions(-) create mode 100644 scripts/backup create mode 100644 scripts/restore diff --git a/README.md b/README.md index 00cc798..b99673d 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ integrated system to create personalised learning environments. Moodle is widely used around the world by universities, schools, companies and all manner of organisations and individuals. -#Installation +# Installation ** After the installation go to your domain https://domain.tld/moodle and create the admin account. ** ## To-do - [X] Install script - [X] Remove script - [ ] Upgrade script -- [ ] Backup and Restore scripts +- [X] Backup and Restore scripts(need testing) - [ ] LDAP integration diff --git a/scripts/_common.sh b/scripts/_common.sh index 26dea4d..b4a95fa 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -33,7 +33,7 @@ ynh_remove_php7 () { } -# Create a dedicated php-fpm config +# Create a dedicated php-fpm config for php7.1 # # usage: ynh_add_fpm_config ynh_add_php7.1-fpm_config () { @@ -58,11 +58,8 @@ ynh_add_php7.1-fpm_config () { sudo systemctl reload php7.1-fpm } -<<<<<<< HEAD + # Remove the dedicated php-fpm config for php7.1 -======= -# Remove the dedicated php-fpm config ->>>>>>> 4d1446000580d39209c48e91f1e764e3bb677881 # # usage: ynh_remove_fpm_config ynh_remove_php7.1-fpm_config () { @@ -70,28 +67,3 @@ ynh_remove_php7.1-fpm_config () { ynh_secure_remove "/etc/php/7.1/fpm/conf.d/20-$app.ini" 2>&1 sudo systemctl reload php7.1-fpm } -<<<<<<< HEAD - -ynh_install_php7 () { - - ynh_package_update - ynh_package_install apt-transport-https --no-install-recommends - - wget -q -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg - echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.list - - ynh_package_update - ynh_install_app_dependencies php7.1 php7.1-zip php7.1-fpm php7.1-mysql php7.1-xml php7.1-intl php7.1-mbstring php7.1-gd php7.1-curl php7.1-soap php7.1-pgsql - sudo update-alternatives --install /usr/bin/php php /usr/bin/php5 70 - sudo systemctl reload php7.1-fpm - -} - -ynh_remove_php7 () { - sudo rm -f /etc/apt/sources.list.d/php7.list - sudo apt-key del 4096R/89DF5277 - sudo apt-key del 2048R/11A06851 - ynh_remove_app_dependencies php7.1 php7.1-zip php7.1-fpm php7.1-mysql php7.1-xml php7.1-intl php7.1-mbstring php7.1-gd php7.1-curl php7.1-soap php7.1-pgsql -} -======= ->>>>>>> 4d1446000580d39209c48e91f1e764e3bb677881 diff --git a/scripts/backup b/scripts/backup new file mode 100644 index 0000000..92b2cdf --- /dev/null +++ b/scripts/backup @@ -0,0 +1,78 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source psql.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +final_path=$(ynh_app_setting_get $app final_path) +domain=$(ynh_app_setting_get $app domain) +db_name=$(ynh_app_setting_get $app psql_db) + +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= + +ynh_backup "$final_path" +# BACKUP APP home directory +ynh_backup "/home/yunohost.app/$app" + +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + +ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# BACKUP THE PHP-FPM CONFIGURATION +#================================================= + +ynh_backup "/etc/php/7.1/fpm/pool.d/$app.conf" +ynh_backup "/etc/php/7.1/fpm/conf.d/20-$app.ini" + +#================================================= +# BACKUP THE PSQL DATABASE +#================================================= + +ynh_psql_dump_db "$db_name" > db.sql +ynh_backup "db.sql" + + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP LOGROTATE +#================================================= + +ynh_backup "/etc/logrotate.d/$app" + +#================================================= +# BACKUP THE CRON FILE +#================================================= + +ynh_backup "/etc/cron.d/$app" diff --git a/scripts/psql.sh b/scripts/psql.sh index a1ac1ce..419d300 100644 --- a/scripts/psql.sh +++ b/scripts/psql.sh @@ -33,7 +33,9 @@ ynh_psql_execute_as_root () { # | arg: file - the file containing SQL commands # | arg: db - the database to connect to ynh_psql_execute_file_as_root() { - ynh_die "ynh_psql_execute_file_as_root is not yet implemented" + file="$1" + db="$2" + su -c "psql $db" postgres < "$file" } # Create a database and grant optionnaly privilegies to a user @@ -70,7 +72,8 @@ ynh_psql_drop_db() { # | arg: db - the database name to dump # | ret: the psqldump output ynh_psql_dump_db() { - ynh_die "ynh_psql_dump_db is not yet implemented" + db="$1" +su --command="pg_dump \"${db}\"" postgres } @@ -97,13 +100,25 @@ ynh_psql_test_if_first_run() { then echo "PostgreSQL is already installed, no need to create master password" else - local pgsql=$(ynh_string_random) + pgsql=$(ynh_string_random) + pg_hba="" echo "$pgsql" >> /etc/yunohost/psql + + if [ -e /etc/postgresql/9.4/ ] + then + pg_hba=/etc/postgresql/9.4/main/pg_hba.conf + elif [ -e /etc/postgresql/9.6/ ] + then + pg_hba=/etc/postgresql/9.6/main/pg_hba.conf + else + ynh_die "postgresql shoud be 9.4 or 9.6" + fi + systemctl start postgresql - sudo -u postgres psql -c"ALTER user postgres WITH PASSWORD '${pgsql}'" + su --command="psql -c\"ALTER user postgres WITH PASSWORD '${pgsql}'\"" postgres # we can't use peer since YunoHost create users with nologin sed -i '/local\s*all\s*all\s*peer/i \ - local all all password' /etc/postgresql/9.4/main/pg_hba.conf + local all all password' "$pg_hba" systemctl enable postgresql systemctl reload postgresql fi diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..8f236c5 --- /dev/null +++ b/scripts/restore @@ -0,0 +1,123 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source psql.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +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 psql_db) +db_pwd=$(ynh_app_setting_get $app psqlpwd) + +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= + +ynh_webpath_available $domain $path_url \ + || ynh_die "Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= + +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +var_root=/home/yunohost.app/$app +ynh_restore_file "$final_path" +ynh_restore_file "$var_root" + +#================================================= +# RESTORE THE psql DATABASE +#================================================= +ynh_psql_test_if_first_run +ynh_psql_create_user $app $db_pwd +ynh_psql_execute_as_root \ +"CREATE DATABASE $db_name ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER $app;" +ynh_psql_execute_file_as_root ./db.sql "$db_name" + +#================================================= +# RECREATE THE DEDICATED USER +#================================================= + +# Create the dedicated user (if not existing) +ynh_system_user_create $app + +#================================================= +# RESTORE USER RIGHTS +#================================================= + +# Restore permissions on app files +chown -R $app: $final_path +chown -R root: $final_path/config.php +mkdir -p $var_root +chown -R $app:root $var_root +chmod -R 700 $var_root + +#================================================= +# RESTORE THE PHP-FPM CONFIGURATION +#================================================= + +ynh_restore_file "/etc/php/7.1/pool.d/$app.conf" +ynh_restore_file "/etc/php/7.1/conf.d/20-$app.ini" + +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= + +# Define and install dependencies +ynh_install_php7 + +#================================================= +# RESTORE THE CRON FILE +#================================================= + +ynh_restore_file "/etc/cron.d/$app" + +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= + +ynh_restore_file "/etc/logrotate.d/$app" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= + +systemctl reload php7.1-fpm +systemctl reload nginx