-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisk
executable file
·154 lines (122 loc) · 2.98 KB
/
bisk
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
#!/bin/bash
set -euf
hasbin()
{
command -vp "$1" >/dev/null 2>&1 && return 0
printf "%s: command is required" "$1"
exit 1
}
hasbin sgdisk
hasbin id
hasbin lsdblk
hasbin basename
hasbin dirname
hasbin cut
hasbin printf
hasbin partclone.dd
usage()
{
desc="Arguments:
device block device path, ex: /dev/sda
directory all the backup files are stored into the given directory "
printf "Usage: %s device directory\n\n%s\n" "$(basename "$0")" "$desc"
}
checkargs()
{
[ "$#" -ne 2 ] && return 1
if [ ! -b "$1" ]; then
printf "%s: block device not found or read-protected\n" "$1"
return 1
fi
if [ ! -d "$2" ]; then
printf "%s: directory not found or read-protected\n" "$2"
return 1
fi
}
save_gpt()
{
_dev="$1"
_dirout="$2"
bkp_file="${_dirout}/$(basename "$_dev")_gpt.pcl"
out="$(sgdisk -b "$bkp_file" "$_dev" 2>&1)"
if [ "$?" -ne 0 ]; then
echo "save_gpt: failed"
echo "$out"
exit 1
fi
echo "save_gpt: ok"
}
save_mbr()
{
_dev="$1"
_dirout="$2"
bkp_file="${_dirout}/$(basename "$_dev")_mbr.img"
out="$(dd if="$_dev" of="$bkp_file" bs=512 count=1 2>&1)"
if [ "$?" -ne 0 ]; then
echo "save_mbr: failed"
echo "$out"
exit 1
fi
echo "save_mbr: ok"
}
save_blk()
{
_blk_path="$1"
_blk_fstype="$2"
_dirout="$3"
_logfile="$4"
blk_name="$(basename "$_blk_path")"
partclone_opt="-c"
partclone_fstype="$_blk_fstype"
[ -z "$_blk_fstype" ] && { partclone_fstype="dd"; partclone_opt=; }
if ! command -vp "partclone.${partclone_fstype}" >/dev/null 2>&1; then
echo "${_blk_fstype}: partclone does not handle this fstype"
return 1
fi
set -o pipefail
partclone."${partclone_fstype}" \
$partclone_opt -d3 -N \
-s "${_blk_path}" \
--logfile "${_logfile}" \
| gzip -2 > "${_dirout}/${blk_name}.gz"
ret="$?"
set +o pipefail
return "$ret"
}
save_partitions()
{
_dev="$1"
_dirout="$2"
data_blks="$(lsdblk "$_dev")"
if [ -z "$data_blks" ]; then
echo "No partition detected"
exit 0
fi
logdir="${_dirout}/logs"
mkdir -p "$logdir"
echo "$data_blks" | while read -r blk; do
blk_path="$(echo "$blk" | cut -d' ' -f1)"
blk_name="$(basename "$blk_path")"
blk_fstype="$(echo "$blk" | cut -d' ' -f2)"
[ "$blk_fstype" = "$blk_path" ] && blk_fstype=
logfile="${logdir}/${blk_name}-partclone.log"
# If we fail to clone a block we try with the next one
if ! save_blk "$blk_path" "$blk_fstype" "$_dirout" "$logfile" ; then
echo "save_blk '${blk}': failed, log=${logfile}"
continue
fi
echo "save_blk '${blk}': ok"
done
}
main()
{
checkargs "$@" || { usage; exit 2; }
if [ "$(id -u)" -ne 0 ]; then
echo "sudo required"
exit 1
fi
save_mbr "$@"
save_gpt "$@"
save_partitions "$@"
}
main "$@"