-
Notifications
You must be signed in to change notification settings - Fork 0
/
bak-disk
executable file
·111 lines (81 loc) · 2.55 KB
/
bak-disk
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
#!/bin/bash
# goncalomb, 2020
set -e
DIR="$1_$(date +%s.%N)"
DEV=$2
CREATE_IMAGE=
if [ "$3" == "img" ]; then
CREATE_IMAGE=true
fi
[ ! -b "$DEV" ] && echo "'$DEV' is not a block device" && exit 1
sudo -v
mkdir -p "$DIR" "$DIR/info" "$DIR/part"
{
echo "bak-disk 1.3"
echo "$DIR"
echo "$DEV"
if [ -n "$CREATE_IMAGE" ]; then
echo "will create image"
else
echo "will not create image"
fi
if [ -n "$CREATE_IMAGE" ]; then
echo
echo img
echo
sudo dd if="$DEV" bs=1M status=progress | gzip > "$DIR/raw.img.gz"
fi
echo
echo info
echo
lsblk "$DEV" > "$DIR/info/lsblk"
echo "lsblk: lsblk DEV > OUT"
lsblk -JOb "$DEV" > "$DIR/info/lsblk_json"
echo "lsblk_json: lsblk -JOb DEV > OUT"
lsblk -POb "$DEV" > "$DIR/info/lsblk_pairs"
echo "lsblk_pairs: lsblk -POb DEV > OUT"
echo "fdisk: fdisk -l DEV > OUT"
sudo fdisk -l "$DEV" > "$DIR/info/fdisk"
echo "gdisk: gdisk -l DEV > OUT"
sudo gdisk -l "$DEV" > "$DIR/info/gdisk"
echo "udevadm: udevadm info -n DEV > OUT"
sudo udevadm info -n "$DEV" > "$DIR/info/udevadm"
echo "smartctl_a: smartctl -d auto -a DEV > OUT"
sudo smartctl -d auto -a "$DEV" > "$DIR/info/smartctl_a" || true
echo "smartctl_x: smartctl -d auto -x DEV > OUT"
sudo smartctl -d auto -x "$DEV" > "$DIR/info/smartctl_x" || true
echo "head: dd status=progress if=DEV bs=512 count=2048 > OUT"
sudo dd status=progress if="$DEV" bs=512 count=2048 > "$DIR/info/head"
echo "sgdisk_backup: sgdisk -b OUT DEV"
sudo sgdisk -b "$DIR/info/sgdisk_backup" "$DEV"
sudo chown "$(id -u):$(id -g)" "$DIR/info/sgdisk_backup"
echo
echo part
echo
TMP=$(mktemp -d)
# mount partitions
lsblk -Po name "$DEV" | while IFS= read -r LINE; do
eval "$LINE"
DEV_PART="/dev/$NAME"
[ "$DEV" == "$DEV_PART" ] && continue
mkdir -p "$TMP/$NAME"
(
set -e
echo "mount '$DEV_PART'"
sudo mount -t auto -o ro "$DEV_PART" "$TMP/$NAME"
echo "tar '$NAME'"
sudo tar --acls --selinux --xattrs -czf "$DIR/part/$NAME.tar.gz" -C "$TMP" "$NAME"
sudo chown "$(id -u):$(id -g)" "$DIR/part/$NAME.tar.gz"
echo "umount '$DEV_PART'"
sudo umount "$TMP/$NAME"
) || true
rmdir "$TMP/$NAME"
done
rmdir "$TMP"
# end
date +%s.%N > "$DIR/end"
} 2>&1 | tee "$DIR/log"
# https://github.com/goncalomb/filekeep
if command -v filekeep > /dev/null; then
filekeep -d "$DIR" create
fi