forked from flatsiedatsie/power-settings
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestore_backup_old.sh
More file actions
executable file
·329 lines (252 loc) · 12.4 KB
/
restore_backup_old.sh
File metadata and controls
executable file
·329 lines (252 loc) · 12.4 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#! /bin/bash
# This script tries to restore a backup
# It doesn't care about the RO system, since if only acts on the user data partition
BOOT_DIR="/boot"
if lsblk | grep -q $BOOT_DIR/firmware; then
#echo "firmware partition is mounted at $BOOT_DIR/firmware"
BOOT_DIR="$BOOT_DIR/firmware"
fi
# Restore the data directories
echo "" >> $BOOT_DIR/candle_log.txt
echo "$(date) - restoring Candle backup" >> $BOOT_DIR/candle_log.txt
echo "$(date) - restoring Candle backup" >> /home/pi/.webthings/candle.log
echo "$(date) - restoring Candle backup" >> /dev/kmsg
BIT_TYPE=$(getconf LONG_BIT)
echo "Bits: $BIT_TYPE"
ARCHSTRING=linux-arm
if [ $BIT_TYPE -eq 64 ]; then
ARCHSTRING=linux-arm64
fi
COUNTER=0
if [ ! -f /home/pi/.webthings/data/power-settings/candle_restore.tar ]; then
echo "Error, backup file to restore from is missing. Aborting."
echo "Error, backup file to restore from is missing. Aborting." >> $BOOT_DIR/candle_log.txt
echo "Candle: Backup restored, but downloading latest addons list failed. Did not restore addons." >> /dev/kmsg
echo "$(date) - Backup restored, but downloading latest addons list failed. Did not restore addons." >> $BOOT_DIR/candle_log.txt
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/error.png" ]; then
/bin/ply-image $BOOT_DIR/error.png
fi
sleep 7
else
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/splash_updating-0.png" ] && [ -f "$BOOT_DIR/splash_updating180-0.png" ]; then
if [ -e "$BOOT_DIR/rotate180.txt" ]; then
/bin/ply-image $BOOT_DIR/splash_updating180-0.png
else
/bin/ply-image $BOOT_DIR/splash_updating-0.png
fi
fi
sleep 1
echo "unpacking the backup file"
# unpack the backup file
tar -xf /home/pi/.webthings/data/power-settings/candle_restore.tar -C /home/pi/.webthings/
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/splash_updating-1.png" ] && [ -f "$BOOT_DIR/splash_updating180-1.png" ]; then
if [ -e "$BOOT_DIR/rotate180.txt" ]; then
/bin/ply-image $BOOT_DIR/splash_updating180-1.png
else
/bin/ply-image $BOOT_DIR/splash_updating-1.png
fi
fi
sleep 1
# Wait for IP address for at most 30 seconds
echo "Candle: late: waiting for IP address"
for i in {1..30}
do
#echo "current hostname: $(hostname -I)"
IPS=$(hostname -I | sed -r 's/192.168.12.1//' | xargs)
if [ "$IPS" = "" ]
then
echo "Candle: late.sh: no network yet $i" >> /dev/kmsg
echo "no network yet $i"
sleep 1
else
echo "Candle: late.sh: IP address detected: $(hostname -I)" >> /dev/kmsg
break
fi
done
if [ -f /home/pi/.webthings/log/logs.sqlite3 ]; then
mv /home/pi/.webthings/log/logs.sqlite3 /home/pi/.webthings/log/logs.sqlite3_bak
fi
#download additional splash images if they don't exist already
if [ ! -f "$BOOT_DIR/splash_updating-0.png" ] && [ -d $BOOT_DIR ]; then
echo "Downloading progress bar images"
wget https://www.candlesmarthome.com/tools/splash_updating-0.png -O $BOOT_DIR/splash_updating-0.png
wget https://www.candlesmarthome.com/tools/splash_updating-1.png -O $BOOT_DIR/splash_updating-1.png
wget https://www.candlesmarthome.com/tools/splash_updating-2.png -O $BOOT_DIR/splash_updating-2.png
wget https://www.candlesmarthome.com/tools/splash_updating-3.png -O $BOOT_DIR/splash_updating-3.png
wget https://www.candlesmarthome.com/tools/splash_updating-4.png -O $BOOT_DIR/splash_updating-4.png
wget https://www.candlesmarthome.com/tools/splash_updating180-0.png -O $BOOT_DIR/splash_updating180-0.png
wget https://www.candlesmarthome.com/tools/splash_updating180-1.png -O $BOOT_DIR/splash_updating180-1.png
wget https://www.candlesmarthome.com/tools/splash_updating180-2.png -O $BOOT_DIR/splash_updating180-2.png
wget https://www.candlesmarthome.com/tools/splash_updating180-3.png -O $BOOT_DIR/splash_updating180-3.png
wget https://www.candlesmarthome.com/tools/splash_updating180-4.png -O $BOOT_DIR/splash_updating180-4.png
wget -c https://www.candlesmarthome.com/tools/error.png -O $BOOT_DIR/error.png
echo "Downloading progress bar images done"
fi
cd /tmp
# Download latest list of available addons
echo "Downloading get_apps.json"
wget https://www.candlesmarthome.com/appstore/get_apps.json -O /tmp/addons.json
# Loop over data dir and compare with addons dir
if [ -f /tmp/addons.json ]; then
while IFS= read -r directory
do
#echo "checking $directory"
if [ -d "/home/pi/.webthings/addons/$directory" ]; then
echo "already installed: $directory"
echo "OK Addon is already installed: $directory" >> /dev/kmsg
else
echo "will attempt to download missing addon: $directory"
echo "Candle: will download missing addon: $directory" >> /dev/kmsg
let COUNTER=COUNTER+1
if [ -f missing.tar ]; then
echo "somehow missing.tar still existed. removing it."
rm missing.tar
fi
# get all potential download URL's
URLS=$(cat /tmp/addons.json | \
grep '"url"' | \
grep "/$directory" | \
sed -n 's/"url": *"\([^"]*\).*/\1/p' | \
sed 's/\\//g' )
echo "DOWNLOAD CANDIDATES:"
echo " $URLS"
if [[ $URLS == *"linux-arm64"* ]]; then
echo "linux-arm64 spotted"
if [ $BIT_TYPE -eq 64 ]; then
URLS=$(echo "$URLS" | grep "linux-arm64")
else
URLS=$(echo "$URLS" | grep -v "linux-arm64")
fi
fi
if [[ $URLS == *"v3.11."* ]]; then
echo "v3.11. spotted"
URLS=$(echo "$URLS" | grep "v3.11.")
elif [[ $URLS == *"v3.9."* ]]; then
echo "v3.9. spotted"
URLS=$(echo "$URLS" | grep "v3.9.")
fi
if [ -n "$URLS" ]; then
echo "selecting first URL from remaining list:"
URLS=`echo "${URLS}" | head -1`
echo "$URLS"
wget $URLS -O missing.tar
else
echo "Did not find a download URL - does addon exist?"
fi
if [ -f missing.tar ]; then
echo "extracting tar..."
tar -xf missing.tar
if [ -d package ]; then
echo "moving extracted package into place"
mv ./package "/home/pi/.webthings/addons/$directory"
chown -R pi:pi "/home/pi/.webthings/addons/$directory"
chmod -R 0755 "/home/pi/.webthings/addons/$directory"
echo "Candle: succesfully downloaded missing addon: $directory" >> /dev/kmsg
echo "Candle: restoring backup: succesfully downloaded addon: $directory" >> $BOOT_DIR/candle_log.txt
fi
rm missing.tar
else
echo "Candle: restoring backup: download of missing addon failed: $directory" >> /dev/kmsg
echo "Candle: restoring backup: download of missing addon failed: $directory" >> $BOOT_DIR/candle_log.txt
fi
if [ "$COUNTER" == 3 ]; then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/splash_updating-2.png" ] && [ -f "$BOOT_DIR/splash_updating180-2.png" ]; then
if [ -e "$BOOT_DIR/rotate180.txt" ]; then
/bin/ply-image $BOOT_DIR/splash_updating180-2.png
else
/bin/ply-image $BOOT_DIR/splash_updating-2.png
fi
fi
sleep 1
fi
if [ "$COUNTER" == 7 ]; then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/splash_updating-3.png" ] && [ -f "$BOOT_DIR/splash_updating180-3.png" ]; then
if [ -e "$BOOT_DIR/rotate180.txt" ]; then
/bin/ply-image $BOOT_DIR/splash_updating180-3.png
else
/bin/ply-image $BOOT_DIR/splash_updating-3.png
fi
fi
sleep 1
fi
fi
done < <(ls /home/pi/.webthings/data)
# clean up the candle_backuped.txt files, which were necessary to make sure that all /data folders were added to the backup file, even if they were empty before
find /home/pi/.webthings/data -maxdepth 1 -type d -exec sh -c 'rm $1/candle_backuped.txt' _ {} \;
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/splash_updating-4.png" ] && [ -f "$BOOT_DIR/splash_updating180-4.png" ]; then
if [ -e "$BOOT_DIR/rotate180.txt" ]; then
/bin/ply-image $BOOT_DIR/splash_updating180-4.png
else
/bin/ply-image $BOOT_DIR/splash_updating-4.png
fi
fi
sleep 1
else
echo "Candle: Backup restored, but downloading latest addons list failed. Did not restore addons." >> /dev/kmsg
echo "$(date) - Backup restored, but downloading latest addons list failed. Did not restore addons." >> $BOOT_DIR/candle_log.txt
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "$BOOT_DIR/error.png" ]; then
/bin/ply-image $BOOT_DIR/error.png
fi
sleep 7
fi
fi
# if the backup didn't restore an entire log file, then put the initial version back, and see if there is logs_meta data to use instead
if [ ! -f /home/pi/.webthings/log/logs.sqlite3 ]; then
if [ -f /home/pi/.webthings/log/logs.sqlite3_bak ]; then
mv /home/pi/.webthings/log/logs.sqlite3_bak /home/pi/.webthings/log/logs.sqlite3
if [ -f /home/pi/.webthings/data/power-settings/logs_meta.sqlite3 ]; then
#log_meta = run_command("sqlite3 " + str(self.log_db_file_path) + " 'SELECT id, descr, maxAge FROM metricIds'")
echo "restoring logs from meta data"
sqlite3 /home/pi/.webthings/log/logs.sqlite3 'DELETE FROM metricIds;'
sqlite3 /home/pi/.webthings/log/logs.sqlite3 'DELETE FROM metricsBoolean;'
sqlite3 /home/pi/.webthings/log/logs.sqlite3 'DELETE FROM metricsNumber;'
sqlite3 /home/pi/.webthings/log/logs.sqlite3 'DELETE FROM metricsOther;'
LINES=$(cat /home/pi/.webthings/data/power-settings/logs_meta.sqlite3)
echo
echo "LINES: "
echo $LINES
echo
IFS='
'
COUNT=0
for LINE in $LINES
do
COUNT=$((COUNT+1))
echo "$COUNT -> $LINE"
if [[ "$LINE" == *\|* ]]; then
echo "Line seems valid"
ID=$(echo $LINE | cut -d '|' -f 1)
DESCR=$(echo $LINE | cut -d '|' -f 2)
MAXAGE=$(echo $LINE | cut -d '|' -f 3)
echo "ID: $ID"
echo "DESCR: $DESCR"
echo "MAXAGE: $MAXAGE"
if [ -n "$ID" ] && [ -n "$DESCR" ] && [ -n "MAXAGE" ]; then
#sqlite3 /home/pi/.webthings/log/logs.sqlite3 'INSERT INTO metricIds (id, descr, maxAge) VALUES ($ID, $DESCR, $MAXAGE);'
echo "Candle: restore_backup.sh: adding log: $DESCR" >> /dev/kmsg
(printf "INSERT INTO metricIds VALUES ('%s', '%s', '%s');" $ID $DESCR $MAXAGE) | sqlite3 /home/pi/.webthings/log/logs.sqlite3
else
echo "A VALUE WAS EMPTY, NOT WRITING TO LOGS DB!"
fi
else
echo "Log meta line did not seem valid!"
fi
done
fi
else
echo "Candle: ERROR, /home/pi/.webthings/log/logs.sqlite3 file seems to be missing!"
echo "Candle: ERROR, /home/pi/.webthings/log/logs.sqlite3 file seems to be missing!" >> /dev/kmsg
fi
else
if [ -f /home/pi/.webthings/log/logs.sqlite3_bak ]; then
rm /home/pi/.webthings/log/logs.sqlite3_bak
fi
if [ -f /home/pi/.webthings/data/power-settings/logs_meta.sqlite3 ]; then
rm /home/pi/.webthings/data/power-settings/logs_meta.sqlite3
fi
fi
# Clean up
rm $BOOT_DIR/bootup_actions.sh
rm $BOOT_DIR/bootup_actions_failed.sh
#sudo systemctl start webthings-gateway.service
exit 1