3

Is it possible to check if an rsync is successful and potentially collect an error if its not?

As I am using --link-dest, I think this may be harder to detect.

I'm fairly new to bash, so any guidance would be appreciated.

Here is a slimmed down version of my script:

#!/bin/bash

set -e

site_host=(
    "[email protected]"
    "[email protected]"  
)

backup_dest=(
    "/Users/computername/Desktop/rsync/test1.co.uk"
    "/Users/computername/Desktop/rsync/test2.co.uk"
)

now=`date "+%d/%m/%Y %H:%M:%S"`
today=`date +"%d-%m-%Y"`
yesterday=`date -v -1d +"%d-%m-%Y"`

log="/Users/computername/Desktop/rsync/rsync.log"

site_count=${#site_host[@]}

for (( i = 0; i < site_count; i++ )); do

    site_source="${site_host[$i]}:~/public_html"
    site_dest="${backup_dest[$i]}/$today/"

    rsync -zavx -e 'ssh -p22' \
            --numeric-ids \
            --delete -r \
            --link-dest=../$yesterday $site_source $site_dest

    echo "$now - File Backup Completed - ${backup_dest[$i]}/$today" >> $log 

done

Updated:

#!/bin/bash

set -e

site_host=(
    "[email protected]"
    "[email protected]"  
)

backup_dest=(
    "/Users/computername/Desktop/rsync/test1.co.uk"
    "/Users/computername/Desktop/rsync/test2.co.uk"
)

now=`date "+%d/%m/%Y %H:%M:%S"`
today=`date +"%d-%m-%Y"`
yesterday=`date -v -1d +"%d-%m-%Y"`

log="/Users/computername/Desktop/rsync/rsync.log"

site_count=${#site_host[@]}

for (( i = 0; i < site_count; i++ )); do

    site_source="${site_host[$i]}:~/public_html"
    site_dest="${backup_dest[$i]}/$today/"

    failures=0

    if  rsync -zavx -e 'ssh -p22' \
            --numeric-ids \
            --delete -r \
            --link-dest=../$yesterday $site_source $site_dest;
    then

        echo "$now - File Backup Completed - ${backup_dest[$i]}/$today" >> $log 

    else

        echo "$now - File Backup Failed - ${backup_dest[$i]}/$today" >> $log 
        failures=$((failures+1))

    fi

    if ((failures != 0)); then exit 1; fi 

done

1 Answer 1

7

Like most programs, rsync returns a nonzero status if an error occurs. Since you have set -e at the top of the script, your script will exit with a nonzero status if rsync does.

If you want to do some recovery if rsync fails, you can keep going and analyze the status. With set -e, you need to put the rsync command in a conditional so that the script doesn't exit.

failures=0
…
  if rsync …; then
    echo "rsync succeeded"
  else
    echo "rsync failed"
    failures=$((failures+1))
  fi
…
if ((failures != 0)); then exit 1; fi

If you want to analyze the status code, a common idiom is status=0; rsync … || status=$?: this command always returns a success status since the command on the right-hand side of the || operator always succeeds, and status will be set to the status of the rsync command.

  status=0
  rsync … || status=$?
  if ((status != 0)); then
    …
  fi
3
  • Thanks Giles. Where you add ... in 'if rsync …; then', what does this refer to?
    – ccdavies
    Commented Jun 10, 2018 at 11:48
  • Sorry, I just realized! I have added an updated version of my code. Is this correct?
    – ccdavies
    Commented Jun 10, 2018 at 11:54
  • @ccdavies The point of counting failures was if you were going to run the whole loop all the time, and exit at the end if there was at least one failure. Your code is correct, but overly complicated for what it does. Commented Jun 10, 2018 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .