forked from nanhantianyi/rpi-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.sh
executable file
·129 lines (90 loc) · 2.8 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
#!/bin/bash
sudo apt-get install -y dosfstools parted kpartx rsync
echo ""
echo "software is ready"
file="rpi.img"
if [ "x$1" != "x" ];then
file="$1"
fi
df=`df -P | grep /dev/root | awk '{print $3}'`
dr=`df -P | grep /dev/mmcblk0p1 | awk '{print $2}'`
df=`echo $df $dr |awk '{print int(($1+$2)*1.2)}'`
echo "create $file ..."
sudo dd if=/dev/zero of=$file bs=1K count=$df
start=`sudo fdisk -l /dev/mmcblk0| awk 'NR==9 {print $2}'`
end=`sudo fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
if [ "$start" == "*" ];then
start=`sudo fdisk -l /dev/mmcblk0| awk 'NR==9 {print $3}'`
end=`sudo fdisk -l /dev/mmcblk0| awk 'NR==9 {print $4}'`
fi
start=`echo $start's'`
end=`echo $end's'`
end2=`sudo fdisk -l /dev/mmcblk0| awk 'NR==10 {print $2}'`
end2=`echo $end2's'`
echo "start=$start"
echo "end=$end"
echo "end2=$end2"
sudo parted $file --script -- mklabel msdos
sudo parted $file --script -- mkpart primary fat32 $start $end
sudo parted $file --script -- mkpart primary ext4 $end2 -1
loopdevice=`sudo losetup -f --show $file`
device=`sudo 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`
sudo mkfs.vfat -F 32 -n "boot" $partBoot
echo "$partBoot format success"
sudo mkfs.ext4 $partRoot
echo "$partRoot format success"
sudo mount -t vfat $partBoot /media
sudo cp -rfp /boot/* /media/
sudo sed -i "s/$opartuuidr/$npartuuidr/g" /media/cmdline.txt
sync
sudo umount /media
sudo mount -t ext4 $partRoot /media
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 /media
sudo rsync --force -rltWDEgop --delete --stats --progress \
$EXCLUDE_SWAPFILE \
--exclude ".gvfs" \
--exclude "/boot" \
--exclude "/dev" \
--exclude "/media" \
--exclude "/mnt" \
--exclude "/proc" \
--exclude "/run" \
--exclude "/sys" \
--exclude "/tmp" \
--exclude "lost\+found" \
--exclude "$file" \
/ ./
for i in boot dev media mnt proc run sys boot; do
if [ ! -d /media/$i ]; then
sudo mkdir /media/$i
fi
done
if [ ! -d /media/tmp ]; then
sudo mkdir /media/tmp
sudo chmod a+w /media/tmp
fi
cd
sudo sed -i "s/$opartuuidb/$npartuuidb/g" /media/etc/fstab
sudo sed -i "s/$opartuuidr/$npartuuidr/g" /media/etc/fstab
sync
sudo umount /media
sudo kpartx -d $loopdevice
sudo losetup -d $loopdevice