I have to transfer large amounts of data (>80 GB) over ssh using rsync. Everything is working fine, but the DSL connection where the backup data is sent from will drop once every 24h for up to 3 minutes (switching providers is not an option).

How do I:

  1. Automatically restart the transfer when the connection is back up?

  2. Make sure there are not by accident two rsync commands running at the same time?

link|improve this question
Can't you check the return code? while ./run_script; do echo "Retrying..."; done; echo "Done." Make sure run_script returns 0 on success. – Kerrek SB Jun 27 '11 at 11:28
feedback

migrated from stackoverflow.com Jun 27 '11 at 14:23

This question came from our site for professional and enthusiast programmers.

1 Answer

The following should be helpful:

#!/bin/bash

while [ 1 ]
do
    rsync -avz --partial source dest
    if [ "$?" = "0" ] ; then
        echo "rsync completed normally"
        exit
    else
        echo "Rsync failure. Backing off and retrying..."
        sleep 180
    fi
done

When the connection dies, rsync will quit with a non-zero exit code. This script simply keeps re-running rsync, letting it continue until the synchronisation completes normally.

link|improve this answer
Thanks, I'm trying this now... but should this: if [ "$?" = "0" ] not be: if [ "$?" == "0" ] (comparision operator)? – Andreas Jun 27 '11 at 12:55
No, in bash "=" is string equality (one of the many things that makes it confusing, I think!) – Peter Jun 27 '11 at 15:11
1  
== is alias for = :D – bbaja42 Jun 27 '11 at 16:27
1  
Ah, good to know. bash will never cease to amaze/horrify me :-P – Peter Jun 28 '11 at 23:32
feedback

Your Answer

 
or
required, but never shown