-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosmc-restore
More file actions
504 lines (466 loc) · 15.3 KB
/
osmc-restore
File metadata and controls
504 lines (466 loc) · 15.3 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#! /bin/bash
# A script to restore backups to removable media. It will also format a USB stick or
# SD card suitable for OSMC and restore the system and boot partitions appropriately
# Copyright (c) Graham Horner, 2017-19.
# Feel free to use and/or modify for any non-commercial purposes.
# No warranty as to fitness of purpose or suitability
# is given or should be inferred.
# Any comments or bug reports to discourse.osmc.tv.
VERSION='0.1.12' ## further check for vero and print version
#set -x
echo $(date) Restore version $VERSION
# check if this is being run as root
if (( $(/usr/bin/id -u) != 0 )); then
echo Must be run as root!
exit 1
fi
# check we have parted
if ! parted --version &> /dev/null; then
GETDEPENDS=1
echo -e "\nThis script needs parted but I can't find it\nTry: sudo apt-get install parted (or similar)\n"
fi
# check we have rsync
if ! rsync --version &> /dev/null; then
GETDEPENDS=1
echo -e "\nThis script needs rsync but I can't find it\nTry: sudo apt-get install rsync (or similar)\n"
fi
# check we have pv
if ! pv --version &> /dev/null; then
GETDEPENDS=1
echo -e "\nThis script needs pv but I can't find it\nTry: sudo apt-get install pv (or similar)\n"
fi
if (($GETDEPENDS)); then exit 0; fi
# Parse arguments
BACKUPDIR="$1"
MEDIA="$2"
FORMAT=0
if [ "$1" == "-f" ]; then
FORMAT=1
BACKUPDIR="$2"
MEDIA="$3"
elif [ "$2" == "-f" ]; then
FORMAT=1
MEDIA="$3"
elif [ "$3" == "-f" ]; then
FORMAT=1
fi
if [ -z $1 ]; then
echo Usage: "$( basename "${BASH_SOURCE[0]}") [-f] source_device|backupdir|/[system|boot] [restore_device][partition]"
echo Example: "$( basename "${BASH_SOURCE[0]}") /media/backup/2017-04-01 sdc"
echo Example: "$( basename "${BASH_SOURCE[0]}") sdb sdc"
exit 1
fi
#echo $BACKUPDIR
CLONE=0
LIVE=0
PARTStoRESTORE=(boot system)
# check if we are only copying one partition
for source in ${PARTStoRESTORE[@]}; do
if [[ "$BACKUPDIR" =~ "$source"$ ]]; then
echo Restoring only $BACKUPDIR
PARTStoRESTORE=($source)
BACKUPDIR=${BACKUPDIR%/$source}
#echo now $BACKUPDIR
if [ -z "$BACKUPDIR" ]; then
CLONE=1
LIVE=1
BACKUPDIR=/
fi
fi
done
# see if $BACKUPDIR is a device
if [[ $(lsblk -lo NAME,TYPE | grep "$BACKUPDIR.*disk"$ ) ]] && ! [[ "$BACKUPDIR" =~ ^\. ]] ; then
CLONE=1
BACKUPDEV=($(lsblk -lo NAME,TYPE | grep "$BACKUPDIR.*disk"$ ))
echo Going to copy ${BACKUPDEV[0]}
elif ! [[ "$BACKUPDIR" =~ ^/ ]]; then
# make BACKUPDIR path absolute
BACKUPDIR="$PWD/$BACKUPDIR"
# tidy it
BACKUPDIR=$( pushd "$BACKUPDIR" > /dev/null && pwd && popd > /dev/null )
else
if [ "$BACKUPDIR" == "/" ]; then
CLONE=1
LIVE=1
echo Going to copy the working filesystem
else
# remove a trailing slash
BACKUPDIR=${BACKUPDIR%/}
fi
fi
VERO=0
if [[ ${BACKUPDIR^^} =~ "VERO" ]]; then
VERO=1
elif ((LIVE)) && [[ $(cat /proc/cmdline) =~ vero ]]; then
VERO=1
elif ! [ -d "$BACKUPDIR/boot" ] && ! (($CLONE)); then
VERO=1
elif [ $(ls "$BACKUPDIR/system/boot" | wc -l) \> 0 ]; then
VERO=1
fi
SCRIPTDIR="$( pushd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd && popd > /dev/null )"
BACKUPFOUND=0
#echo $BACKUPDIR
if [ -d "$BACKUPDIR" ] || (($CLONE)); then
for source in ${PARTStoRESTORE[@]}; do
if [ $source == "boot" ] && (($VERO)); then
BACKUPFOUND=$(($BACKUPFOUND + 1))
continue
fi
if (($CLONE)); then
if (($LIVE)); then
if [ $source == "system" ]; then
# the system partition is mounted somewhere - find the device it is on
SRCDEV=$(findmnt -l | grep "^/\s.*/dev" | sed 's/^\S*\s*\(\S*\).*/\1/')
else
SRCDEV=$(findmnt -l | grep "^/$source\s.*/dev" | sed 's/^\S*\s*\(\S*\).*/\1/')
fi
else
# partition is defined by device ID so re-mount it
if [ $source == "system" ]; then
SRCDEV="/dev/$BACKUPDIR"2
elif [ $source == "boot" ]; then
SRCDEV="/dev/$BACKUPDIR"1
fi
umount $SRCDEV || echo Cannot unmount $SRCDEV
mkdir -p /media/sdcard/$source
if [ $(ls /media/sdcard/$source | wc -l) == 0 ]; then mount $SRCDEV /media/sdcard/$source || exit 1; fi
# else we assume it's already mounted, (eg if last backup didn't complete)
# but lets just check (SHOULDN'T NEED THIS, NOW)
if ! findmnt -l | grep "^/media/sdcard/$source " &> /dev/null; then
echo /media/sdcard/$source is not a mount point
exit 1
fi
fi
BACKUPFOUND=1
elif [ -d "$BACKUPDIR"/$source ]; then
BACKUPFOUND=$(($BACKUPFOUND + 1))
else
echo There is no $source directory in $BACKUPDIR, please check
fi
done
if ! (($BACKUPFOUND)); then exit 0; fi
if ((CLONE)) && !((LIVE)); then BACKUPDIR=/media/sdcard; fi
else
echo Cannot find $BACKUPDIR, please check
exit 0
fi
# have a look for the media to install to
MEDIA=${MEDIA##/dev/}
PARTPREFIX=
while ! lsblk | grep "^$MEDIA " > /dev/null; do
#lsblk -l | grep "\s1\s*\S.*\sdisk" | sed 's/^\(\S*\)\s.*/\1/'
lsblk -lo NAME,SIZE,TYPE | grep "disk"$
read -rp "Please enter the device name of the media to restore to ... " MEDIA
echo
MEDIA=${MEDIA##/dev/}
PARTPREFIX=
if [[ $MEDIA =~ mmcblk.* ]]; then
PARTPREFIX="p"
fi
if findmnt -l "/dev/$MEDIA$PARTPREFIX"1 > /dev/null && ! findmnt -l "/dev/$MEDIA$PARTPREFIX"1 | grep "/media" > /dev/null; then
read -n1 -rsp "/dev/$MEDIA is mounted but not removable - are you sure (Y/N)? " ANSWER
if ! [[ $ANSWER =~ [Yy] ]]; then
echo
echo Aborting ....
exit 0
else
echo
fi
fi
done
if [[ $MEDIA =~ mmcblk.* ]]; then
PARTPREFIX="p"
fi
# trim partition number if both partitions to be restored
PARTNO=0
if [[ $MEDIA =~ (mmcblk[0-9])p([0-9])$ ]]; then
DEVICE=${BASH_REMATCH[1]}
PARTNO=${BASH_REMATCH[2]}
elif ! [[ $MEDIA =~ mmcblk.* ]] && [[ $MEDIA =~ (.*)([0-9])$ ]]; then
DEVICE=${BASH_REMATCH[1]}
PARTNO=${BASH_REMATCH[2]}
else
DEVICE=$MEDIA
fi
if [ ${#PARTStoRESTORE[@]} \> 1 ] || (($FORMAT)); then
MEDIA=$DEVICE
PARTNO=0
fi
function format(){
read -n1 -rsp "Going to erase all of /dev/$MEDIA, press Y to continue ... " ANSWER
if ! [[ $ANSWER =~ [Yy] ]]; then
echo
echo Aborting ....
exit 0
else
echo
fi
if findmnt -l "/dev/$MEDIA$PARTPREFIX"1 > /dev/null; then
echo "/dev/$MEDIA$PARTPREFIX"1 is mounted
PART=1
while findmnt -l "/dev/$MEDIA$PARTPREFIX$PART" > /dev/null; do
if ! umount "/dev/$MEDIA$PARTPREFIX$PART"; then
echo
echo Problem unmounting /dev/$MEDIA$PARTPREFIX$PART
exit 1
else
echo Unmounted "/dev/$MEDIA$PARTPREFIX$PART"
fi
PART=$(($PART + 1))
done
fi
# find out how big it is to determine AU size
DISKSIZE=$(lsblk -blo NAME,SIZE | grep "$MEDIA " | sed 's/^\S*\s*\(\S*\)\s.*/\1/')
BLANK=4MiB
PART1=248MiB
if (( DISKSIZE > 33000000000)); then
BLANK=16MiB
PART1=256MiB
fi
if [[ $MEDIA =~ "mmcblk" ]]; then
echo Formatting SD card $MEDIA
else
echo Formatting USB stick $MEDIA
fi
parted -s /dev/$MEDIA mklabel msdos mkpart primary fat32 $BLANK $PART1 \
&& parted -s /dev/$MEDIA -- mkpart primary ext4 $PART1 -1s
# refresh the partitions in the kernel, but that also mounts them
# if there was already a filesystem there
partprobe /dev/"$MEDIA"
# wait for the system to register the new partitions
while ! [ -e /dev/"$MEDIA$PARTPREFIX"1 ]; do
echo Waiting for /dev/"$MEDIA"
sleep 1
done
parted /dev/"$MEDIA" set 1 lba on
umount /dev/"$MEDIA$PARTPREFIX"1 2> /dev/null
mkfs.vfat -F 32 -s 4 -I /dev/"$MEDIA$PARTPREFIX"1
partprobe /dev/"$MEDIA"
# wait for the system to register the new partitions
while ! [ -e /dev/"$MEDIA$PARTPREFIX"1 ]; do
echo Waiting for /dev/"$MEDIA"
sleep 1
done
umount /dev/"$MEDIA$PARTPREFIX"2 2> /dev/null
mkfs.ext4 /dev/"$MEDIA$PARTPREFIX"2
# run this again to refresh things
partprobe /dev/"$MEDIA" &> /dev/null
while ! [ -e /dev/"$MEDIA$PARTPREFIX"1 ]; do
echo Waiting for /dev/"$MEDIA"
sleep 1
done
}
if (($FORMAT)); then format; fi
START=$(date +%s)
function restore(){
for source in ${PARTStoRESTORE[@]}; do
PARTNAME=
if [[ $source =~ "boot" ]]; then
PARTTYPE=vfat; PARTSIZEMIN=244000000
CPPARAM="--no-preserve=ownership"
elif [ $source == "system" ]; then
PARTTYPE=ext
if [ "$BACKUPDIR" == '/' ]; then
if [ $1 == "dry" ]; then
echo Checking size of working system
fi
PARTSIZEMIN=($(du -xsB1 "$BACKUPDIR"))
else
if [ $1 == "dry" ]; then
echo Checking size of "$BACKUPDIR/$source"
fi
PARTSIZEMIN=($(du -xsB1 "$BACKUPDIR/$source"))
fi
CPPARAM="-x" # don't copy mounts boot, media, mnt
if [ $1 == "dry" ]; then
printf "%'u%s\n\n" $((${PARTSIZEMIN[0]} / 1024)) "KiB in $source"
fi
fi
for partindex in $(seq 1 3 "$((${#PARTS[@]} - 2))"); do
# do it only once if partition number has been specified
if [ $PARTNO != "0" ]; then
if [ $partindex \> "$(( ($PARTNO - 1)*3 + 1 ))" ]; then break
elif [ $partindex \< "$(( ($PARTNO - 1)*3 + 1 ))" ]; then continue
fi
fi
#echo partition: ${PARTS[$partindex]}
if [[ ${PARTS[$partindex]} =~ "$PARTTYPE" ]]; then
PARTNAME=${PARTS[$(($partindex - 1))]}; #echo $PARTNAME
PARTSIZE=${PARTS[$(($partindex + 1))]}; #echo $PARTSIZE
if (( $PARTSIZE < $PARTSIZEMIN )); then
printf "%s%'u%s\n" "$PARTTYPE partition at $PARTNAME may be too small (" $(($PARTSIZE / 1024)) " KiB)"
read -n1 -rsp "Press Y to continue, any other key to skip: " ANSWER
if ! [[ $ANSWER =~ [Yy] ]]; then
echo
echo Skipping $PARTNAME partition ....
PARTNAME=skip
continue
else
break
fi
elif [ $1 != "dry" ]; then
printf "%s%'u%s\n" "Found $PARTTYPE partition at $PARTNAME with enough space (" $(($PARTSIZE / 1024)) " KiB)"
read -n1 -rsp "Press Y to write $source to this partition, any other key to skip: " ANSWER
if ! [[ $ANSWER =~ [Yy] ]]; then
echo
echo Skipping $PARTNAME partition ....
echo
PARTNAME=skip
continue
else
break
fi
fi
fi
done
if [ -z $PARTNAME ]; then
echo "Cannot find a suitable $PARTTYPE partition for $source"
echo
continue
fi
if [ $PARTNAME == "skip" ]; then continue; fi
PARTSFOUND=$(( $PARTSFOUND + 1 ))
if [ $1 == "dry" ]; then continue; fi
# check if target is already mounted - it will be if we've just formatted it
TARGET=$(findmnt -lo SOURCE,TARGET "/dev/$PARTNAME" | grep "[(media)|(mnt)]") # | sed 's/^.*\/[\(media\)|\(mnt\)]\/\(.*\)/\/\1\/\3/')
TARGET=${TARGET#/dev/"$PARTNAME "}
echo
#echo target: $TARGET
if ! [ -z $TARGET ] && ! $(echo "$TARGET" | grep "/media" > /dev/null); then
read -n1 -rsp "/dev/$PARTNAME is mounted but not removable - are you sure (Y/N)? " ANSWER
if ! [[ $ANSWER =~ [Yy] ]]; then
echo
echo Aborting ....
exit 0
else
echo
fi
fi
# tried to use an existing mount but safer to do it this way
if ! [ -z "$TARGET" ]; then
if ! umount "$TARGET";then
echo Cannot unmount $TARGET - is it busy?
exit 1
fi
fi
mkdir -p /media/$source
#ls -al /media/$source
mount /dev/"$PARTNAME" /media/$source
#echo $(findmnt -l "/dev/$PARTNAME")
TARGET=/media/$source
if [ $source == 'system' ] && [ "$BACKUPDIR" == '/' ]; then
echo Copying root directory to /dev/$PARTNAME
EXCLUDES='--exclude=.gvfs'
# directories generated by the system
EXCLUDES+=' --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/run/* --exclude=/tmp/*'
# you probably don't want this directory - the equivalent of Windows *.chk files
EXCLUDES+=' --exclude=/lost+found'
# these mount directories should be excluded automatically by the -x switch in rsync
# but just to be sure
EXCLUDES+=' --exclude=/mnt/* --exclude=/media/*'
### you may like to uncomment the next line - these directories should be unnecessary for a restore
#EXCLUDES+=' --exclude=/var/cache/* --exclude=/var/run/* --exclude=/var/tmp/*'
#cp -a "$CPPARAM" "$BACKUPDIR". "$TARGET"
rsync -axcH "$EXCLUDES" --delete --delete-excluded --info=progress2 --no-specials --no-devices "$BACKUPDIR" "$TARGET"
elif [ $source == 'boot' ] && (($VERO)); then
echo Copying Vero installer files to /dev/$PARTNAME
FILESFOUND=1
for img in kernel dtb; do
if [ -f "$BACKUPDIR/../$img.img" ]; then
echo Copying $img.img to /dev/$PARTNAME
cp "$BACKUPDIR/../$img.img" "$TARGET"
elif [ -f "$SCRIPTDIR/$img.img" ]; then
echo Copying $img.img to /dev/$PARTNAME
cp "$SCRIPTDIR/$img.img" "$TARGET"
else
FILESFOUND=0
fi
done
if ! ((FILESFOUND)) ; then
echo "Can't find kernel.img and/or dtb.img in ${BACKUPDIR%/*}."
echo For Vero, you need copies of kernel.img and \(for Vero4k\) dtb.img from a recent install image.
echo Just copy these two files onto the FAT partition of your SD card or USB stick
fi
rm "$TARGET"/filesystem.sh &> /dev/null
echo Writing filesystem.sh to $TARGET
cat <<- EOF > "$TARGET"/filesystem.sh
mkdir /mnt/backup
DEVICE=\$( mount | grep "/mnt/boot" | awk '{ print \$1 }' )
DEVICE=\${DEVICE%1}2
mount \$DEVICE /mnt/backup
if [ -f /mnt/backup/filesystem.tar ]; then
SYSTEMSIZE=\$(du -s /mnt/backup/filesystem.tar | awk '{ print \$1 }')
cat /mnt/backup/filesystem.tar | pv -s \${SYSTEMSIZE}k -n | tar -xf - -C /mnt/root
elif [ -f /mnt/backup/filesystem.tar.xz ]; then
SYSTEMSIZE=\$(du -s /mnt/backup/filesystem.tar.xz | awk '{ print \$1 }')
cat /mnt/backup/filesystem.tar.xz | pv -s \${SYSTEMSIZE}k -n | tar -xJf - -C /mnt/root
# provide for restore direct from backup via ln -s <system> filesystem
elif [ -h /mnt/backup/filesystem ]; then
SYSTEMSIZE=\$(du -s /mnt/backup/filesystem | awk '{ print \$1 }')
find /mnt/backup/filesystem -type s -exec rm {} \\;
tar -cf - -C /mnt/backup/filesystem ./ | pv -s \${SYSTEMSIZE}k -n | tar -xf - -C /mnt/root
else
SYSTEMSIZE=\$(du -s /mnt/backup | awk '{ print \$1 }')
find /mnt/backup -type s -exec rm {} \\;
tar -cf - -C /mnt/backup ./ | pv -s \${SYSTEMSIZE}k -n | tar -xf - -C /mnt/root
fi
sync
umount /mnt/backup
EOF
else
if [ -f "$TARGET"/filesystem.tar ]; then
rm "$TARGET"/filesystem.tar
fi
echo Copying $BACKUPDIR/$source to /dev/$PARTNAME
if (($VERO)); then
tar -cf - -C "$BACKUPDIR/$source" ./ | pv -s ${PARTSIZEMIN[0]} | cat - > "$TARGET"/filesystem.tar
else
tar -cf - -C "$BACKUPDIR/$source" ./ | pv -s ${PARTSIZEMIN[0]} | tar -xf - -C "$TARGET"
fi
fi
echo unmounting "$TARGET"
echo
umount "$TARGET"
if [ -d "$TARGET" ]; then rmdir "$TARGET"; fi
umount /media/sdcard/$source &> /dev/null && \
rmdir /media/sdcard/$source &> /dev/null
RESTORED=$(( $RESTORED + 1 ))
done
}
RESTORED=0
PARTSFOUND=0
while [ $PARTSFOUND \< ${#PARTStoRESTORE[@]} ]; do
# check what partitions we have
PARTS=($(lsblk -lo NAME | grep "$MEDIA$PARTPREFIX\S$"))
echo Looking for existing partitions on $DEVICE
echo Partitions found: ${#PARTS[@]} ${PARTS[@]}
echo
if (( ${#PARTS[@]} >= ${#PARTStoRESTORE[@]} )); then
PARTS=($(lsblk -blo NAME,FSTYPE,SIZE | grep "$MEDIA$PARTPREFIX\S\s"))
restore dry
else
echo Not enough partitions on $DEVICE
fi
if [ $PARTSFOUND \< ${#PARTStoRESTORE[@]} ]; then
echo Cannot not find one or more suitable partitions
echo "Partitions on $DEVICE are as follows"
parted /dev/$DEVICE print
read -n1 -rsp "Do you want to re-format the card and try again (Y/N)? " ANSWER
echo
if [[ $ANSWER =~ [Yy] ]]; then
format
START=$(date +%s)
PARTSFOUND=0
else
break
fi
fi
done
restore all
rmdir /media/sdcard &> /dev/null
if [ $RESTORED \< ${#PARTStoRESTORE[@]} ]; then
echo WARNING: only $RESTORED of ${#PARTStoRESTORE[@]} partitions were restored
fi
ELAPSED=$(($(date +%s) - $START))
echo Time to restore: $(date -d@${ELAPSED} -u +%Hh:%Mm:%Ss)