forked from nanhantianyi/rpi-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.sh
executable file
·157 lines (111 loc) · 3.21 KB
/
back.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
#!/bin/bash
if [ `whoami` != "root" ];then
echo "This script must be run as root!"
exit 1
fi
# install software
apt update
apt install -y dosfstools parted kpartx rsync
echo ""
echo "software is ready"
file="rpi-`date +%Y%m%d%H%M%S`.img"
if [ "x$1" != "x" ];then
file="$1"
fi
# boot mount point
boot_mnt=`findmnt -n /dev/mmcblk0p1 | awk '{print $1}'`
root_info=`df -PT / | tail -n 1`
root_type=`echo $root_info | awk '{print $2}'`
dr=`echo $root_info | awk '{print $4}'`
db=`df -P | grep /dev/mmcblk0p1 | awk '{print $2}'`
ds=`echo $dr $db |awk '{print int(($1+$2)*1.2)}'`
echo "create $file ..."
dd if=/dev/zero of=$file bs=1K count=0 seek=$ds
#truncate -s ${ds}k $file
start=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $2}'`
end=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
if [ "$start" == "*" ];then
start=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
end=`fdisk -l /dev/mmcblk0| awk 'NR==9 {print $4}'`
fi
start=`echo $start's'`
end=`echo $end's'`
end2=`fdisk -l /dev/mmcblk0| awk 'NR==10 {print $2}'`
end2=`echo $end2's'`
echo "start=$start"
echo "end=$end"
echo "end2=$end2"
parted $file --script -- mklabel msdos
parted $file --script -- mkpart primary fat32 $start $end
parted $file --script -- mkpart primary ext4 $end2 -1
loopdevice=`losetup -f --show $file`
device=`kpartx -va $loopdevice | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
device="/dev/mapper/${device}"
echo "device=$device"
partBoot="${device}p1"
partRoot="${device}p2"
echo "partBoot=$partBoot"
echo "partRoot=$partRoot"
sleep 5s
opartuuidb=`blkid -o export /dev/mmcblk0p1 | grep PARTUUID`
opartuuidr=`blkid -o export /dev/mmcblk0p2 | grep PARTUUID`
npartuuidb=`blkid -o export ${partBoot} | grep PARTUUID`
npartuuidr=`blkid -o export ${partRoot} | grep PARTUUID`
boot_label=`dosfslabel /dev/mmcblk0p1 | tail -n 1`
root_label=`e2label /dev/mmcblk0p2 | tail -n 1`
mkfs.vfat -F 32 -n "$boot_label" $partBoot
echo "$partBoot format success"
mkfs.ext4 $partRoot
e2label $partRoot $root_label
echo "$partRoot format success"
mount -t vfat $partBoot /mnt
cp -rfp ${boot_mnt}/* /mnt/
sed -i "s/$opartuuidr/$npartuuidr/g" /mnt/cmdline.txt
sync
umount /mnt
mount -t ext4 $partRoot /mnt
if [ -f /etc/dphys-swapfile ]; then
SWAPFILE=`cat /etc/dphys-swapfile | grep ^CONF_SWAPFILE | cut -f 2 -d=`
if [ "$SWAPFILE" = "" ]; then
SWAPFILE=/var/swap
fi
EXCLUDE_SWAPFILE="--exclude $SWAPFILE"
fi
cd /mnt
rsync --force -rltWDEgop --delete --stats --progress \
$EXCLUDE_SWAPFILE \
--exclude ".gvfs" \
--exclude "$boot_mnt" \
--exclude "/dev" \
--exclude "/media" \
--exclude "/mnt" \
--exclude "/proc" \
--exclude "/run" \
--exclude "/snap" \
--exclude "/sys" \
--exclude "/tmp" \
--exclude "lost\+found" \
--exclude "$file" \
/ ./
if [ ! -d $boot_mnt ]; then
mkdir $boot_mnt
fi
if [ -d /snap ]; then
mkdir /mnt/snap
fi
for i in boot dev media mnt proc run sys boot; do
if [ ! -d /mnt/$i ]; then
mkdir /mnt/$i
fi
done
if [ ! -d /mnt/tmp ]; then
mkdir /mnt/tmp
chmod a+w /mnt/tmp
fi
cd
sed -i "s/$opartuuidb/$npartuuidb/g" /mnt/etc/fstab
sed -i "s/$opartuuidr/$npartuuidr/g" /mnt/etc/fstab
sync
umount /mnt
kpartx -d $loopdevice
losetup -d $loopdevice