18 January 2010 View 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

[cce_bash]
wget http://www.hartvig.de/files/recp
mv recp ~/bin/
chmod +x ~/bin/recp
[/cce_bash]

Usage

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

[cc]cp file1 file2[/cc]

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

[cc]recp file1 file2[/cc]

Issues

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

[cce_bash]
#!/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”
[/cce_bash]

Credits: Forum post on inuxquestions.org

Tags:
blog comments powered by Disqus