#!/bin/bash

# Source app helpers
source /usr/share/yunohost/helpers

# Récupère les infos de l'application.
app=$YNH_APP_INSTANCE_NAME

final_path=$(ynh_app_setting_get $app final_path)
domain=$(ynh_app_setting_get $app domain)
db_user=$(ynh_app_setting_get $app db_user)

# The parameter $1 is the backup directory location
# which will be compressed afterward
backup_dir=$1/apps/$app
sudo mkdir -p "$backup_dir"

# Backup sources & data
sudo cp -a $final_path/. $backup_dir/sources

# Copy Nginx and YunoHost parameters to make the script "standalone"
sudo cp -a /etc/yunohost/apps/$app/. $backup_dir/yunohost
sudo cp -a /etc/nginx/conf.d/$domain.d/$app.conf $backup_dir/nginx.conf

# Copy dedicated php-fpm process to backup folder
sudo cp -a /etc/php5/fpm/pool.d/$app.conf $backup_dir/php-fpm.conf

# Backup db
root_pwd=$(sudo cat /etc/yunohost/mysql)
sudo mysqldump -u root -p$root_pwd --no-create-db $db_user --result-file="$backup_dir/db.sql"