This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
radxa-rbuild-backup.sh
executable file
·285 lines (245 loc) · 7.35 KB
/
radxa-rbuild-backup.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
#!/bin/bash
set -e
VERSION="1.0"
MODEL=`cat /proc/device-tree/model`
BOARD_NAME=${MODEL// /-}
SCRIPT_NAME=$(basename $0)
CONFIG_MOUNT=$(mktemp -d)
EFI_MOUNT=$(mktemp -d)
ROOT_MOUNT=$(mktemp -d)
DEVICE=/dev/$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p' | cut -b 1-7)
check_root() {
if [ $(id -u) != 0 ]; then
echo -e "${SCRIPT_NAME} needs to be run as root.\n"
exit 1
fi
}
get_option() {
exclude=""
label=rootfs
model=$BOARD_NAME
OLD_OPTIND=$OPTIND
while getopts "o:e:l:u:h" flag; do
case $flag in
o)
output="$OPTARG"
;;
e)
exclude="${exclude} --exclude ${OPTARG}"
;;
l)
label="$OPTARG"
;;
u)
$OPTARG
unattended="1"
;;
h)
$OPTARG
print_help="1"
;;
esac
done
OPTIND=$OLD_OPTIND
}
confirm() {
if [ "$unattended" == "1" ]; then
return 0
fi
printf "\n%s [Y/n] " "$1"
read resp
if [ "$resp" == "Y" ] || [ "$resp" == "y" ] || [ "$resp" == "yes" ]; then
return 0
fi
if [ "$2" == "abort" ]; then
echo -e "Abort.\n"
exit 0
fi
if [ "$2" == "clean" ]; then
rm "$3"
echo -e "Abort.\n"
exit 0
fi
return 1
}
install_tools() {
commands="rsync parted gdisk fdisk kpartx mkfs.vfat losetup"
packages="rsync parted gdisk fdisk kpartx dosfstools util-linux"
idx=1
need_packages=""
for cmd in $commands; do
if ! command -v $cmd > /dev/null; then
pkg=$(echo "$packages" | cut -d " " -f $idx)
printf "%-30s %s\n" "Command not found: $cmd", "package required: $pkg"
need_packages="$need_packages $pkg"
fi
((++idx))
done
if [ "$need_packages" != "" ]; then
confirm "Do you want to apt-get install the packages?" "abort"
apt-get update
apt-get install -y --no-install-recommends $need_packages
echo '--------------------'
fi
}
gen_partitions() {
if [ "$model" ]; then
loader1_size=8000
reserved1_size=128
reserved2_size=8192
loader2_size=8192
atf_size=8192
boot_size=1048576
system_start=0
loader1_start=64
config_start=32768
reserved1_start=$(expr ${loader1_start} + ${loader1_size})
reserved2_start=$(expr ${reserved1_start} + ${reserved1_size})
loader2_start=$(expr ${reserved2_start} + ${reserved2_size})
atf_start=$(expr ${loader2_start} + ${loader2_size})
boot_start=$(expr ${atf_start} + ${atf_size})
efi_start=65536
rootfs_start=679936
else
echo cannot find board name !!!
exit -1
fi
}
gen_image_file() {
if [ "$output" == "" ]; then
output="${PWD}/${model}-backup-$(date +%y%m%d-%H%M).img"
else
if [ "${output:(-4)}" == ".img" ]; then
mkdir -p $(dirname $output)
else
mkdir -p "$output"
output="${output%/}/${model}-backup-$(date +%y%m%d-%H%M).img"
fi
fi
rootfs_size=$(expr $(df -P | grep /$ | awk '{print $3}') \* 5 / 4 \* 1024)
backup_size=$(expr \( $rootfs_size + \( ${rootfs_start} + 40 \) \* 512 \) / 1024 / 1024)
dd if=/dev/zero of=${output} bs=1M count=0 seek=$backup_size status=none
if [ "$model" ]; then
parted -s ${output} mklabel gpt
parted -s ${output} unit s mkpart config ${config_start} $(expr ${efi_start} - 1)
parted -s ${output} unit s mkpart boot ${efi_start} $(expr ${rootfs_start} - 1)
parted -s ${output} set 2 boot on
parted -s ${output} -- unit s mkpart rootfs ${rootfs_start} -34s
parted -s ${output} set 3 boot on
fi
}
check_avail_space() {
output_=${output}
while true; do
store_size=$(df -BM | grep "$output_\$" | awk '{print $4}' | sed 's/M//g')
if [ "$store_size" != "" ] || [ "$output_" == "\\" ]; then
break
fi
output_=$(dirname $output_)
done
if [ $(expr ${store_size} - ${backup_size}) -lt 64 ]; then
rm ${output}
echo -e "No space left on ${output_}\nAborted.\n"
exit 1
fi
return 0
}
backup_image() {
loopdevice=$(losetup -f --show $output)
mapdevice="/dev/mapper/$(kpartx -va $loopdevice | sed -E 's/.*(loop[0-9]+)p.*/\1/g' | head -1)"
sleep 2 # waiting for kpartx
if [ "$model" ]; then
mkfs.vfat -n config ${mapdevice}p1
mkfs.vfat -n boot ${mapdevice}p2
mkfs.ext4 -L ${label} ${mapdevice}p3
mount -t vfat ${mapdevice}p1 ${CONFIG_MOUNT}
mount -t vfat ${mapdevice}p2 ${EFI_MOUNT}
mount -t ext4 ${mapdevice}p3 ${ROOT_MOUNT}
dd if=${DEVICE} of=${output} skip=${loader1_start} seek=${loader1_start} count=32703 conv=notrunc
fi
rsync --force -rltWDEgop --delete --stats --progress //config/ ${CONFIG_MOUNT}
rsync --force -rltWDEgop --delete --stats --progress //boot/efi/ ${EFI_MOUNT}
rsync --force -rltWDEgop --delete --stats --progress $exclude \
--exclude "$output" \
--exclude '.gvfs' \
--exclude '/config' \
--exclude '/dev' \
--exclude '/media' \
--exclude '/mnt' \
--exclude '/proc' \
--exclude '/run' \
--exclude '/sys' \
--exclude '/tmp' \
--exclude 'lost\+found' \
// $ROOT_MOUNT
# special dirs
for i in config dev media mnt proc run sys; do
if [ ! -d $ROOT_MOUNT/$i ]; then
mkdir $ROOT_MOUNT/$i
fi
done
if [ ! -d $ROOT_MOUNT/tmp ]; then
mkdir $ROOT_MOUNT/tmp
chmod a+w $ROOT_MOUNT/tmp
fi
expand_fs && update_uuid && sync
umount $CONFIG_MOUNT && rm -rf $CONFIG_MOUNT
umount $EFI_MOUNT && rm -rf $EFI_MOUNT
umount $ROOT_MOUNT && rm -rf $ROOT_MOUNT
kpartx -d $loopdevice
losetup -d $loopdevice
echo -e "\nBackup done, the file is ${output}"
}
expand_fs() {
rm $ROOT_MOUNT/etc/growroot-grown
echo "resize_root" > $CONFIG_MOUNT/before.txt
}
update_uuid() {
if [ "$model" ]; then
old_config_uuid=$(blkid -o export ${DEVICE}p1 | grep ^UUID)
old_efi_uuid=$(blkid -o export ${DEVICE}p2 | grep ^UUID)
old_root_uuid=$(blkid -o export ${DEVICE}p3 | grep ^UUID)
new_config_uuid=$(blkid -o export ${mapdevice}p1 | grep ^UUID)
new_efi_uuid=$(blkid -o export ${mapdevice}p2 | grep ^UUID)
new_root_uuid=$(blkid -o export ${mapdevice}p3 | grep ^UUID)
sed -i "s/$old_config_uuid/$new_config_uuid/g" $ROOT_MOUNT/etc/fstab
sed -i "s/$old_efi_uuid/$new_efi_uuid/g" $ROOT_MOUNT/etc/fstab
sed -i "s/$old_root_uuid/$new_root_uuid/g" $ROOT_MOUNT/etc/fstab
sed -i "s/${old_root_uuid}/${new_root_uuid}/g" $ROOT_MOUNT/boot/extlinux/extlinux.conf
sed -i "s/${old_root_uuid}/${new_root_uuid}/g" $ROOT_MOUNT/etc/kernel/cmdline
fi
}
usage() {
echo -e "Usage:\n sudo ./${SCRIPT_NAME} [-o path|-e pattern|-m model|-l label|-t target|-u]"
echo ' -o specify output position, default is $PWD'
echo ' -e exclude files matching pattern for rsync'
echo ' -l specify a volume label for rootfs, default is rootfs'
echo ' -u unattended, no need to confirm in the backup process'
}
main() {
check_root
echo -e "Welcome to rockpi-backup.sh, part of the ROCK Pi toolkit.\n"
echo -e " Enter ${SCRIPT_NAME} -h to view help."
echo -e " For a description and example usage, see the README.md at:
https://rock.sh/rockpi-toolbox \n"
echo '--------------------'
if [ "$target" == "expand" ]; then
target_expand
else
install_tools
gen_partitions
gen_image_file
check_avail_space
printf "The backup file will be saved at %s\n" "$output"
printf "After this operation, %s MB of additional disk space will be used.\n" "$backup_size"
confirm "Do you want to continue?" "clean" "$output"
backup_image
fi
}
get_option $@
if [ "$print_help" == "1" ]; then
usage
else
main
fi
# end