18 January 2010 0 Comments

Howto : Resume a failed copy command where it left off

Currently, file copying utilities such as cp, scp, rsync etc. are not able to resume where they left off after a failed copy operation. In this article, a solution is provided using the utility dd to pick up where cp left off. Read on for more.nova

Installation


To install recp, type the following commands in a terminal

1
2
3
wget http://www.hartvig.de/files/recp
mv recp ~/bin/
chmod +x ~/bin/recp

Usage

Lets assume, that you have tried to copy file1 to file2 with the command

1
cp file1 file2

and that the copying process has somehow been interrupted. Now, you can simply resume the copying with the command

1
recp file1 file2

Issues

Please be aware, that since the script uses dd to continue copying it is both processor intensive and slow.

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
#!/bin/bash

#set -x

if [ $# -ne 2 ]
then
        echo $0: Usage: $0 original copy
        exit 1
fi

# We need some more meaningful names.
ORIGINAL="$1"
COPY="$2"

if [ ! -f "$ORIGINAL" ]
then
        # ORIGINAL wasn't found.
        echo $0: "$ORIGINAL": No such file
        exit 1
fi

# Calculate the number of bytes to skip before we start copying.
if [ -f "$COPY" ]
then
        # This is a continuation of earlier, interrupted copy.

        WC_OUTPUT=$(wc --bytes "$COPY")

        # Since wc pads its output with spaces making it hard to reliably
        # parse, we have to do a little hack job:

        # Make sure there's at least one space at the beginning.
        WC_OUTPUT=" $WC_OUTPUT"

        # Sqeeze out excess spaces, making the size the second field.
        SKIP_BYTES=$(echo "$WC_OUTPUT" | tr -s ' ' | cut -d' ' -f2)
else
        # This is the first attempt at copying--there's no COPY yet.
        SKIP_BYTES=0
fi

# Do the actual copying.
dd if="$ORIGINAL" of="$COPY" conv=notrunc bs=1 skip="$SKIP_BYTES" seek="$SKIP_BYTES"

Credits: Forum post on inuxquestions.org

Tags: