-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_restore.sh
executable file
·299 lines (278 loc) · 8.7 KB
/
mm_restore.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
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
#!/bin/bash
known_list="request valid-url jsdom node-fetch digest-fetch"
# backup MM modules config
base=$HOME/MagicMirror
saveDir=$HOME/MM_backup
logfile=$HOME/MagicMirror/installers/restore.log
# is this a mac
mac=$(uname -s)
fetch=
process_args(){
local OPTIND
while getopts ":hs:b:r:u:f" opt
do
#echo opt=$opt
case $opt in
# help
h) echo
echo $0 takes optional parameters
echo
echo -e "\t -s MagicMirror_dir"
echo -e "\t\tdefault $base"
echo
echo -e "\t -b backup_dir "
echo -e "\t\tdefault $saveDir"
echo
echo -e "\t -f "
echo -e "\t\tfetch/clone repo and restore latest tag"
echo
echo -e "\t -r github repository name (reponame)"
echo -e "\t\ttypically https://github.com/username/reponame.git"
echo -e "\t\tdefault output of git remote -v (if set)"
echo -e "\t\t -r overrides the git remote setting"
echo
echo -e "\t -u github username"
echo -e "\t\tdefault none"
exit 1
;;
s)
# source MagicMirror folder
b=$(echo $OPTARG | tr -d [:blank:])
if [ -d $HOME/$b ]; then
base=$HOME/$b
else
if [ -d $b ]; then
base=$b
else
echo unable to find Source folder $OPTARG | tee -a $logfile
exit 2
fi
fi
echo MagicMirror folder to update is $base | tee -a $logfile
;;
b)
echo processing backup parm
# backup folder
saveDir=$(echo $OPTARG | tr -d [:blank:])
# if the backup/restrore folder exists in users home
if [ -d $HOME/$saveDir ]; then
# use it
saveDir=$HOME/$saveDir
else
# if the folder doesn't exist
if [ ! -d $saveDir ]; then
# check to see if it starts with a /
if [ ${saveDir:0:1} != "/" ]; then
# if its a full path to the home folder
# it will be created on clone
if [ $HOME/$saveDir != $saveDir ]; then
echo backup folder $saveDir not found, will be created on git clone | tee -a $logfile
fi
fi
fi
fi
echo backup folder is $saveDir | tee -a $logfile
;;
u)
# username
user_name=$(echo $OPTARG | tr -d [:blank:])
;;
r)
# github repo name or url
repo=$OPTARG
# check for full url specified, we only want the name
IFS='/'; repoIN=($OPTARG); unset IFS;
# if there were slashes
if [ ${#repoIN[@]} -gt 0 ]; then
# get the last element of split array
index=${#repoIN[@]}
# get the name
repot=${repoIN[$((index -1))]}
# user is one array element earlier
# get the user name from the URL
user_name=${repoIN[$(($index-2))]}
# check for '.git'
IFS='.'; repoN=($repot); unset IFS;
# get just the name
repo_name=${repoN[0]}
fetch=true
else
repo_name=$repo
fetch=true
fi
;;
f)
fetch=true
fetch_tag=
vparm=${@:$OPTIND}
if [[ ${vparm:0:1} != "-" ]];then
fetch_tag=$(echo ${@:$OPTIND}| cut -d' ' -f1)
OPTIND=$((OPTIND+1))
fi
;;
*) echo "Illegal option '-$OPTARG'" && exit 3
;;
esac
done
}
# if this script was started directly then arg0 is 'mm_backup.sh', else it is the first argument provided (oops)
if [[ $0 == *.sh ]]; then
process_args "$0 $@"
else
process_args "$0 $@"
fi
if [ $mac == 'Darwin' ]; then
cmd=greadlink
else
cmd=readlink
fi
date +"restore starting - %a %b %e %H:%M:%S %Z %Y" >>$logfile
echo restoring MM configuration from $saveDir to $base | tee -a $logfile
echo
cd $HOME
# fetch use latest tag (bu highest number)
if [ "$fetch." != "." ]; then
echo trying to fetch repo from github | tee -a $logfile
# if the directory doesn't exist
if [ ! -d $saveDir ]; then
# and we have username and repo name
if [ "$user_name." != "." -a "$repo_name." != "." ]; then
echo folder $saveDir does not exist, will clone it from github | tee -a $logfile
git clone "https://github.com/$user_name/$repo_name" $saveDir >/dev/null 2>&1
cd $saveDir
else
echo -e "\t\t need both the github username and the github repository name to retrieve the github repo" | tee -a $logfile
exit 4
fi
else
cd $saveDir
if [ "$(git remote -v)." != "." ]; then
repo_name=$(git remote -v | head -n1 | awk '{print $2}')
echo $saveDir exists fetching all tags from $repo_name | tee -a $logfile
if [ "$user_name." != "." -a "$repo_name." != "." ]; then
git fetch --all --tags
else
echo -e "\t\t need both the github username and the github repository name" | tee -a $logfile
exit 5
fi
else
echo -e "$saveDir exists, but its not a linked git repo, can't fetch or clone" | tee -a $logfile
exit 6
fi
fi
else
if [ ! -d $saveDir ]; then
echo Backup directory $saveDir not found, exiting | tee -a $logfile
exit 1
fi
cd $saveDir
fi
# get the last numeric tag
if [ "$fetch_tag." != "." ]; then
last_tag=$fetch_tag
else
last_tag=$(git tag -l | sort -g -r | head -n1)
fi
# get on some known branch
git checkout main >/dev/null 2>&1
# delete the temp branch for the restore
git branch -D restore-branch >/dev/null 2>&1
# create the branch from the tag
git checkout tags/$last_tag -b restore-branch >/dev/null 2>&1
echo created git branch from last tag = $last_tag | tee -a $logfile
# restore the config for MM
cp -p $saveDir/config.js $base/config
# restore the custom/.css for MM (no error if not found)
cp -p $saveDir/custom.css $base/css 2>/dev/null
echo restored config.js and custom.css | tee -a $logfile
repo_list=$saveDir/module_list
echo processing module_list >>$logfile
if [ -e $repo_list ]; then
SAVEIFS=$IFS # Save current IFS
IFS=$'\n'
# split output on new lines, not spaces
urls=($(cat $repo_list))
IFS=$SAVEIFS
# if there modules to restore
if [ ${#urls} -gt 0 ]; then
# restore the repos
cd $base/modules
# loop thru the modules listed
for repo_url in "${urls[@]}"
do
module=$(echo $repo_url | awk -F/ '{print $(NF)}' | awk -F. '{print $1}')
# if the module folder does not exist
if [ ! -d $module ]; then
echo restoring $module | tee -a $logfile
gc=$(git clone $repo_url 2>&1)
gc_rc=$?
if [ $gc_rc -eq 0 ]; then
cd $module
if [ -e package.json ]; then
if [ $(grep \""dependencies\"" package.json | wc -l) -gt 0 ]; then
echo module $module contains package.json, doing npm install | tee -a $logfile
npm install --only=prod --no-audit --no-fund --loglevel error --legacy-peer-deps 2>&1 >> $logfile
fi
else
echo module $module DOES NOT contain package.json | tee -a $logfile
fi
# if there is a folder of module specific files saved by backup
if [ -d $saveDir/$module ]; then
# copy them from the backup
echo there were files saved for this module , restoring | tee -a $logfile
cp -a $saveDir/$module/. ~/MagicMirror/modules/$module
fi
cd - >/dev/null
fi
else
echo -e "\e[91m $module folder already exists, skipping restore from $repo_url \e[90m" | tee -a $logfile
tput init 2>/dev/null
echo
fi
echo
done
# lets check for modules with missing requires (libs removed from MM base)
# this skips any default modules
echo Checking for modules with removed libraries| tee -a $logfile
mods=($(find . -maxdepth 2 -type f -name node_helper.js | awk -F/ '{print $2}'))
# loop thru all the installed modules
for mod in "${mods[@]}"
do
# get the require statements from the node helper
requires=($(grep -e "require(" $mod/node_helper.js | awk -F '[()]' '{print $2}' | tr -d '"' | tr -d "'"))
# loop thru the requires
for require in "${requires[@]}"
do
# check it against the list of known lib removals
case " $known_list " in (*" $require "*) :;; (*) false;; esac
# if found in the list
if [ $? == 0 ]; then
# if no package.json, we would have to create one
cd $mod
if [ ! -e package.json ]; then
echo -e ' \n\t ' package.json not found for module $mod
echo -e ' \n\t\t ' adding package.json for module $mod | tee -a $logfile
npm init -y >/dev/null 2>&1
fi
# if package.json exists, could have been just added
if [ -e package.json ]; then
# check for this library in the package.json
pk=$(grep $require package.json)
# if not present, need to do install
if [ "$pk." == "." ]; then
echo -e ' \n\t\t ' require for $require in module $mod not found in package.json | tee -a $logfile
echo -e ' \n\t\t ' installing $require for module $mod | tee -a $logfile
npm install $require --save --no-audit --no-fund --loglevel error --legacy-peer-deps --only=prod 2>&1 >> $logfile
fi
fi
cd - >/dev/null
fi
done
done
fi
echo
echo restore completed, you can start MagicMirror now | tee -a $logfile
else
echo no saved module repo list | tee -a $logfile
fi
date +"restore ended - %a %b %e %H:%M:%S %Z %Y" >>$logfile