-
Notifications
You must be signed in to change notification settings - Fork 15
/
emoncms-import.sh
executable file
·205 lines (178 loc) · 7.01 KB
/
emoncms-import.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
script_location="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
config_location=${script_location}/config.cfg
echo "=== Emoncms import start ==="
date +"%Y-%m-%d-%T"
echo "Backup module version:"
grep version "${script_location}/module.json"
echo "EUID: $EUID"
echo "Reading ${config_location}...."
if [ -f "${config_location}" ]
then
source "${config_location}"
echo "Location of data databases: $database_path"
echo "Location of emonhub.conf: $emonhub_config_path"
echo "Location of Emoncms: $emoncms_location"
echo "Backup destination: $backup_location"
echo "Backup source path: $backup_source_path"
else
echo "ERROR: Backup config file ${config_location} does not exist"
exit 1
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)
#-----------------------------------------------------------------------------------------------
# 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"
else
image="new"
echo "$image image"
fi
#-----------------------------------------------------------------------------------------------
# Get latest backup filename
if [ ! -d "${backup_source_path}" ]; then
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)
if [[ -z "${backup_filename}" ]] #if backup does not exist (empty filename string)
then
echo "Error: cannot find backup, stopping import"
exit 1
fi
# if backup exists
echo "Backup found: ${backup_filename} starting import.."
echo "Read 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)
IFS=":" read username password database <<< "$auth"
else
echo "Error: cannot read MYSQL authentication details from Emoncms settings.php"
echo "$PWD"
exit 1
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
fi
mkdir "${import_location}"
sudo chown "${user}" "${import_location}"
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
fi
echo "Removing compressed backup to save disk space.."
sudo rm "${backup_filename}"
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
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
else
"Error: cannot find emoncms.sql database to import"
exit 1
fi
else
echo "Error: cannot read MYSQL authentication details from Emoncms settings.php"
exit 1
fi
echo "Import feed meta data.."
sudo rm -rf "${database_path}"/{phpfina,phptimeseries} 2> /dev/null
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"
fi
if [ -d "${import_location}/phptimeseries" ]; then
sudo mv "${import_location}/phptimeseries" "${database_path}"
sudo chown -R www-data:root "${database_path}/phptimeseries"
fi
# cleanup
sudo rm "${import_location}/emoncms.sql"
# 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"
else
echo "WARNING: emonhub.conf found in backup, but no emonHub directory (${emonhub_config_path}) found"
fi
fi
# Start with blank emonhub.conf
# if [[ $image == "old" ]]
# then # Legacy image use emonhub.conf without MQTT authenitication
# echo "Start with fresh config: copy LEGACY default.emonhub.conf:"
# echo "cp $emonhub_specimen_config/old.default.emonhub.conf $emonhub_config_path/emonhub.conf"
# cp $emonhub_specimen_config/old.default.emonhub.conf $emonhub_config_path/emonhub.conf
# else # Newer Feb15+ image use latest emonhub.conf with MQTT node variable topic structure and MQTT authentication enabled
# echo "Start with fresh config: copy NEW default emonpi.emonhub.conf:"
# echo "cp $emonhub_specimen_config/emonpi.default.emonhub.conf $emonhub_config_path/emonhub.conf"
# cp $emonhub_specimen_config/emonpi.default.emonhub.conf $emonhub_config_path/emonhub.conf
# fi
redis-cli "flushall" 2>&1
if [ -f /opt/openenergymonitor/EmonScripts/common/emoncmsdbupdate.php ]; then
echo "Updating Emoncms Database.."
php /opt/openenergymonitor/EmonScripts/common/emoncmsdbupdate.php
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
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