forked from yoffy/jetson-nano-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone-storage.sh
executable file
·76 lines (63 loc) · 1.15 KB
/
clone-storage.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
#!/bin/bash
set -eu
function usage() {
ME=$(basename $0)
echo "
usage: ${ME} device
Clone SD card to external storage.
device
Destination device to be erased and cloned. (ex. /dev/sda)
"
}
function is_mounted() {
if df | grep "^${DEV}" > /dev/null; then
return 0
else
return 1
fi
}
function atexit() {
if [[ -n $DST ]]; then
if is_mounted; then
sudo umount "${DST}" && true
fi
rmdir "${DST}"
fi
}
function erase() {
echo "WARNING: ${DEV} to be erased!"
echo "continue? [y/N]"
read ANSWER
case "${ANSWER}" in
y | Y | yes)
sudo parted -s "${DEV}" -- mklabel gpt mkpart primary 0% 100%
sudo mkfs.ext4 "${DEV}"
;;
*)
exit 1
;;
esac
}
if [[ $# -ne 1 ]] || [[ ! -e $1 ]]; then
usage
exit 1
fi
DEV=$1
SRC=/
DST=$(mktemp -d)
trap atexit EXIT ERR
if is_mounted; then
echo "${DEV} is mounted."
echo "Unmount ${DEV} before run this script."
exit 1
fi
# install rsync
if ! type rsync >/dev/null 2>&1; then
sudo apt install -y rsync
fi
# erase storage (can be commented out)
if ! erase; then
exit 1
fi
sudo mount "${DEV}" "${DST}"
sudo rsync -axHAWX --numeric-ids --progress --exclude=/proc --exclude=/tmp "${SRC}" "${DST}"