-
Notifications
You must be signed in to change notification settings - Fork 62
/
backup
executable file
·71 lines (55 loc) · 1.51 KB
/
backup
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
#!/bin/bash
# unmount any partitions on the target device
a=9
while [ $a -gt 0 ]
do
sudo umount $1$a
a=`expr $a - 1`
done
# wipe the FAT on the target
sudo dd if=/dev/zero of=$1 bs=512 count=1
# prepare mount points
src=$(mktemp -d)
dst=$(mktemp -d)
# find the last partition on the source
line=$(sudo parted /dev/mmcblk0 unit s print | tail -n 2 | head -n -1)
lastpnum=$(echo $line | cut -d ' ' -f 1)
# prepare the new FAT
sudo parted $1 mklabel msdos
# mirror the partitions on mmcblk0
sudo parted /dev/mmcblk0 unit s print | tail -n +8 | head -n -1 | while read line ; do
pnum=$(echo $line | cut -d ' ' -f 1)
start=$(echo $line | cut -d ' ' -f 2)
end=$(echo $line | cut -d ' ' -f 3)
type=$(echo $line | cut -d ' ' -f 5)
fstype=$(echo $line | cut -d ' ' -f 6)
flags=$(echo $line | cut -d ' ' -f 7)
if [ "$type" == "extended" ]; then
sudo parted $1 -- mkpart extended $start -1s
else
if [ "$pnum" == "$lastpnum" ]; then
sudo parted $1 -- mkpart $type $fstype $start -1s
else
sudo parted $1 mkpart $type $fstype $start $end
fi
sudo partprobe
# create file systems
if [ "$fstype" == "fat32" ] || [ "$fstype" == "fat16" ]; then
sudo mkfs.fat $1$pnum
fi
if [ "$fstype" == "ext4" ]; then
sudo mkfs.ext4 -F $1$pnum
fi
# mount and copy
sudo mount $1$pnum $dst
sudo mount /dev/mmcblk0p$pnum $src
sudo cp -axv $src/. $dst/.
sudo umount $dst
sudo umount $src
fi
if [ "$flags" == "lba" ]; then
sudo parted $1 set $pnum lba on
else
sudo parted $1 set $pnum lba off
fi
done