#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."

with_mysql=$(ynh_app_setting_get --app=$app --key=with_mysql)
with_sftp=$(ynh_app_setting_get --app=$app --key=with_sftp)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)

#=================================================
# DECLARE DATA AND CONF FILES TO BACKUP
#=================================================
ynh_print_info --message="Declaring files to be backed up..."

#=================================================
# BACKUP THE APP MAIN DIR
#=================================================

ynh_backup --src_path="$install_dir"

#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================

ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf"

#=================================================
# BACKUP THE PHP-FPM CONFIGURATION
#=================================================

if [ $phpversion != "none" ]
then
    ynh_backup --src_path="/etc/php/${phpversion}/fpm/pool.d/$app.conf"
fi

#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================

if [ $with_mysql -eq 1 ]
then
    ynh_print_info --message="Backing up the MySQL database..."
    ynh_mysql_dump_db --database="$db_name" > db.sql
fi

#=================================================
# END OF SCRIPT
#=================================================

ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)."