blob: 9aa7845a83cea7208d308db7989e69295fda5829 (
plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#!/bin/bash
# dd-resume version #VERSION#
usage() {
>&2 printf 'Usage: dd-resume [options]\n'
>&2 printf ' copy resumably with dd via network\n\n'
>&2 printf 'Options (all options are mandatory unless explicitely stated):\n'
>&2 printf ' --input $input-device name of input device\n'
>&2 printf ' --host $input-host name of input device'"'"'s host (target only)\n'
>&2 printf ' --log-file $log-file name of log file (target only)\n'
>&2 printf ' --output $output-device name of output device (target only)\n'
>&2 printf ' --port $remote-port port of target host (source only)\n'
>&2 printf ' --start $start-position start position for transfer (source only)\n'
>&2 printf ' --required print list of required programs and exit\n'
>&2 printf '#HELPTEXT# #\n'
[ -n "$1" ] && exit $1
exit 1
}
dd_options='bs=1M'
needed_programs='dd nc ss screen pgrep kill ssh'
eval set -- "$(
getopt -o '' \
--long input: \
--long host: \
--long log-file: \
--long output: \
--long port: \
--long start: \
--long required \
--long help \
--long version \
-n "$(basename "$0")" \
-- "$@" \
|| echo "usage"
)"
while true; do
case $1 in
--input)
if [ -n "${input_device}" ]; then
>&2 printf 'cannot handle multiple input-devices\n\n'
usage
fi
shift
input_device="$1"
;;
--host)
if [ -n "${host}" ]; then
>&2 printf 'cannot handle multiple hosts\n\n'
usage
fi
shift
host="$1"
;;
--log-file)
if [ -n "${log_file}" ]; then
>&2 printf 'cannot handle multiple log files\n\n'
usage
fi
shift
log_file="$1"
;;
--output)
if [ -n "${output_device}" ]; then
>&2 printf 'cannot handle multiple output devices\n\n'
usage
fi
shift
output_device="$1"
;;
--port)
if [ -n "${remote_port}" ]; then
>&2 printf 'cannot handle multiple ports\n\n'
usage
fi
shift
remote_port="$1"
;;
--start)
if [ -n "${start}" ]; then
>&2 printf 'cannot handle multiple starts\n\n'
usage
fi
shift
start="$1"
;;
--required)
printf '%s\n' ${needed_programs}
exit 0
;;
--help)
usage 0
;;
--version)
>&2 printf '%s\n' '#VERSION#'
exit 0
;;
--)
shift
break
;;
*)
>&2 printf 'oops, option "%s" is unkknown\n' "$1"
exit 1
esac
shift
done
if [ $# -ne 0 ]; then
>&2 printf 'too many arguments\n\n'
usage
fi
for needed in ${needed_programs}; do
if ! which "${needed}" >/dev/null; then
>&2 printf 'please install required program %s\n' "${needed}"
exit 1
fi
done
if [ -z "${input_device}" ]; then
>&2 printf 'no input device specified\n\n'
usage
fi
if [ -n "${host}" ]; then
# was run on the receiver side
if [ -z "${log_file}" ] || \
[ -z "${output_device}" ] || \
[ -n "${remote_port}" ] || \
[ -n "${start}" ]; then
>&2 printf 'conflicting options\n\n'
usage
fi
if [ ! -w "${log_file%/*}" ] && [ ! -w "${log_file}" ]; then
>&2 printf 'cannot write/create log-file %s\n\n' "${log_file}"
usage
fi
if [ ! -w "${output_device}" ]; then
>&2 printf 'cannot write to output device %s\n\n' "${output_device}"
usage
fi
start=0
if [ -f "${log_file}" ]; then
start=$(
sed -n '
1 {
s/^start: //
p
q
}
' "${log_file}"
)
copied=$(
sed '
s/^\([0-9]\+\)+0 records out$/\1/
t
d
' "${log_file}" | \
sort -n | \
tail -n1
)
if [ -n "${copied}" ]; then
start=$((start+copied))
fi
fi
previously_running=$(
pgrep -x 'dd|nc'
)
printf 'start: %s\n' "${start}" > \
"${log_file}"
screen -d -m bash -c '
set +o pipefail
nc -l -c | \
dd of="'"${output_device}"'" '"${dd_options}"' seek='"${start}"' 2>>"'"${log_file}"'" >/dev/null
printf '"'"'\nwrite-pipe-exit: %s\n'"'"' $? >>"'"${log_file}"'"
'
for i in {1..20}; do
if [ -n "${port}" ]; then
break
elif [ ${i} -gt 1 ]; then
sleep 0.1
fi
nc_pid=$(
pgrep -x nc | \
grep -vxF "$(printf '%s\n' "${previously_running}")"
)
dd_pid=$(
pgrep -x dd | \
grep -vxF "$(printf '%s\n' "${previously_running}")"
)
port=$(
ss -nlp | \
grep -F ",pid=${nc_pid}," | \
awk '{print $5}' | \
sed 's/^.*://'
)
done
if [ -z "${port}" ]; then
>&2 printf 'could not determine listening port\n'
exit 1
fi
screen -d -m bash -c '
while kill -SIGUSR1 '"${dd_pid}"'; do
sleep 1
done
'
ssh "${host}" "$0 --input ${input_device} --start ${start} --port ${port}"
printf '\nssh-exit: %s\n' $? >>"${log_file}"
else
# was run on the sender side
if [ -n "${log_file}" ] || \
[ -n "${output_device}" ] || \
[ -z "${remote_port}" ] || \
[ -z "${start}" ]; then
>&2 printf 'conflicting options\n\n'
usage
fi
remote_ip="${SSH_CLIENT%% *}"
if [ -z "${remote_ip}" ]; then
>&2 printf 'cannot obtain remote ip\n'
exit 1
fi
if [ ! -r "${input_device}" ]; then
>&2 printf 'cannot read from %s\n\n' "${input_device}"
usage
fi
set +o pipefail
dd if="${input_device}" ${dd_options} skip="${start}" 2>/dev/null | \
nc -cn "${remote_ip}" "${remote_port}"
exit $?
fi
|