Skip to content

Commit

Permalink
Merge pull request #73 from CDuffers/export-tweaks
Browse files Browse the repository at this point in the history
Add hostname and error handling to the export process
  • Loading branch information
TrystanLea authored Nov 16, 2023
2 parents 7c00b96 + 779912b commit 3716012
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 138 deletions.
4 changes: 2 additions & 2 deletions backup-module/backup_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function backup_controller()

if ($route->action == "download") {
header("Content-type: application/zip");
$backup_filename="emoncms-backup-".date("Y-m-d").".tar.gz";
$backup_filename="emoncms-backup-".gethostname()."-".date("Y-m-d").".tar.gz";
header("Content-Disposition: attachment; filename=$backup_filename");
header("Pragma: no-cache");
header("Expires: 0");
Expand Down Expand Up @@ -120,7 +120,7 @@ function backup_controller()
if ((move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) && ($uploadOk == 1)) {

$redis->rpush("service-runner","$import_script $import_flag>$import_logfile");
header('Location: '.$path.'backup#import');
header('Location: '.$path.'backup#import-archive');
} else {
return "<br><div class='alert alert-error'><b>Error:</b> Import archive not selected</div>";
}
Expand Down
4 changes: 2 additions & 2 deletions backup-module/backup_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<br><br>
<pre id="export-log-bound" class="log"><div id="export-log"></div></pre>
<?php
$backup_filename="emoncms-backup-".date("Y-m-d").".tar.gz";
$backup_filename="emoncms-backup-".gethostname()."-".date("Y-m-d").".tar.gz";
if (file_exists($parsed_ini['backup_location']."/".$backup_filename) && !file_exists("/tmp/backuplock")) {
echo '<br><br><b>Right Click > Download:</b><br><a href="'.$path.'backup/download">'.$backup_filename.'</a>';
}
Expand Down Expand Up @@ -161,7 +161,7 @@ function export_log_update() {
$("#export-log").html(result);
document.getElementById("export-log-bound").scrollTop = document.getElementById("export-log-bound").scrollHeight

if (result.indexOf("=== Emoncms export complete! ===")!=-1) {
if (result.indexOf("=== Emoncms export complete! ===")!=-1 || result.indexOf("=== Emoncms export completed with ERRORS! ===")!=-1) {
clearInterval(export_updater);
}
}
Expand Down
188 changes: 94 additions & 94 deletions emoncms-export.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,79 @@
#!/bin/bash
script_location="`dirname $0`"

date=$(date +"%Y-%m-%d")
# Set the shell to trigger errors when commands within a pipe have a non-zero return code
set -o pipefail

# The errors variable is set when error_handler() is called, the variable is used by the finish() function for success or failure messages
errors=false

# This error handler function display information of what happened and where, but does NOT stop the script execution
error_handler() {
echo "Error: RC=$1 occurred on line $2"
errors=true
}

# Set trap for ERR to pass the return code and line number to error_handler()
trap 'error_handler $? $LINENO' ERR

# Exit handler used to ensure the exit message AJAX expects is found, whilst summarising if errors were found
# This also picks up the natural exit when reaching end of script

function finish() {
if [[ "${errors}" == "false" && $? == 0 ]]; then
echo "=== Emoncms export complete! ==="
# The strings output are identified in the interface to stop ongoing AJAX calls, please ammend in interface if changed here
else
echo "=== Emoncms export completed with ERRORS! ==="
# The strings output are identified in the interface to stop ongoing AJAX calls, please ammend in interface if changed here
fi
}

# Set trap for whenever EXIT is called to call finish()
trap finish EXIT

declare -a included not_found
script_location="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
config_location=${script_location}/config.cfg

echo "=== Emoncms export start ==="
date
echo "Backup module version:"
cat $script_location/backup-module/module.json | grep version
grep version "${script_location}/module.json"
echo "EUID: $EUID"
echo "Reading $script_location/config.cfg...."
if [ -f "$script_location/config.cfg" ]
echo "Reading ${config_location}...."

if [ ! -f "${config_location}" ]
then
source "$script_location/config.cfg"
echo "Location of databases: $database_path"
echo "Location of emonhub.conf: $emonhub_config_path"
echo "Location of Emoncms: $emoncms_location"
echo "Backup destination: $backup_location"
else
echo "ERROR: Backup $script_location/backup/config.cfg file does not exist"
echo "ERROR: Backup config file ${config_location} does not exist"
exit 1
sudo systemctl start feedwriter > /dev/null
fi

source "${config_location}"
echo "Location of databases: $database_path"
echo "Location of emonhub.conf: $emonhub_config_path"
echo "Location of Emoncms: $emoncms_location"
echo "Backup destination: $backup_location"

tar_filename="${backup_location}/emoncms-backup-$(hostname)-$(date +"%Y-%m-%d").tar"

module_location="${emoncms_location}/Modules/backup"
echo "emoncms backup module location $module_location"

#-----------------------------------------------------------------------------------------------
# Remove Old backup files
#-----------------------------------------------------------------------------------------------
if [ -f $backup_location/emoncms.sql ]
then
sudo rm $backup_location/emoncms.sql
fi

if [ -f $backup_location/emoncms-backup-$date.tar ]
then
sudo rm $backup_location/emoncms-backup-$date.tar
fi
for file in "${backup_location}/emoncms.sql" "${tar_filename}"
do
if [ -f "${file}" ]
then
sudo rm "${file}"
fi
done

#-----------------------------------------------------------------------------------------------
# Check emonPi / emonBase image version
#-----------------------------------------------------------------------------------------------
image_version=$(ls /boot | grep emonSD)
image_version=$(cd /boot && echo *emonSD* )
# Check first 16 characters of filename
image_date=${image_version:0:16}

Expand All @@ -63,8 +95,8 @@ fi
sudo systemctl stop feedwriter

# Get MYSQL authentication details from settings.php
if [ -f $script_location/get_emoncms_mysql_auth.php ]; then
auth=$(echo $emoncms_location | php $script_location/get_emoncms_mysql_auth.php php)
if [ -f "${script_location}/get_emoncms_mysql_auth.php" ]; then
auth=$(echo "${emoncms_location}" | php "${script_location}/get_emoncms_mysql_auth.php" php)
IFS=":" read username password database <<< "$auth"
else
echo "Error: cannot read MYSQL authentication details from Emoncms $script_location/get_emoncms_mysql_auth.php php & settings.php"
Expand All @@ -75,7 +107,7 @@ fi

# MYSQL Dump Emoncms database
if [ -n "$username" ]; then # if username string is not empty
mysqldump -u$username -p$password $database > $backup_location/emoncms.sql
mysqldump -u"${username}" -p"${password}" "${database}" > "${backup_location}/emoncms.sql"
if [ $? -ne 0 ]; then
echo "Error: failed to export mysql data"
echo "emoncms export failed"
Expand All @@ -88,85 +120,53 @@ else
exit 1
fi

if [ -f $backup_location/emoncms.sql ]
then
echo "-- adding $backup_location/emoncms.sql to archive --"
tar -c --file=$backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql --transform 's?.*/??g' 2>&1
else
echo "no file $backup_location/emoncms.sql"
fi

if [ -f $emonhub_config_path/emonhub.conf ]
then
echo "-- adding $emonhub_config_path/emonhub.conf to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar $emonhub_config_path/emonhub.conf --transform 's?.*/??g' 2>&1
else
echo "no file $emonhub_config_path/emonhub.conf"
fi

if [ -f $emoncms_location/settings.ini ]
then
echo "-- adding $emoncms_location/settings.ini to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar $emoncms_location/settings.ini --transform 's?.*/??g' 2>&1
else
echo "no file $emoncms_location/settings.ini"
fi

if [ -f $emoncms_location/settings.php ]
then
echo "-- adding $emoncms_location/settings.php to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar $emoncms_location/settings.php --transform 's?.*/??g' 2>&1
else
echo "no file $emoncms_location/settings.php"
fi
#
for file in "${backup_location}/emoncms.sql" "${emonhub_config_path}/emonhub.conf" "${emoncms_location}/settings.ini" "${emoncms_location}/settings.php"
do
if [ -f "${file}" ]
then
echo "-- adding ${file} to archive --"
tar -vr --file="${tar_filename}" "${file}" --transform 's?.*/??g' 2>&1
included+=("${file}")
else
echo "no ${file} to backup"
not_found+=("${file}")
fi
done

# Append database folder to the archive with absolute path
if [ -d $database_path/phpfina ]
then
echo "-- adding $database_path/phpfina to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar -C $database_path phpfina 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar phpfina"
fi
else
echo "no phpfina directory"
fi

if [ -d $database_path/phpfiwa ]
then
echo "-- adding $database_path/phpfiwa to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar -C $database_path phpfiwa 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar phpfiwa"
fi
else
echo "no phpfiwa directory"
fi
for dir in phpfina phpfiwa phptimeseries
do
if [ -d "${database_path}/${dir}" ]
then
echo "-- adding ${database_path}/${dir} to archive --"
tar -vr --file="${tar_filename}" -C "${database_path}" "${dir}" 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar ${dir}"
fi
included+=("${dir}")
else
echo "no ${database_path}/${dir} directory to backup"
not_found+=("${dir}")
fi
done

if [ -d $database_path/phptimeseries ]
then
echo "-- adding $database_path/phptimeseries to archive --"
tar -vr --file=$backup_location/emoncms-backup-$date.tar -C $database_path phptimeseries 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar phptimeseries"
fi
else
echo "no phptimeseries directory $database_path/phptimeseries"
fi
sudo systemctl start feedwriter > /dev/null

# Compress backup
echo "Compressing archive..."
gzip -fv $backup_location/emoncms-backup-$date.tar 2>&1
gzip -fv "${tar_filename}" 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to compress tar file"
echo "emoncms export failed"
sudo systemctl start feedwriter > /dev/null
exit 1
fi

sudo systemctl start feedwriter > /dev/null

echo "Backup saved: $backup_location/emoncms-backup-$date.tar.gz"
echo "Backup saved: ${tar_filename}.gz"
date
echo -e "\nBackup included components: ${included[*]}"
echo -e "INFO: These components couldn't be found to backup: ${not_found[*]}\n"

echo "Export finished...refresh page to view download link"
echo "=== Emoncms export complete! ===" # This string is identified in the interface to stop ongoing AJAX calls, please ammend in interface if changed here

# The exit trap will pickup the natural exit and display the string to stop ongoing AJAX calls
Loading

0 comments on commit 3716012

Please sign in to comment.