Rsync continuously until it's done

1 minute to read

My concept of Done isn't only applied on Scrum, but into my programming as well 🙂

While syncing my data to do an offsite backup, I often run into connection issues (mostly due to IP changes - since I'm using a dyndns service, because it's free as in beer).

I ran into solutions like this one, but it doesn't solve the problem of program halt (like ctrl+c or even using a kill command). Even worse, sometimes the connection is so bad that you get an "unknown" error and will have a weird state.

That said, I ended with this script to keep my stuff in sync continuously until it's really done (or expires on retries - and then it sends me an email so I can take a look at).


# Trap interruptions to exit without looping
trap 'echo "Stopped."; exit;' SIGINT SIGTERM


while :
    i = $(( $i + 1 ))
    rsync -ahvzP $@
    if [ "$?" = "0" ] ; then

        if [ $i -gt $MAX ] ; then
            echo "Hit maximum number of retries, giving up." | mail -s "Rsync failure" -a "From: Super Rsync <root>" root

    echo "Failure. Retrying..."
    sleep 180

echo "Done"

I've been running it for some time now, and looks like it does the job really well.

Let me know if it works for you too!