Retrying an rsync (or ssh) command until it succeeds

I recently have been working with a computer in remote Alaska that is connected to the internet via a highly unreliable cellular modem. It turns out that the antenna placement on this device leads to a ton of interference with the other equipment in the enclosure, resulting in lost packets and generally abysmal connectivity. I needed to get some files off the computer, and rsync was the obvious tool for the job. Most of the time, though, rsync wouldn’t even connect. And on the one-in-roughly-twenty times it did manage to connect, it would probably fail partway through. This required persistence. Here’s an example of a shell script I used to continuously retry an rsync command until it succeeds.

#!/bin/bash

while true; do
    # -a archive mode
    # -z compress files during transfer (crucial!)
    # -P enable partial transfers (also crucial!) and show progress
    rsync -azP --timeout=10 server.example.org:~/my_directory .

    # success if we have a zero return code
    if [ $? -eq 0 ]; then
        echo "Success."
        break
    else
        echo "Failure. Retrying in 5 seconds."
        sleep 5
    fi
done