Skip to content

Instantly share code, notes, and snippets.

@rabinshr
Last active August 29, 2015 14:01
Show Gist options
  • Save rabinshr/370d6c641c7f43681fc8 to your computer and use it in GitHub Desktop.
Save rabinshr/370d6c641c7f43681fc8 to your computer and use it in GitHub Desktop.
Reliably copy files from a folder. Particularly useful when you have a jittery network that timeouts every now and then
#!/bin/bash
# reliable file/s transfer from a folder
# try rsync
I=0
MAX_RESTARTS=100
LAST_EXIT_CODE=1
if [ $# -lt 2 ]; then
echo "Usage: $0 src dest"
exit
fi
echo "copying files from $1 to $2"
while [ $I -le $MAX_RESTARTS ]
do
I=$(( $I + 1 ))
echo $I. start of rsync
rsync -vt -P --bwlimit=50 $1 $2
LAST_EXIT_CODE=$?
if [ $LAST_EXIT_CODE -eq 0 ]; then
break
fi
done
# check if successful
if [ $LAST_EXIT_CODE -ne 0 ]; then
echo rsync failed for $I times. giving up.
else
echo rsync successful after $I times.
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment