Skip to content

Instantly share code, notes, and snippets.

@tsaavik
Last active January 21, 2016 18:38
Show Gist options
  • Save tsaavik/c8476dbaadfe7096c6db to your computer and use it in GitHub Desktop.
Save tsaavik/c8476dbaadfe7096c6db to your computer and use it in GitHub Desktop.
Automatic rsync retry with incremental (delay) backoff
#!/bin/bash
#
# Rsyncr (Rsync Retry) 2016 David Mcanulty
# Runs rsync, retrying on errors up to a maximum number of tries with incremental backoff (delay)
# Based on idea from https://gist.github.com/iangreenleaf/279849 by iangreenleaf 2011
# Set your max rsync retries here
max_retries=50
# Trap interrupts (ctrl-c) and exit instead of continuing the loop
trap "echo Exited!; exit;" SIGINT SIGTERM
if [[ $# -eq 0 ]]; then
echo "*** No arguments provided to $0 showing rsync help instead ***"
sleep 2; rsync --help
exit 1
fi
read -rs -p "Optional remote rsync password(enter for none): " RSYNC_PASSWORD
i=0
while [[ ${i} -le ${max_retries} ]] ; do
if rsync "$@" ;then
echo "Rsync exited successfully after ${i} retries"
break
else
i=$(($i+1))
sleep ${i}
fi
done
exit $?
@tsaavik
Copy link
Author

tsaavik commented Jan 21, 2016

I thought about adding password handling for ssh connections, but you should just use ssh keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment