#!/bin/bash

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

# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

ynh_print_info "Declaring files to be backed up..."

### N.B. : the following 'ynh_backup' calls are only a *declaration* of what needs
### to be backuped and not an actual copy of any file. The actual backup that
### creates and fills the archive with the files happens in the core after this
### script is called. Hence ynh_backups calls take basically 0 seconds to run.

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

ynh_backup "$install_dir"

#=================================================
# BACKUP THE DATA DIR
#=================================================

# Only relevant if there is a "data_dir" resource for this app
# NB: $data_dir is not backuped during safety-backup-before-upgrades,
# because the data dir may be huge and we don't want to just yolo-create a 10+ GB archive just for upgrades.
# On the other hand, $data_dir is also *not* removed by default in the "app remove" step unless --purge is used
# This means that even if the upgrade fails and the backup is restored, the data are still there.
ynh_backup "$data_dir"

#=================================================
# SYSTEM CONFIGURATION
#=================================================

# Backup the PHP-FPM configuration
ynh_backup "/etc/php/$php_version/fpm/pool.d/$app.conf"

# Backup the NGINX configuration
ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"

# Backup the systemd service unit
ynh_backup "/etc/systemd/system/$app.service"

# Backup the logrotate configuration
ynh_backup "/etc/logrotate.d/$app"

# Backup the Fail2Ban config
ynh_backup "/etc/fail2ban/jail.d/$app.conf"
ynh_backup "/etc/fail2ban/filter.d/$app.conf"

#=================================================
# BACKUP VARIOUS FILES
#=================================================

ynh_backup "/etc/cron.d/$app"

ynh_backup "/etc/$app/"

# NB: /var/log is not backuped during safety-backup-before-upgrades, same as $data_dir
ynh_backup "/var/log/$app/"

#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================
ynh_print_info "Backing up the MySQL database..."

### (However, things like MySQL dumps *do* take some time to run, though the
### copy of the generated dump to the archive still happens later)

ynh_psql_dump_db > db.sql

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

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