From fb3f5c218de82e4c33453982ca370bb74e8ab1d2 Mon Sep 17 00:00:00 2001 From: Alexandre CUER Date: Fri, 7 Feb 2025 10:40:48 +0100 Subject: [PATCH 1/3] adapt shell scripts so they also work in container --- emoncms-copy.sh | 9 ++- emoncms-export.sh | 26 +++++++-- emoncms-import.sh | 137 ++++++++++++++++++++++++++++++---------------- 3 files changed, 115 insertions(+), 57 deletions(-) diff --git a/emoncms-copy.sh b/emoncms-copy.sh index 0b807ce..7dfaff4 100755 --- a/emoncms-copy.sh +++ b/emoncms-copy.sh @@ -51,8 +51,9 @@ else fi #----------------------------------------------------------------------------------------------- - -sudo systemctl stop feedwriter +if [ ! -f "/.dockerenv" ]; then + sudo systemctl stop feedwriter +fi # Get MYSQL authentication details from settings.php if [ -f /home/pi/backup/get_emoncms_mysql_auth.php ]; then @@ -85,7 +86,9 @@ cp /home/pi/data/node-red/settings.js $backup_location cp --verbose -r /home/pi/data/phpfina/. $backup_location/phpfina cp --verbose -r /home/pi/data/phptimeseries/. $backup_location/phptimeseries -sudo systemctl start feedwriter > /dev/null +if [ ! -f "/.dockerenv" ]; then + sudo systemctl start feedwriter > /dev/null +fi date echo "=== Emoncms export complete! ===" # This string is identified in the interface to stop ongoing AJAX calls, please ammend in interface if changed here diff --git a/emoncms-export.sh b/emoncms-export.sh index 0b20192..f7ccba7 100755 --- a/emoncms-export.sh +++ b/emoncms-export.sh @@ -66,7 +66,11 @@ for file in "${backup_location}/emoncms.sql" "${tar_filename}" do if [ -f "${file}" ] then - sudo rm "${file}" + if [ -f "/.dockerenv" ]; then + rm "${file}" + else + sudo rm "${file}" + fi fi done @@ -92,7 +96,9 @@ fi #----------------------------------------------------------------------------------------------- # Disabled in @borphin commit? -sudo systemctl stop feedwriter +if [ ! -f "/.dockerenv" ]; then + sudo systemctl stop feedwriter +fi # Get MYSQL authentication details from settings.php if [ -f "${script_location}/get_emoncms_mysql_auth.php" ]; then @@ -101,7 +107,9 @@ if [ -f "${script_location}/get_emoncms_mysql_auth.php" ]; then else echo "Error: cannot read MYSQL authentication details from Emoncms $script_location/get_emoncms_mysql_auth.php php & settings.php" echo "$PWD" - sudo systemctl start feedwriter > /dev/null + if [ ! -f "/.dockerenv" ]; then + sudo systemctl start feedwriter > /dev/null + fi exit 1 fi @@ -111,12 +119,16 @@ if [ -n "$username" ]; then # if username string is not empty if [ $? -ne 0 ]; then echo "Error: failed to export mysql data" echo "emoncms export failed" - sudo systemctl start feedwriter > /dev/null + if [ ! -f "/.dockerenv" ]; then + sudo systemctl start feedwriter > /dev/null + fi exit 1 fi else echo "Error: Cannot read MYSQL authentication details from Emoncms settings.php" - sudo systemctl start feedwriter > /dev/null + if [ ! -f "/.dockerenv" ]; then + sudo systemctl start feedwriter > /dev/null + fi exit 1 fi @@ -151,7 +163,9 @@ do fi done -sudo systemctl start feedwriter > /dev/null +if [ ! -f "/.dockerenv" ]; then + sudo systemctl start feedwriter > /dev/null +fi # Compress backup echo "Compressing archive..." diff --git a/emoncms-import.sh b/emoncms-import.sh index a5ba544..b737b1e 100755 --- a/emoncms-import.sh +++ b/emoncms-import.sh @@ -82,49 +82,65 @@ fi echo "Decompressing backup.." import_location="${backup_location}/import" if [ -d "${import_location}" ]; then - sudo rm -rf "${import_location}" - # Belt and braces cleanup in case any '.' prefixed files exist + if [ -f "/.dockerenv" ]; then + rm -rf "${import_location}" + else + sudo rm -rf "${import_location}" + fi + # Belt and braces cleanup in case any '.' prefixed files exist fi mkdir "${import_location}" -sudo chown "${user}" "${import_location}" +if [ ! -f "/.dockerenv" ]; then + sudo chown "${user}" "${import_location}" +fi tar xfzv "${backup_filename}" -C "${import_location}" 2>&1 if [ $? -ne 0 ]; then echo "Error: failed to decompress backup" echo "${backup_filename} has not been removed for diagnotics" echo "Removing files in ${import_location}" - sudo rm -Rf "${import_location}/*" - echo "Import failed" - exit 1 + if [ -f "/.dockerenv" ]; then + rm -Rf "${import_location}/*" + else + sudo rm -Rf "${import_location}/*" + fi + echo "Import failed" + exit 1 fi echo "Removing compressed backup to save disk space.." -sudo rm "${backup_filename}" +if [ -f "/.dockerenv" ]; then + rm "${backup_filename}" +else + sudo rm "${backup_filename}" +fi if [ -n "$password" ] then # if username sring is not empty if [ -f "${import_location}/emoncms.sql" ]; then - echo "Stopping services.." - if [[ $emonhub == "loaded" ]]; then - sudo systemctl stop emonhub - fi - if [[ $feedwriter == "loaded" ]]; then - sudo systemctl stop feedwriter - fi - if [[ $mqtt_input == "loaded" ]]; then - sudo systemctl stop mqtt_input - fi - if [[ $emoncms_mqtt == "loaded" ]]; then - sudo systemctl stop emoncms_mqtt + if [ ! -f "/.dockerenv" ]; then + echo "Stopping services.." + if [[ $emonhub == "loaded" ]]; then + sudo systemctl stop emonhub + fi + if [[ $feedwriter == "loaded" ]]; then + sudo systemctl stop feedwriter + fi + if [[ $mqtt_input == "loaded" ]]; then + sudo systemctl stop mqtt_input + fi + if [[ $emoncms_mqtt == "loaded" ]]; then + sudo systemctl stop emoncms_mqtt + fi fi echo "Emoncms MYSQL database import..." mysql -u"${username}" -p"${password}" "${database}" < "${import_location}/emoncms.sql" - if [ $? -ne 0 ]; then - echo "Error: failed to import mysql data" - echo "Import failed" - exit 1 - fi + if [ $? -ne 0 ]; then + echo "Error: failed to import mysql data" + echo "Import failed" + exit 1 + fi else "Error: cannot find emoncms.sql database to import" exit 1 @@ -135,28 +151,49 @@ else fi echo "Import feed meta data.." -sudo rm -rf "${database_path}"/{phpfina,phptimeseries} 2> /dev/null +if [ -f "/.dockerenv" ]; then + rm -rf "${database_path}"/{phpfina,phptimeseries} 2> /dev/null +else + sudo rm -rf "${database_path}"/{phpfina,phptimeseries} 2> /dev/null +fi echo "Restore phpfina and phptimeseries data folders..." if [ -d "${import_location}/phpfina" ]; then - sudo mv "${import_location}/phpfina" "${database_path}" - sudo chown -R www-data:root "${database_path}/phpfina" + if [ -f "/.dockerenv" ]; then + mv "${import_location}/phpfina" "${database_path}" + else + sudo mv "${import_location}/phpfina" "${database_path}" + sudo chown -R www-data:root "${database_path}/phpfina" + fi fi if [ -d "${import_location}/phptimeseries" ]; then - sudo mv "${import_location}/phptimeseries" "${database_path}" - sudo chown -R www-data:root "${database_path}/phptimeseries" + if [ -f "/.dockerenv" ]; then + mv "${import_location}/phptimeseries" "${database_path}" + else + sudo mv "${import_location}/phptimeseries" "${database_path}" + sudo chown -R www-data:root "${database_path}/phptimeseries" + fi fi # cleanup -sudo rm "${import_location}/emoncms.sql" +if [ -f "/.dockerenv" ]; then + rm "${import_location}/emoncms.sql" +else + sudo rm "${import_location}/emoncms.sql" +fi # Save previous config settings as old.emonhub.conf if [ -f "${import_location}/emonhub.conf" ]; then if [ -d "${emonhub_config_path}" ]; then echo "Import emonhub.conf > ${emonhub_config_path}/emohub.conf" - sudo mv "${import_location}/emonhub.conf" "${emonhub_config_path}/emonhub.conf" - sudo chmod 666 "${emonhub_config_path}/emonhub.conf" + if [ -f "/.dockerenv" ]; then + mv "${import_location}/emonhub.conf" "${emonhub_config_path}/emonhub.conf" + chmod 666 "${emonhub_config_path}/emonhub.conf" + else + sudo mv "${import_location}/emonhub.conf" "${emonhub_config_path}/emonhub.conf" + sudo chmod 666 "${emonhub_config_path}/emonhub.conf" + fi else echo "WARNING: emonhub.conf found in backup, but no emonHub directory (${emonhub_config_path}) found" fi @@ -182,24 +219,28 @@ if [ -f /opt/openenergymonitor/EmonScripts/common/emoncmsdbupdate.php ]; then fi # Restart services -if [[ $emonhub == "loaded" ]]; then - echo "Restarting emonhub..." - sudo systemctl start emonhub -fi -if [[ $feedwriter == "loaded" ]]; then - echo "Restarting feedwriter..." - sudo systemctl start feedwriter -fi -if [[ $mqtt_input == "loaded" ]]; then - echo "Restarting mqtt_input..." - sudo systemctl start mqtt_input -fi -if [[ $emoncms_mqtt == "loaded" ]]; then - echo "Restarting emoncms_mqtt..." - sudo systemctl start emoncms_mqtt +if [ ! -f "/.dockerenv" ]; then + if [[ $emonhub == "loaded" ]]; then + echo "Restarting emonhub..." + sudo systemctl start emonhub + fi + if [[ $feedwriter == "loaded" ]]; then + echo "Restarting feedwriter..." + sudo systemctl start feedwriter + fi + if [[ $mqtt_input == "loaded" ]]; then + echo "Restarting mqtt_input..." + sudo systemctl start mqtt_input + fi + if [[ $emoncms_mqtt == "loaded" ]]; then + echo "Restarting emoncms_mqtt..." + sudo systemctl start emoncms_mqtt + fi fi date +"%Y-%m-%d-%T" # This string is identified in the interface to stop ongoing AJAX calls in logger window, please ammend in interface if changed here echo "=== Emoncms import complete! ===" -sudo systemctl restart apache2 +if [ ! -f "/.dockerenv" ]; then + sudo systemctl restart apache2 +fi From 8803d8641ac79d7cfd0d6bf4617246bc1ccfd6d0 Mon Sep 17 00:00:00 2001 From: Alexandre CUER Date: Fri, 7 Feb 2025 11:36:12 +0100 Subject: [PATCH 2/3] Adapt shell scripts so they also work in container - next --- emoncms-import.sh | 48 ++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/emoncms-import.sh b/emoncms-import.sh index b737b1e..df0abbc 100755 --- a/emoncms-import.sh +++ b/emoncms-import.sh @@ -24,38 +24,44 @@ fi echo "Starting import from $backup_source_path to $backup_location..." -emonhub=$(systemctl show emonhub | grep LoadState | cut -d"=" -f2) -feedwriter=$(systemctl show feedwriter | grep LoadState | cut -d"=" -f2) -mqtt_input=$(systemctl show mqtt_input | grep LoadState | cut -d"=" -f2) -emoncms_mqtt=$(systemctl show emoncms_mqtt | grep LoadState | cut -d"=" -f2) +if [ ! -f "/.dockerenv" ]; then + emonhub=$(systemctl show emonhub | grep LoadState | cut -d"=" -f2) + feedwriter=$(systemctl show feedwriter | grep LoadState | cut -d"=" -f2) + mqtt_input=$(systemctl show mqtt_input | grep LoadState | cut -d"=" -f2) + emoncms_mqtt=$(systemctl show emoncms_mqtt | grep LoadState | cut -d"=" -f2) +fi #----------------------------------------------------------------------------------------------- # Check emonPi / emonBase image version #----------------------------------------------------------------------------------------------- -image_version=$(ls /boot | grep emonSD) -# Check first 16 characters of filename -image_date=${image_version:0:16} -if [[ "${image_version:0:6}" == "emonSD" ]] -then - echo "Image version: $image_version" -fi - -# Very old images (the ones shipped with kickstarter campaign) have "emonpi-28May2015" -if [[ -z $image_version ]] || [[ "$image_date" == "emonSD-17Jun2015" ]] -then - image="old" - echo "$image image" +if [ -f "/.dockerenv" ]; then + image_version="container" else - image="new" - echo "$image image" + image_version=$(ls /boot | grep emonSD) + # Check first 16 characters of filename + image_date=${image_version:0:16} + if [[ "${image_version:0:6}" == "emonSD" ]] + then + echo "Image version: $image_version" + fi + + # Very old images (the ones shipped with kickstarter campaign) have "emonpi-28May2015" + if [[ -z $image_version ]] || [[ "$image_date" == "emonSD-17Jun2015" ]] + then + image="old" + echo "$image image" + else + image="new" + echo "$image image" + fi fi #----------------------------------------------------------------------------------------------- # Get latest backup filename if [ ! -d "${backup_source_path}" ]; then - echo "Error: ${backup_source_path} does not exist, nothing to import" - exit 1 + echo "Error: ${backup_source_path} does not exist, nothing to import" + exit 1 fi backup_filename=$(ls -t "${backup_source_path}"/*.tar.gz | head -1) From 92d57d8e48299bd4cef74d5638ed406638554d3a Mon Sep 17 00:00:00 2001 From: Alexandre CUER Date: Fri, 7 Feb 2025 13:16:03 +0100 Subject: [PATCH 3/3] Adapt shell scripts so they also work in container - final --- emoncms-copy.sh | 34 +++++++++++++++++++--------------- emoncms-export.sh | 34 +++++++++++++++++++--------------- emoncms-import.sh | 4 ++-- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/emoncms-copy.sh b/emoncms-copy.sh index 7dfaff4..9217503 100755 --- a/emoncms-copy.sh +++ b/emoncms-copy.sh @@ -31,23 +31,27 @@ backup_location="/home/pi/usbdisk/backup" #----------------------------------------------------------------------------------------------- -# Check emonPi / emonBase image version +# Check container / emonPi / emonBase image version #----------------------------------------------------------------------------------------------- -image_version=$(ls /boot | grep emonSD) -# Check first 16 characters of filename -image_date=${image_version:0:16} - -if [[ "${image_version:0:6}" == "emonSD" ]] -then - echo "Image version: $image_version" -fi - -# Detect if SD card image verion, used to restore the correct emonhub.conf -if [[ "$image_date" == "emonSD-17Jun2015" ]] -then - image="old" +if [ -f "/.dockerenv" ]; then + echo "running in container" else - image="new" + image_version=$(ls /boot | grep emonSD) + # Check first 16 characters of filename + image_date=${image_version:0:16} + + if [[ "${image_version:0:6}" == "emonSD" ]] + then + echo "Image version: $image_version" + fi + + # Detect if SD card image verion, used to restore the correct emonhub.conf + if [[ "$image_date" == "emonSD-17Jun2015" ]] + then + image="old" + else + image="new" + fi fi #----------------------------------------------------------------------------------------------- diff --git a/emoncms-export.sh b/emoncms-export.sh index f7ccba7..6423e8a 100755 --- a/emoncms-export.sh +++ b/emoncms-export.sh @@ -75,27 +75,31 @@ do done #----------------------------------------------------------------------------------------------- -# Check emonPi / emonBase image version +# Check container / emonPi / emonBase image version #----------------------------------------------------------------------------------------------- -image_version=$(cd /boot && echo *emonSD* ) -# Check first 16 characters of filename -image_date=${image_version:0:16} +if [ -f "/.dockerenv" ]; then + echo "running in container" +else + image_version=$(cd /boot && echo *emonSD* ) + # Check first 16 characters of filename + image_date=${image_version:0:16} -if [[ "${image_version:0:6}" == "emonSD" ]] -then - echo "Image version: $image_version" -fi + if [[ "${image_version:0:6}" == "emonSD" ]] + then + echo "Image version: $image_version" + fi -# Very old images (the ones shipped with kickstarter campaign) have "emonpi-28May2015" -if [[ -z $image_version ]] || [[ "$image_date" == "emonSD-17Jun2015" ]] -then - image="old" -else - image="new" + # Very old images (the ones shipped with kickstarter campaign) have "emonpi-28May2015" + if [[ -z $image_version ]] || [[ "$image_date" == "emonSD-17Jun2015" ]] + then + image="old" + else + image="new" + fi fi #----------------------------------------------------------------------------------------------- -# Disabled in @borphin commit? +# Disabled in @borpin commit? if [ ! -f "/.dockerenv" ]; then sudo systemctl stop feedwriter fi diff --git a/emoncms-import.sh b/emoncms-import.sh index df0abbc..34ce7c4 100755 --- a/emoncms-import.sh +++ b/emoncms-import.sh @@ -32,10 +32,10 @@ if [ ! -f "/.dockerenv" ]; then fi #----------------------------------------------------------------------------------------------- -# Check emonPi / emonBase image version +# Check container / emonPi / emonBase image version #----------------------------------------------------------------------------------------------- if [ -f "/.dockerenv" ]; then - image_version="container" + echo "running in container" else image_version=$(ls /boot | grep emonSD) # Check first 16 characters of filename