Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial changes to remove hardcoded paths #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.project
.env
.vscode/*
backup/config.cfg
14 changes: 8 additions & 6 deletions backup/backup_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ function backup_controller()
global $route, $session, $path;
$result = false;

$parsed_ini = parse_ini_file("Modules/backup/config.cfg", true);

$export_flag = "/tmp/emoncms-flag-export";
$export_script = "/home/pi/backup/emoncms-export.sh";
$export_logfile = "/home/pi/data/emoncms-export.log";
$export_script = $parsed_ini['backup_script_location']."/emoncms-export.sh";
$export_logfile = $parsed_ini['backup_location']."/emoncms-export.log";

$import_flag = "/tmp/emoncms-flag-import";
$import_script = "/home/pi/backup/emoncms-import.sh";
$import_logfile = "/home/pi/data/emoncms-import.log";
$import_script = $parsed_ini['backup_script_location']."/emoncms-import.sh";
$import_logfile = $parsed_ini['backup_location']."/emoncms-import.log";

// This module is only to be ran by the admin user
if (!$session['write'] && !$session['admin']) return array('content'=>false);
Expand Down Expand Up @@ -64,7 +66,7 @@ function backup_controller()
header("Content-Disposition: attachment; filename=$backup_filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile("/home/pi/data/$backup_filename");
readfile($parsed_ini['backup_location']."/".$backup_filename);
exit;
}

Expand All @@ -73,7 +75,7 @@ function backup_controller()
// ini_set('upload_max_filesize', '200M');
// ini_set('post_max_size', '200M');
$uploadOk = 1;
$target_path = "/home/pi/data/uploads/";
$target_path = $parsed_ini['backup_location']."/uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);

$imageFileType = pathinfo($target_path,PATHINFO_EXTENSION);
Expand Down
27 changes: 0 additions & 27 deletions config.cfg

This file was deleted.

16 changes: 9 additions & 7 deletions default.config.cfg
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# Emoncms Export / Import scripts config file
# Emoncms Export and Import scripts config file

## NON-EMONPI DEFAULT CONFIG ##

# These settings are used for both the export & import scripts.
# These settings are used for both the export and import scripts.
# To export from one location and import into another
# Used by both PHP and Bash and comments are not ignored

backup_script_location="~/backup"

# Directory location of Emoncms
emoncms_location="/var/www/emoncms"

# Destination location of exported backup .tar.gz - Must be writable
backup_location="~/data"

# location of mysql database
mysql_path="~/data"
# location of phpfina phpfiwa and phptimeseries databases
database_path="/var/lib"

# Location of emonhub.conf (leave blank if backup not required)
# Location of emonhub.conf - leave blank if backup not required
emonhub_config_path="~/etc/emonhub"

# Location of specimen emonhub config
emonhub_specimen_config="~/emonhub/conf"

# Location of emoncms.conf (leave blank if backup not required)
# Location of emoncms.conf - leave blank if backup not required
emoncms_config_path="/var/www/emoncms"

# Location of compressed backup data to import (.tar.gz)
# Location of compressed backup data to import .tar.gz
backup_source_path="~/uploads"
115 changes: 85 additions & 30 deletions emoncms-export.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#!/bin/bash
script_location="`dirname $0`"

date=$(date +"%Y-%m-%d")

echo "=== Emoncms export start ==="
date
echo "EUID: $EUID"
echo "Reading /home/pi/backup/config.cfg...."
if [ -f /home/pi/backup/config.cfg ]
echo "Reading $script_location/backup/config.cfg...."
if [ -f "$script_location/backup/config.cfg" ]
then
source /home/pi/backup/config.cfg
echo "Location of mysql database: $mysql_path"
source "$script_location/backup/config.cfg"
echo "Location of databases: $database_path"
echo "Location of emonhub.conf: $emonhub_config_path"
echo "Location of emoncms.conf: $emoncms_config_path"
echo "Location of Emoncms: $emoncms_location"
echo "Backup destination: $backup_location"
else
echo "ERROR: Backup /home/pi/backup/config.cfg file does not exist"
echo "ERROR: Backup $script_location/backup/config.cfg file does not exist"
exit 1
fi

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

#-----------------------------------------------------------------------------------------------
# Remove Old backup files
#-----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -58,14 +62,14 @@ fi



sudo service feedwriter stop
# sudo service feedwriter stop

# Get MYSQL authentication details from settings.php
if [ -f /home/pi/backup/get_emoncms_mysql_auth.php ]; then
auth=$(echo $emoncms_location | php /home/pi/backup/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 <<< "$auth"
else
echo "Error: cannot read MYSQL authentication details from Emoncms settings.php"
echo "Error: cannot read MYSQL authentication details from Emoncms $script_location/get_emoncms_mysql_auth.php php & settings.php"
echo "$PWD"
exit 1
fi
Expand All @@ -86,34 +90,85 @@ fi

echo "Emoncms MYSQL database dump complete, adding files to archive .."

if [ $image="old" ]; then
if [ -f $backup_location/emoncms.sql ]
then
tar --append --file=$backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql
else
echo "no file $backup_location/emoncms.sql"
fi

if [ -f $emonhub_config_path/emonhub.conf ]
then
tar --append --file=$backup_location/emoncms-backup-$date.tar $emonhub_config_path/emonhub.conf
else
echo "no file $emonhub_config_path/emonhub.conf"
fi

if [ -f $emoncms_config_path/emoncms.conf ]
then
tar --append --file=$backup_location/emoncms-backup-$date.tar $emoncms_config_path/emoncms.conf
else
echo "no file $emoncms_config_path/emoncms.conf"
fi

if [ -f $emoncms_location/settings.php ]
then
tar --append --file=$backup_location/emoncms-backup-$date.tar $emoncms_location/settings.php
else
echo "no file $emoncms_location/settings.php"
fi

#if [ $image="old" ]; then
# Create backup archive and add config files stripping out the path
# Old image = don't backup nodeRED config (since nodeRED doesnot exist)
tar -cf $backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql $emonhub_config_path/emonhub.conf $emoncms_config_path/emoncms.conf $emoncms_location/settings.php --transform 's?.*/??g' 2>&1
# tar -cf $backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql $emoncms_config_path/emoncms.conf $emoncms_location/settings.php --transform 's?.*/??g' 2>&1
# if [ $? -ne 0 ]; then
# echo "Error: failed to tar config data"
# echo "emoncms export failed"
# exit 1
# fi
#fi

