-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtransferPraceData.sh
executable file
·493 lines (434 loc) · 16.7 KB
/
transferPraceData.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/bin/bash
# Sebastian von Alfthan sebastian.von.alfthan@csc.fi
# Yann Kempf yann.kempf@fmi.fi
# Urs Ganse urs.ganse@utu.fi
# FUNCTIONS ##################################
function transferFileList {
server=$1
path=$2
inputfile=$3
localTapePath=$4
export GLOBUS_TCP_SOURCE_RANGE=20000,20500
export GLOBUS_TCP_PORT_RANGE=20000,20500
while read line; do
#inputfile produced with ls -la, get name and size. sed one-liner to remove color-codes
file=$(echo $line| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | gawk '{print $9}')
size=$(echo $line | gawk '{print $5}')
#chunksize
chunkSize=1000000000
totalChunks=$(( 1+size/chunkSize ))
retval=0
#file exists on archive folder, if it is incomplete on archive server then that is not taken into account in any way
if [ -e ${localTapePath}/${file} ]
then
tapeSize=$( ls -la ${localTapePath}/${file} | gawk '{print $5}' )
if [ $tapeSize -eq $size ]
then
#file complete
retval=1
echo "$(date) ${file}: File is already transferred and on archive"
fi
fi
#compute where to start download
if [ -e $file ]
then
#file exists
localSize=$( ls -la $file | gawk '{print $5}' )
#Start from next possible chunkposition, some data may be lost from incomplete chunk
i=$(( localSize / chunkSize ))
if [ $localSize -eq $size ]
then
#file complete
retval=1
echo "$(date) ${file}: File is already transferred"
fi
else
#nothing has been transferred, start from beginning
i=0
fi
retryIndex=0
while [ $retval -eq 0 ]
do
sleep 1 #short sleep to make it easier to cancel..
#offset into file where we start to download data
offset=$(echo $i $chunkSize|gawk '{print $1*$2}')
echo "$(date) ${file}: Starting download of chunk $((i+1))/$totalChunks at $offset "
localStartSize=$offset
startTime=$( date +"%s.%N" )
globus-url-copy -rst -len $chunkSize -off $offset ${server}/${path}/${file} ./
rc=$?
if [[ $rc != 0 ]] ; then
echo "Failed: globus-url-copy -rst -len $chunkSize -off $offset ${server}/${path}/$file ./"
exit $rc
fi
localEndSize=$( ls -la $file | gawk '{print $5}' )
endTime=$( date +"%s.%N" )
echo $startTime $endTime $localStartSize $localEndSize $file $((i+1)) "$(date)" |
gawk '{
dataMb=($4-$3)/(1024*1024);
times=($2-$1);
print $7,$5,": chunk ",$6," downloaded at", dataMb," MB in ",times " s : ", dataMb/times, "MB/s"
}'
localSize=$( ls -la $file | gawk '{print $5}' )
if [ $localSize -eq $size ]
then
#the whole file has been downloaded, excellent!
echo "$(date) ${file}: Done"
if [ -e ${localTapePath}/${file} ]
then
echo "$(date) ${file}: WARNING file with the same name already exists on ${localTapePath} - file not moved from staging at $(pwd)"
else
mv ${file} ${localTapePath}/
echo "$(date) ${file}: Moved from staging at $( pwd ) to ${localTapePath}"
fi
retval=1
retryIndex=0
else
#file not complete
if [ $localSize -lt $(( chunkSize + offset )) ]
then
#we failed to download the whole chunk
retryIndex=$(( retryIndex+1 ))
echo "$(date) ${file}: Chunk transfer failed, retry number $retryIndex "
if [ $retryIndex -gt 2 ]
then
echo "$(date) ${file}: Too many retries, abort. Failed on reading to offset $offset"
retval=2
fi
else
#chunk downloaded, lets get the next one
i=$(( i+1 ))
retryIndex=0
fi
fi
done
done < $inputfile
}
function transferFileListDdSsh {
user=$1
server=$2
path=$3
inputfile=$4
localTapePath=$5
while read line
do
#inputfile produced with ls -la, get name and size. sed one-liner to remove color-codes
file=$(echo $line| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | gawk '{print $9}')
size=$(echo $line | gawk '{print $5}')
#chunksize
chunkSize=$(( 1024 * 1024 * 1024 ))
totalChunks=$(( 1+size/chunkSize ))
retval=0
#file exists on archive folder, if it is incomplete on archive server then that is not taken into account in any way
if [ -e ${localTapePath}/${file} ]
then
tapeSize=$( ls -la ${localTapePath}/${file} | gawk '{print $5}' )
if [ $tapeSize -eq $size ]
then
#file complete
retval=1
echo "$(date) ${file}: File is already transferred and on archive"
fi
fi
#compute where to start download
if [ -e $file ]
then
#file exists
localSize=$( ls -la $file | gawk '{print $5}' )
#Start from next possible chunkposition, some data may be lost from incomplete chunk
i=$(( localSize / chunkSize ))
if [ $localSize -eq $size ]
then
#file complete
retval=1
echo "$(date) ${file}: File is already transferred"
fi
else
#nothing has been transferred, start from beginning
i=0
fi
retryIndex=0
while [ $retval -eq 0 ]
do
sleep 1 #short sleep to make it easier to cancel..
#Size of current transfer. chunksize for all, except last transfer
transferSize=$(echo $i $chunkSize $size|gawk '{if(($1+1)*$2 > $3) print $3-$1*$2; else print $2;}')
echo transferSize $transferSize
echo "$(date) ${file}: Starting download of chunk $((i+1))/$totalChunks "
startTime=$( date +"%s.%N" )
if [ $server == "localhost" ]
then
dd iflag=fullblock bs=${chunkSize} skip=$i count=1 if=${path}/${file} 2>> dd_read.err | tee >(md5sum > .transfer_${file}_chunk_${i}_checksum.txt) |\
dd iflag=fullblock bs=${chunkSize} seek=$i count=1 of=${file} 2>> dd_write.err
sourceChecksum=`cat .transfer_${file}_chunk_${i}_checksum.txt`
else
ssh -o Compression=no ${user}@${server} "dd iflag=fullblock bs=${chunkSize} skip=$i count=1 if=${path}/${file} | tee >(md5sum > ${path}/.transfer_${file}_chunk_${i}_checksum.txt)" 2>> dd_read.err |\
dd iflag=fullblock bs=${chunkSize} seek=$i count=1 of=${file} 2>> dd_write.err
sourceChecksum=`ssh ${user}@${server} "cat ${path}/.transfer_${file}_chunk_${i}_checksum.txt"`
fi
endTime=$( date +"%s.%N" )
echo $startTime $endTime $chunkSize $file $((i+1)) "$(date)" |
gawk '{
dataMb=($3)/(1024*1024);
times=($2-$1);
print $6,$4,": chunk ",$5," downloaded at", dataMb," MB in ",times " s : ", dataMb/times, "MB/s"
}'
#Test if file is complete
# Check checksums
targetChecksum=`dd iflag=fullblock bs=${chunkSize} skip=$i count=1 if=${file} 2>>/dev/null | md5sum`
if [[ $sourceChecksum == $targetChecksum ]]; then
echo "Chunk $i transferred successfully"
i=$(( i+1 ))
retryIndex=0
else
retryIndex=$(( retryIndex+1 ))
echo "$(date) ${file}: Chunk $i checksum inconsistent, retry number $retryIndex "
if [ $retryIndex -gt 10 ]
then
echo "$(date) ${file}: Too many retries, abort. Failed on reading to offset $offset"
retval=2
fi
fi
# Initially it breaks if there is no file.
touch $file
localSize=$( ls -la $file | gawk '{print $5}' )
if [ $localSize -eq $size ]
then
#the whole file has been downloaded, excellent!
echo "$(date) ${file}: Done"
if [ -e ${localTapePath}/${file} ]
then
echo "$(date) ${file}: WARNING file with the same name already exists on ${localTapePath} - file not moved from staging at $(pwd)"
else
mv ${file} ${localTapePath}/
echo "$(date) ${file}: Moved from staging at $( pwd ) to ${localTapePath}"
fi
retval=1
retryIndex=0
fi
done
done < $inputfile
}
function transferFileListRsync {
user=$1
server=$2
path=$3
inputfile=$4
localTapePath=$5
while read line; do
#inputfile produced with ls -la, get name and size. sed one-liner to remove color-codes
file=$(echo $line| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | gawk '{print $9}')
size=$(echo $line | gawk '{print $5}')
retval=0
#file exists on archive folder, if it is incomplete on archive server then that is not taken into account in any way
if [ -e ${localTapePath}/${file} ]
then
tapeSize=$( ls -la ${localTapePath}/${file} | gawk '{print $5}' )
if [ $tapeSize -eq $size ]
then
#file complete
retval=1
echo "$(date) ${file}: File is already transferred and on archive"
fi
fi
retryIndex=0
while [ $retval -eq 0 ]
do
sleep 1 #short sleep to make it easier to cancel..
echo "$(date) ${file}: Starting download ($retryIndex retries)"
#create empty file
if [ ! -e $file ]
then
touch $file
fi
startTime=$( date +"%s.%N" )
localStartSize=$( ls -la $file | gawk '{print $5}' )
if [ $localStartSize -ne $size ]
then
#start download if file is not complete
rsync --inplace --partial ${user}@${server}:${path}/${file} ./
rc=$?
if [[ $rc != 0 ]] ; then
echo "Failed: rsync --inplace --partial ${user}@${server}:${path}/${file} ./"
fi
localEndSize=$( ls -la $file | gawk '{print $5}' )
endTime=$( date +"%s.%N" )
echo $startTime $endTime $localStartSize $localEndSize $file "$(date)" |
gawk '{
dataMb=($4-$3)/(1024*1024);
times=($2-$1);
print $6,$5,": downloaded at", dataMb," MB in ",times " s : ", dataMb/times, "MB/s"
}'
else
echo "$(date) ${file}: File is already transferred and in staging area"
fi
localSize=$( ls -la $file | gawk '{print $5}' )
if [ $localSize -eq $size ]
then
#the whole file has been downloaded, excellent!
echo "$(date) ${file}: Done"
if [ -e ${localTapePath}/${file} ]
then
echo "$(date) ${file}: WARNING file with the same name already exists on ${localTapePath} - file not moved from staging at $(pwd)"
else
mv ${file} ${localTapePath}/ 2>> errlog
echo "$(date) ${file}: Moved from staging at $( pwd ) to ${localTapePath}"
fi
retval=1
retryIndex=0
else
echo "$(date) ${file}: File is not complete; $localEndSize / $size"
retryIndex=$(( retryIndex+1 ))
if [ $retryIndex -gt 50 ]
then
echo "$(date) ${file}: Too many retries, abort. (50 max)"
retval=2
fi
fi
done
done < $inputfile
}
# MAIN PROGRAM ##################################
export GLOBUS_TCP_SOURCE_RANGE=20000,20500
export GLOBUS_TCP_PORT_RANGE=20000,20500
if [ ! $# -eq 5 ]
then
cat <<EOF
transferPraceData userserver path transfer_file local_storage_path
Script for transferring data using gridFTP or rsync (depends on machine).
Please run grid_proxy_init first when using the gridFTP backend.
user Username, option not used for gridftp or local-dd transfers (put arbitrary name)
server One of: Hermit (gridftp), Hazelhen-r (rsync), Hazelhen-ds (dd|ssh), Abel (gridftp), Sisu-g (gridftp), LUMI-r (rsync), LUMI-ds (dd|ssh), turso-ds (dd|ssh), localhost-dd (local-dd) localhost-rp (rsync with special port)
path is a path on remote machine (e.g. /univ_1/ws1/ws/iprsalft-paper1-runs-0/2D/ecliptic/AAE)"
transfer_file is a file in the path on the remote machine created using ls -la *myfiles_to_transfer* > transfer_list.txt"
local_storage_path is the folder where the files are ultimately copied after transfer, e.g., a tape drive. During transfer they go to the current folder. "." is also allowed.
EOF
exit
fi
user=$1
machine=$2
path=$3
inputfile=$4
localTapePath=$5
parallelTransfers=5
#read in command line variables
if [ $machine == "Abel" ]
then
server=gsiftp://gridftp1.prace.uio.no:2811
method=gridftp
elif [ $machine == "Hermit" ]
then
server=gsiftp://gridftp-fr1.hww.de:2812
method=gridftp
elif [ $machine == "Hazelhen-r" ]
then
server=hazelhen.hww.de
method=rsync
elif [ $machine == "Hazelhen-ds" ]
then
server=hazelhen.hww.de
method=ddssh
elif [ $machine == "Sisu-g" ]
then
server=gsiftp://gridftp.csc.fi:2811
method=gridftp
elif [ $machine == "LUMI-r" ]
then
server=lumi.csc.fi
method=rsync
elif [ $machine == "LUMI-ds" ]
then
server=lumi.csc.fi
method=ddssh
elif [ $machine == "turso-ds" ]
then
server=turso.cs.helsinki.fi
method=ddssh
elif [ $machine == "localhost-dd" ]
then
server=localhost
method=ddssh
elif [ $machine == "localhost-rp" ]
then
server=localhost
method=rsync
export RSYNC_RSH="ssh -p 1235"
else
echo "Allowed server values are Hermit, Hazelhen-r, Hazelhen-ds, Abel, Sisu-g, LUMI-r, LUMI-ds, turso-ds, localhost-dd, localhost-rp"
exit 1
fi
if [ ! -d $localTapePath ]
then
echo "Tape path $localTapePath does not exist!"
exit
fi
#clean up old inputfile
if [ -e $inputfile ]
then
rm $inputfile
fi
echo "Downloading inputfile $inputfile from $path"
if [ $method == "gridftp" ]
then
globus-url-copy -rst ${server}/${path}/$inputfile ./$inputfile
rc=$?
if [[ $rc != 0 ]] ; then
echo "Failed: globus-url-copy -rst ${server}/${path}/$inputfile ./$inputfile"
echo "Could not download list of files"
exit $rc
fi
elif [ $method == "rsync" ]
then
rsync -P --inplace ${user}@${server}:${path}/$inputfile ./$inputfile
rc=$?
if [[ $rc != 0 ]] ; then
echo "Failed: rsync -P --inplace ${user}@${server}:${path}/$inputfile ./$inputfile"
echo "Could not download list of files"
exit $rc
fi
elif [ $method == "ddssh" ]
then
if [ $server == "localhost" ]
then
cp ${path}/$inputfile ./$inputfile
rc=$?
else
rsync -P --inplace ${user}@${server}:${path}/$inputfile ./$inputfile
rc=$?
fi
if [[ $rc != 0 ]] ; then
echo "Failed: Could not download list of files"
exit $rc
fi
else
echo "Failed: Unknown method $method"
echo "Could not download list of files"
exit $rc
fi
echo "Transferring files in $inputfile at $path" >> transferLog.txt
echo "Files staged at $( pwd) and archived at ${localTapePath}" >> transferLog.txt
cat $inputfile |gawk '{s=s+$5} END {print NR, " files with ",s/(1024*1024*1024),"GB of data"}' >> transferLog.txt
#how many files (max) per transfer
filesPerTransfer=$( wc -l $inputfile |gawk -v p=$parallelTransfers '{print int($1/p)+1}' )
echo "$parallelTransfers parallel transfers with up to $filesPerTransfer per transfer" >> transferLog.txt
#split into transferfiles
rm -f .para_$inputfile_*
split -l $filesPerTransfer -d $inputfile .para_$inputfile_
i=0
for paraInput in .para_$inputfile_*
do
if [ $method == "gridftp" ]
then
transferFileList $server $path $paraInput $localTapePath >> transferLog.txt &
elif [ $method == "rsync" ]
then
transferFileListRsync $user $server $path $paraInput $localTapePath >> transferLog.txt &
echo "Started background transfer-job $!"
elif [ $method == "ddssh" ]
then
transferFileListDdSsh $user $server $path $paraInput $localTapePath >> transferLog.txt &
echo "Started background transfer-job $!"
fi
transferPids[$i]=$!
i=$((i+1))
done