Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Commit 5e2aae9

Browse files
committedJul 25, 2019
first commit
0 parents  commit 5e2aae9

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed
 

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 akgnah
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Rockpi-toolkit
2+
3+
Welcome to rockpi-toolkit, the repository will collect tools that can be used on officially supported ubuntu and debian.
4+
5+
## 1. rockpi-backup.sh
6+
7+
This script allows you to back up your system using Rockpi4 or RockpiS. It is currently possible to back up each other between uSD and eMMC. I haven't tested it on NVMe, because I don't have it yet.
8+
9+
### Install
10+
11+
``` bash
12+
linaro@rockpi:~ $ curl -sL https://rock.sh/rockpi-backup -o rockpi-backup.sh
13+
linaro@rockpi:~ $ chmod +x rockpi-backup.sh
14+
```
15+
16+
### Usage
17+
18+
Run ./rockpi-backup.sh -h to print usage.
19+
20+
```bash
21+
linaro@rockpi:~ $ sudo ./rockpi-backup.sh -h
22+
Usage:
23+
sudo ./rockpi-backup.sh [-o output|-m model|-t target|-u]
24+
-o specify output position, default is $PWD
25+
-m specify model, rockpi4 or rockpis, default is rockpi4
26+
-t specify target, backup or expand, default is backup
27+
-u unattended backup image, no confirmations asked
28+
linaro@rockpi:~ $
29+
```
30+
31+
If you run it without any arguments, the script will work with the default values and will confirm you.
32+
33+
```bash
34+
linaro@rockpi:~ $ sudo ./rockpi-backup.sh
35+
Welcome to use rockpi-backup.sh, part of the Rockpi toolbox.
36+
37+
Enter rockpi-backup.sh -h to view help.
38+
For a description and example usage, see the README.md at:
39+
https://rock.sh/rockpi-toolbox
40+
41+
--------------------
42+
Warning: The resulting partition is not properly aligned for best performance.
43+
--------------------
44+
The backup file will be saved at /home/linaro/rockpi-backup-190725-1004.img
45+
After this operation, 4656 MB of additional disk space will be used.
46+
47+
Do you want to continue? [Y/n]
48+
```
49+
50+
You can specify output path with provide -o argument, if it is a directory, the output file will be directory+date.img, if it is a .img ending file, the output file will be the file.
51+
52+
```bash
53+
linaro@rockpi:~ $ sudo ./rockpi-backup.sh -o /home/linaro/debian.img
54+
Welcome to use rockpi-backup.sh, part of the Rockpi toolbox.
55+
56+
Enter rockpi-backup.sh -h to view help.
57+
For a description and example usage, see the README.md at:
58+
https://rock.sh/rockpi-toolbox
59+
60+
--------------------
61+
Warning: The resulting partition is not properly aligned for best performance.
62+
--------------------
63+
The backup file will be saved at /home/linaro/debian.img
64+
After this operation, 4656 MB of additional disk space will be used.
65+
66+
Do you want to continue? [Y/n]
67+
```
68+
69+
### Tips
70+
71+
1. If you want to use dd to restore your image, and your uSD or eMMC has GPT partitions and is mounted, please umount before dd.
72+
73+
2. If you are using debian, want to backup and restore from uSD to eMMC, or from eMMC to uSD, you need to edit the boot line in /etc/fstab, mmcblk0 for uSD and mmcblk1 for eMMC.

‎rockpi-backup.sh

