#!/bin/bash ### ABOUT: See: http://gist.github.com/366269 ### Runs rsync, retrying on errors up to a maximum number of tries. ### On failure script waits for internect connection to come back up by pinging google.com before continuing. ### ### Usage: $ ./rsync-retry.sh source destination ### Example: $ ./rsync-retry.sh user@server.example.com:~/* ~/destination/path/ ### ### INPORTANT: ### To avoid repeated password requests use public key authentication instead of passwords ### "ssh-keygen" (with no password), then "ssh-copy-id user@server.example.com" echo -n "Enter No. of retries to attempt... " read MAX_RETRIES echo -n "Recursive flag ON? (y/n)? " read YN if [[ $YN == "y" || $YN == "Y" ]]; then RFLAG=r fi COM="rsync -vzP$RFLAG --inplace -e 'ssh -o \"ServerAliveInterval 10\"' $1 $2" echo echo "Using command: $COM" COUNT=0 # Trap interrupts and exit instead of continuing the loop trap "echo Ctl+C Detected... Exiting!; exit;" SIGINT SIGTERM # Set the initial exit value to failure false while [ $? -ne 0 -a $COUNT -lt $MAX_RETRIES ]; do COUNT=$(($COUNT+1)) if [ $COUNT -ne 1 ]; then echo echo "Testing Internet connection..." false while [ $? -ne 0 ]; do ping -q -c 3 google.com if [ $? -eq 0 ]; then echo "Connected!!" else echo "Ping failed trying again..." fi done fi echo echo "Attempt No. $COUNT/$MAX_RETRIES" echo rsync -vzP$RFLAG --inplace -e 'ssh -o "ServerAliveInterval 10"' $1 $2 #$COM done if [ $COUNT -eq $MAX_RETRIES -a $? -ne 0 ]; then echo "Hit maximum number of retries($MAX_RETRIES), giving up." fi if [ $? -eq 0 ]; then echo "Finished after $COUNT retries!!" fi