-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone-target
136 lines (121 loc) · 3.39 KB
/
clone-target
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
#!/bin/bash
IFS="
"
count=0
bold=$(tput bold)
normal=$(tput sgr0)
COMPRESSED=0
set_lport () {
LPORT=1235
while [ `netstat -ln | grep ":$LPORT" | wc -l` -gt 0 ]
do
$LPORT += 1
done
}
echo "ID Option Type"
echo "1 Disk"
echo "2 Partition"
echo "3 MBR"
echo "4 File"
echo -n "Type the ID of the type of clone that you wish to perform [1]: "
read option
case $option in
1)
type="disk"
;;
2)
type="partition"
;;
3)
type="MBR"
;;
4)
type="file"
;;
*)
type="disk"
;;
esac
echo ""
echo "ID $type"
if [ "$type" == "partition" ]; then
for i in `lsblk -e 7 -e 11 -l | grep "part" | sed -e 's/^\([a-z]*[0-9]*\) * [0-9]*:[0-9]* *[0-9] *\([0-9\.GMK]*\) .*/\1 \2/' | sort`
do
count=$((count+1))
echo "$count $i"
disks="$disks"$'\n'"$count $i"
done
elif [ "$type" == "file" ]; then
echo "Enter a file name to save to."
read filename
else
for i in `lsblk -l -e 7 -e 11 | grep "disk" | sed -e 's/ */ /g' | grep -v "NAME" | cut -d' ' -f1,4 | sort`
do
count=$((count+1))
echo "$count $i"
disks="$disks"$'\n'"$count $i"
done
fi
if [ ! "$type" == "file" ]; then
echo ""
echo -n "Type the ID matching the $type that you wish to ${bold}overwrite${normal}: "
read disknum
disk=`echo "$disks" | grep "^$disknum " | cut -d' ' -f2`
if [ "$disk" == "" ]; then
echo "Sorry, but that is not a valid selection. Exiting."
exit
fi
fi
if [ "$type" == "file" ]; then
destination="$filename"
else
destination="/dev/$disk"
fi
echo -n "Ready to overwrite $type ${bold}$destination${normal}? [n]: "
read choice
if [ ! "$choice" == "y" ] && [ ! "$choice" == "Y" ] && [ ! "$choice" == "yes" ]; then
echo "Action Aborted"
exit
fi
IP=`ifconfig | grep "inet " | grep -v "127.0.0.1" | sed -e 's/^ *//' | cut -d' ' -f2 | grep "" -m1`
BCAST=`ifconfig | grep " $IP " | grep broadcast | sed -e 's/^.*broadcast //'`
BCASTLO="127.255.255.255"
GO=1
set_lport; # Sets the listening port and increments from 1235 until it finds a port that's open
echo "Listening on IP:PORT ${bold}$IP:$LPORT${normal}."
echo "Waiting for handshake with origin server."
SIZESTRING=""
while [ $GO -eq 1 ];
do
echo "$IP:$LPORT" | nc -u -b -w1 $BCAST 1234 > /dev/null 2>&1 &
echo "127.0.0.1:$LPORT" | nc -u -b -w1 $BCASTLO 1234 > /dev/null 2>&1 &
echo -n "."
RESULT=`nc -l -p $LPORT -w5 2> /dev/null | sed -e 's/\n//'`;
if [ ! "$RESULT" == "" ] && [ $(echo $RESULT | cut -d':' -f1) -gt 1 ]; then
SIZE=$(echo $RESULT | cut -d':' -f1)
COMPRESSED=$(echo $RESULT | cut -d':' -f2)
if [ ! "$COMPRESSED" == "0" ] && [ ! "$COMPRESSED" == "1" ]; then
COMPRESSED=1
fi
if [ $COMPRESSED -eq 1 ] && [ "$type" == "file" ]; then
if [ ! $(echo $destination | grep '\.lz4' | wc -l) -eq 1 ]; then
destination+=".lz4"
fi
fi
SIZESTRING="-s $SIZE"
GO=0
echo ""
fi
done
umount /dev/${disk}* > /dev/null 2>&1
udevadm control -s
if [ "$type" == "MBR" ]; then
nc -l -p $LPORT | lz4 -d | sudo dd of=$destination
else
if [ "$COMPRESSED" == "1" ] && [ ! "$type" == "file" ]; then
nc -l -p $LPORT | lz4 -d | sudo pv -btearp -B 4194304 -N TARGET $SIZESTRING > $destination
else
nc -l -p $LPORT | sudo pv -btearp -B 4194304 -N TARGET $SIZESTRING > $destination
fi
fi
udevadm control -S