+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#!/bin/bash -e
2+
AUTHOR="Akgnah <1024@setq.me>"
3+
VERSION="0.10.0"
4+
SCRIPT_NAME=`basename $0`
5+
ROOTFS_MOUNT=/tmp/rootfs
6+
ROOTFS_PATH=/tmp/rootfs.img
7+
DEVICE=/dev/$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p' | cut -b 1-7)
8+
9+
10+
if [ `id -u` != 0 ]
11+
then
12+
echo -e "${SCRIPT_NAME} needs to be run as root.\n"
13+
exit 1
14+
fi
15+
16+
17+
confirm() {
18+
if [ "$unattended" == "1" ]; then
19+
return 0
20+
fi
21+
printf "\n%s [Y/n] " "$1"
22+
read resp
23+
if [ "$resp" == "Y" ] || [ "$resp" == "y" ] || [ "$resp" == "yes" ]; then
24+
return 0
25+
fi
26+
if [ "$2" == "abort" ]; then
27+
echo -e "Abort.\n"
28+
exit 0
29+
fi
30+
if [ "$2" == "clean" ]; then
31+
rm "$3"
32+
echo -e "Abort.\n"
33+
exit 0
34+
fi
35+
return 1
36+
}
37+
38+
39+
commands="rsync parted gdisk resize-helper"
40+
packages="rsync parted gdisk 96boards-tools-common"
41+
need_packages=""
42+
43+
idx=1
44+
for cmd in $commands
45+
do
46+
if ! command -v $cmd > /dev/null; then
47+
pkg=$(echo "$packages" | cut -d " " -f $idx)
48+
printf "%-30s %s\n" "Command not found: $cmd", "package required: $pkg"
49+
need_packages="$need_packages $pkg"
50+
fi
51+
((++idx))
52+
done
53+
54+
if [ "$need_packages" != "" ]; then
55+
confirm "Do you want to apt-get install the packages?" "abort"
56+
apt-get install -y --no-install-recommends $need_packages
57+
fi
58+
59+
60+
OLD_OPTIND=$OPTIND
61+
while getopts "o:m:t:hu" flag; do
62+
case $flag in
63+
o)
64+
output="$OPTARG"
65+
;;
66+
m)
67+
model="$OPTARG"
68+
;;
69+
t)
70+
target="$OPTARG"
71+
;;
72+
h)
73+
$OPTARG
74+
print_help="1"
75+
;;
76+
u)
77+
$OPTARG
78+
unattended="1"
79+
;;
80+
esac
81+
done
82+
OPTIND=$OLD_OPTIND
83+
84+
85+
gen_partitions() {
86+
if [ "$model" == "rockpis" ]; then
87+
boot_size=229376
88+
else
89+
boot_size=1048576
90+
fi
91+
92+
loader1_size=8000
93+
reserved1_size=128
94+
reserved2_size=8192
95+
loader2_size=8192
96+
atf_size=8192
97+
98+
system_start=0
99+
loader1_start=64
100+
reserved1_start=$(expr ${loader1_start} + ${loader1_size})
101+
reserved2_start=$(expr ${reserved1_start} + ${reserved1_size})
102+
loader2_start=$(expr ${reserved2_start} + ${reserved2_size})
103+
atf_start=$(expr ${loader2_start} + ${loader2_size})
104+
boot_start=$(expr ${atf_start} + ${atf_size})
105+
rootfs_start=$(expr ${boot_start} + ${boot_size})
106+
}
107+
108+
109+
gen_image_file() {
110+
if [ "$output" == "" ]; then
111+
output=${PWD}/rockpi-backup-`date +%y%m%d-%H%M`.img
112+
else
113+
if [ "${output:(-4)}" == ".img" ]; then
114+
mkdir -p `dirname $output`
115+
else
116+
mkdir -p "$output"
117+
output=${output%/}/rockpi-backup-`date +%y%m%d-%H%M`.img
118+
fi
119+
fi
120+
121+
rootfs_size=$(expr `df -P | grep /dev/root | awk '{print $3}'` \* 5 \/ 4 \/ 1024)
122+
dd if=/dev/zero of=${ROOTFS_PATH} bs=1M count=0 seek=$rootfs_size status=none
123+
124+
img_rootfs_size=$(stat -L --format="%s" ${ROOTFS_PATH})
125+
gptimg_min_size=$(expr $img_rootfs_size + \( ${loader1_size} + ${reserved1_size} + ${reserved2_size} + ${loader2_size} + ${atf_size} + ${boot_size} + 35 \) \* 512)
126+
gpt_image_size=$(expr $gptimg_min_size \/ 1024 \/ 1024 + 2)
127+
128+
dd if=/dev/zero of=${output} bs=1M count=0 seek=$gpt_image_size status=none
129+
130+
parted -s ${output} mklabel gpt
131+
parted -s ${output} unit s mkpart loader1 ${loader1_start} $(expr ${reserved1_start} - 1)
132+
parted -s ${output} unit s mkpart loader2 ${loader2_start} $(expr ${atf_start} - 1)
133+
parted -s ${output} unit s mkpart trust ${atf_start} $(expr ${boot_start} - 1)
134+
parted -s ${output} unit s mkpart boot ${boot_start} $(expr ${rootfs_start} - 1)
135+
parted -s ${output} set 4 boot on
136+
parted -s ${output} -- unit s mkpart rootfs ${rootfs_start} -34s
137+
138+
if [ "$model" == "rockpis" ]; then
139+
ROOT_UUID="614e0000-0000-4b53-8000-1d28000054a9"
140+
else
141+
ROOT_UUID="B921B045-1DF0-41C3-AF44-4C6F280D3FAE"
142+
fi
143+
144+
gdisk ${output} > /dev/null << EOF
145+
x
146+
c
147+
5
148+
${ROOT_UUID}
149+
w
150+
y
151+
q
152+
EOF
153+
}
154+
155+
156+
check_avail_space() {
157+
output_=${output}
158+
while true; do
159+
store_size=`df -BM | grep "$output_\$" | awk '{print $4}' | sed 's/M//g'`
160+
if [ "$store_size" != "" ] || [ "$output_" == "\\" ]; then
161+
break
162+
fi
163+
output_=`dirname $output_`
164+
done
165+
166+
if [ $(expr ${store_size} - ${gpt_image_size}) -lt 64 ]; then
167+
rm ${ROOTFS_PATH}
168+
rm ${output}
169+
echo -e "No space left on ${output_}\nAborted.\n"
170+
exit 1
171+
fi
172+
173+
tmp_size=`df -BM | grep "/\$" | awk '{print $4}' | sed 's/M//g'`
174+
175+
if [ $(expr ${tmp_size} - ${gpt_image_size}) -lt 64 ]; then
176+
if [ $(expr ${store_size} - ${gpt_image_size} - ${gpt_image_size}) -lt 64 ]; then
177+
rm ${ROOTFS_PATH}
178+
rm ${output}
179+
echo -e "No space left on /tmp or ${output_}\nAborted.\n"
180+
exit 1
181+
else
182+
echo '--------------------'
183+
echo -e "No space left on /tmp, so ${SCRIPT_NAME} put a temporary rootfs.img in ${output_}"
184+
confirm "Do you want to continue?" "abort"
185+
mv ${ROOTFS_PATH} ${output_}/rootfs.img
186+
ROOTFS_PATH=${output_}/rootfs.img
187+
fi
188+
fi
189+
190+
return 0
191+
}
192+
193+
194+
backup_image() {
195+
mkdir -p ${ROOTFS_MOUNT}
196+
mkfs.ext4 ${ROOTFS_PATH}
197+
mount -t ext4 ${ROOTFS_PATH} $ROOTFS_MOUNT
198+
199+
rsync --force -rltWDEgop --delete --stats --progress \
200+
--exclude "$output" \
201+
--exclude '.gvfs' \
202+
--exclude '/dev' \
203+
--exclude '/media' \
204+
--exclude '/mnt' \
205+
--exclude '/proc' \
206+
--exclude '/run' \
207+
--exclude '/sys' \
208+
--exclude '/tmp' \
209+
--exclude 'lost\+found' \
210+
--exclude "$ROOTFS_MOUNT" \
211+
// $ROOTFS_MOUNT
212+
213+
# special dirs
214+
for i in dev media mnt proc run sys boot; do
215+
if [ ! -d $ROOTFS_MOUNT/$i ]; then
216+
mkdir $ROOTFS_MOUNT/$i
217+
fi
218+
done
219+
220+
if [ ! -d $ROOTFS_MOUNT/tmp ]; then
221+
mkdir $ROOTFS_MOUNT/tmp
222+
chmod a+w $ROOTFS_MOUNT/tmp
223+
fi
224+
225+
sync
226+
umount $ROOTFS_MOUNT
227+
228+
dd if=${DEVICE}p1 of=${output} seek=${loader1_start} conv=notrunc
229+
dd if=${DEVICE}p2 of=${output} seek=${loader2_start} conv=notrunc
230+
dd if=${DEVICE}p3 of=${output} seek=${atf_start} conv=notrunc
231+
dd if=${DEVICE}p4 of=${output} conv=notrunc seek=${boot_start} status=progress
232+
dd if=${ROOTFS_PATH} of=${output} conv=notrunc,fsync seek=${rootfs_start} status=progress
233+
234+
rm ${ROOTFS_PATH}
235+
236+
echo "Backup done, backup file is ${output}"
237+
}
238+
239+
240+
usage() {
241+
echo -e "Usage:\n sudo ./${SCRIPT_NAME} [-o output|-m model|-t target|-u]"
242+
echo ' -o specify output position, default is $PWD'
243+
echo ' -m specify model, rockpi4 or rockpis, default is rockpi4'
244+
echo ' -t specify target, backup or expand, default is backup'
245+
echo ' -u unattended backup image, no confirmations asked'
246+
}
247+
248+
249+
main() {
250+
echo -e "Welcome to use rockpi-backup.sh, part of the Rockpi toolkit.\n"
251+
echo -e " Enter ${SCRIPT_NAME} -h to view help."
252+
echo -e " For a description and example usage, see the README.md at:
253+
https://rock.sh/rockpi-toolbox \n"
254+
echo '--------------------'
255+
if [ "$target" == "expand" ]; then
256+
gdisk ${DEVICE} << EOF
257+
w
258+
y
259+
y
260+
EOF
261+
systemctl enable resize-helper
262+
else
263+
gen_partitions
264+
gen_image_file
265+
check_avail_space
266+
echo '--------------------'
267+
printf "The backup file will be saved at %s\n" "$output"
268+
printf "After this operation, %s MB of additional disk space will be used.\n" "$gpt_image_size"
269+
confirm "Do you want to continue?" "clean" "$output"
270+
backup_image
271+
fi
272+
}
273+
274+
275+
if [ "$print_help" == "1" ]; then
276+
usage
277+
else
278+
main
279+
fi
280+
# end

0 commit comments

Comments
 (0)
This repository has been archived.