#if [ $image="new" ]; then
# Create backup archive and add config files stripping out the path
# New image = backup NodeRED
# tar -cf $backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql $emonhub_config_path/emonhub.conf $emoncms_config_path/emoncms.conf $emoncms_location/settings.php /home/pi/data/node-red/flows_emonpi.json /home/pi/data/node-red/flows_emonpi_cred.json /home/pi/data/node-red/settings.js --transform 's?.*/??g' 2>&1
# if [ $? -ne 0 ]; then
# echo "Error: failed to tar config data"
# echo "emoncms export failed"
# exit 1
# fi
#fi

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

if [ $image="new" ]; then
# Create backup archive and add config files stripping out the path
# New image = backup NodeRED
tar -cf $backup_location/emoncms-backup-$date.tar $backup_location/emoncms.sql $emonhub_config_path/emonhub.conf $emoncms_config_path/emoncms.conf $emoncms_location/settings.php /home/pi/data/node-red/flows_emonpi.json /home/pi/data/node-red/flows_emonpi_cred.json /home/pi/data/node-red/settings.js --transform 's?.*/??g' 2>&1
if [ -d $database_path/phpfiwa ]
then
tar --append --file=$backup_location/emoncms-backup-$date.tar -C $database_path phpfiwa 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar config data"
echo "emoncms export failed"
exit 1
echo "Error: failed to tar phpfiwa"
fi
else
echo "no phpfiwa directory"
fi

# Append database folder to the archive with absolute path
tar --append --file=$backup_location/emoncms-backup-$date.tar -C $mysql_path phpfina phptimeseries 2>&1
if [ $? -ne 0 ]; then
echo "Error: failed to tar mysql dump and data"
echo "emoncms export failed"
exit 1
if [ -d $database_path/phptimeseries ]
then
tar --append --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

# Compress backup
Expand All @@ -125,9 +180,9 @@ if [ $? -ne 0 ]; then
exit 1
fi

