forked from flatsiedatsie/power-settings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestore_backup.sh
235 lines (182 loc) · 9.13 KB
/
restore_backup.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
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
#! /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
# Restore the data directories
echo "" >> /boot/candle_log.txt
echo "$(date) - restoring Candle backup" >> /boot/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/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/candle_log.txt
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/error.png" ]; then
/bin/ply-image /boot/error.png
fi
sleep 7
else
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/splash_updating-0.png" ] && [ -f "/boot/splash_updating180-0.png" ]; then
if [ -e "/boot/rotate180.txt" ]; then
/bin/ply-image /boot/splash_updating180-0.png
else
/bin/ply-image /boot/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/splash_updating-1.png" ] && [ -f "/boot/splash_updating180-1.png" ]; then
if [ -e "/boot/rotate180.txt" ]; then
/bin/ply-image /boot/splash_updating180-1.png
else
/bin/ply-image /boot/splash_updating-1.png
fi
fi
sleep 1
# Wait for IP address for at most 30 seconds
echo "waiting for IP address"
for i in {1..30}
do
#echo "current hostname: $(hostname -I)"
if [ "$(hostname -I)" = "" ]
then
echo "Candle: rc.local doing bootup_actions: no network yet $i" >> /dev/kmsg
echo "no network yet $i"
sleep 1
else
echo "Candle: rc.local doing bootup_actions: IP address detected: $(hostname -I)" >> /dev/kmsg
break
fi
done
#download additional splash images if they don't exist already
if [ ! -f "/boot/splash_updating-0.png" ] && [ -d /boot ]; then
echo "Downloading progress bar images"
wget https://www.candlesmarthome.com/tools/splash_updating-0.png -O /boot/splash_updating-0.png
wget https://www.candlesmarthome.com/tools/splash_updating-1.png -O /boot/splash_updating-1.png
wget https://www.candlesmarthome.com/tools/splash_updating-2.png -O /boot/splash_updating-2.png
wget https://www.candlesmarthome.com/tools/splash_updating-3.png -O /boot/splash_updating-3.png
wget https://www.candlesmarthome.com/tools/splash_updating-4.png -O /boot/splash_updating-4.png
wget https://www.candlesmarthome.com/tools/splash_updating180-0.png -O /boot/splash_updating180-0.png
wget https://www.candlesmarthome.com/tools/splash_updating180-1.png -O /boot/splash_updating180-1.png
wget https://www.candlesmarthome.com/tools/splash_updating180-2.png -O /boot/splash_updating180-2.png
wget https://www.candlesmarthome.com/tools/splash_updating180-3.png -O /boot/splash_updating180-3.png
wget https://www.candlesmarthome.com/tools/splash_updating180-4.png -O /boot/splash_updating180-4.png
wget -c https://www.candlesmarthome.com/tools/error.png -O /boot/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.9."* ]]; then
echo "v3.9. spotted"
URLS=$(echo "$URLS" | grep "v3.9.")
fi
if [ -n "$URLS" ]; then
echo "selecting first URL from remaning 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"
echo "Candle: succesfully downloaded missing addon: $directory" >> /dev/kmsg
echo "Candle: restoring backup: succesfully downloaded addon: $directory" >> /boot/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/candle_log.txt
fi
if [ "$COUNTER" == 3 ]; then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/splash_updating-2.png" ] && [ -f "/boot/splash_updating180-2.png" ]; then
if [ -e "/boot/rotate180.txt" ]; then
/bin/ply-image /boot/splash_updating180-2.png
else
/bin/ply-image /boot/splash_updating-2.png
fi
fi
sleep 1
fi
if [ "$COUNTER" == 7 ]; then
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/splash_updating-3.png" ] && [ -f "/boot/splash_updating180-3.png" ]; then
if [ -e "/boot/rotate180.txt" ]; then
/bin/ply-image /boot/splash_updating180-3.png
else
/bin/ply-image /boot/splash_updating-3.png
fi
fi
sleep 1
fi
fi
done < <(ls /home/pi/.webthings/data)
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/splash_updating-4.png" ] && [ -f "/boot/splash_updating180-4.png" ]; then
if [ -e "/boot/rotate180.txt" ]; then
/bin/ply-image /boot/splash_updating180-4.png
else
/bin/ply-image /boot/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/candle_log.txt
if [ -e "/bin/ply-image" ] && [ -e /dev/fb0 ] && [ -f "/boot/error.png" ]; then
/bin/ply-image /boot/error.png
fi
sleep 7
fi
fi
# Clean up
rm /boot/bootup_actions.sh
rm /boot/bootup_actions_failed.sh
#sudo systemctl start webthings-gateway.service
exit 1