sudo service feedwriter start > /dev/null

echo "Backup saved: $backup_location/emoncms-backup-$date.tar.gz"
#sudo service feedwriter start > /dev/null
echo "Contents of Backup: $backup_location/emoncms-backup-$date.tar.gz"
tar -tvf $backup_location/emoncms-backup-2018-02-26.tar.gz
date
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
27 changes: 13 additions & 14 deletions emoncms-import.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#!/bin/bash

backup_source_path="/home/pi/data/uploads"
data_path="/home/pi/data"
script_location="`dirname $0`"

echo "=== Emoncms import start ==="
date +"%Y-%m-%d-%T"
echo "EUID: $EUID"
echo "Reading /home/pi/backup/config.cfg...."
if [ -f /home/pi/backup/config.cfg ]
echo "Reading $script_location/backup/config.cfg...."
if [ -f "$script_location/backup/config.cfg" ]
then
source /home/pi/backup/config.cfg
echo "Location of mysql database: $mysql_path"
source "$script_location/backup/config.cfg"
echo "Location of data databases: $database_path"
echo "Location of emonhub.conf: $emonhub_config_path"
echo "Location of emoncms.conf: $emoncms_config_path"
echo "Location of Emoncms: $emoncms_location"
echo "Backup destination: $backup_location"
echo "Backup source path: $backup_source_path"
else
echo "ERROR: Backup /home/pi/backup/config.cfg file does not exist"
echo "ERROR: Backup $script_location/backup/config.cfg file does not exist"
exit 1
fi

Expand Down Expand Up @@ -64,8 +63,8 @@ fi
echo "Backup found: $backup_filename starting import.."

echo "Read MYSQL authentication details from settings.php"
if [ -f /home/pi/backup/get_emoncms_mysql_auth.php ]; then
auth=$(echo $emoncms_location | php /home/pi/backup/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 <<< "$auth"
else
echo "Error: cannot read MYSQL authentication details from Emoncms settings.php"
Expand Down Expand Up @@ -120,17 +119,17 @@ else
fi

echo "Import feed meta data.."
sudo rm -rf $mysql_path/{phpfina,phptimeseries} 2> /dev/null
sudo rm -rf $database_path/{phpfina,phptimeseries} 2> /dev/null

echo "Restore phpfina and phptimeseries data folders..."
if [ -d $backup_location/import/phpfina ]; then
sudo mv $backup_location/import/phpfina $mysql_path
sudo chown -R www-data:root $mysql_path/phpfina
sudo mv $backup_location/import/phpfina $database_path
sudo chown -R www-data:root $database_path/phpfina
fi

if [ -d $backup_location/import/phptimeseries ]; then
sudo mv $backup_location/import/phptimeseries $mysql_path
sudo chown -R www-data:root $mysql_path/phptimeseries
sudo mv $backup_location/import/phptimeseries $database_path
sudo chown -R www-data:root $database_path/phptimeseries
fi

# cleanup
Expand Down
2 changes: 2 additions & 0 deletions emonpi.default.config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# These settings are used for both the export & import scripts.
# To export from one location and import into another

backup_script_location="/home/pi/backup"

# Directory location of Emoncms
emoncms_location="/var/www/emoncms"

Expand Down
5 changes: 2 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ Follow on screen prompts, RasPi will shutdown when process is compleate. It can

# Emoncms Module Install

Install this module in your home folder then symlink the sub-folder called backup to your emoncms Modules directory:
Install this module within your home folder or a folder of your choice then symlink the sub-folder called backup to your emoncms Modules directory (assuming your emoncms folder is in the usual place). From within the parent folder you choose (such as ~/)

cd ~/
git clone https://github.com/emoncms/backup.git
ln -s /home/pi/backup/backup/ /var/www/emoncms/Modules/backup
ln -s $PWD/backup/ /var/www/emoncms/Modules/backup

**Note: Ensure you are running the latest version of Emoncms on the Stable branch. [A change was merged on the 9th Feb 16 to Emoncms core](https://github.com/emoncms/emoncms/commit/e83ad78e6155275d7537104367b8d44ef63d78fe) that enables symlinked modules which is essential for backup module to appear in Emoncms**

Expand Down