1

I am writing a bash script which does a backup to a mounted drive.

The backup itself works fine however even if there is an error during transfer it will say its successful.

backup() {
echo -e "Backup Started\n" >> $1
echo -e "Mounting backup Drive" >> $1
mount $2 /mnt 2>>$1
if [ $? -eq 0 ] ; then
   echo -e "Backup Drive: $2 mounted successfully" >> $1
   cd /
   echo -e "Sync data to $2\n" >> $1
   nice -n 19 rsync -lHa --exclude-from '/opt/ts/bin/exclude.txt' / /mnt/ 2> ${ERROR}
   local RETURNCODE=$?
   if [ $RETURNCODE -eq 23 ] ; then backup_failed "Backup Warning: Some files did not copy" $1 $2; fi
   if [ $RETURNCODE -eq 20 ] ; then backup_failed "Backup Failed: Transfer was terminated prematurely" $1 $2; fi
   if [ $RETURNCODE -eq 11 ] ; then backup_failed "Backup Failed: Input/Output Error [URGENT]" $1 $2; fi
   if [ $RETURNCODE -ne 0 ] ; then backup_failed "Backup Failed: Error while copying data" $1 $2; fi
   echo -e "Backup completed Successfully with code $RETURNCODE `date`\n" >> $1
   echo -e >> $1
   printf "Time taken: "%dh:%dm:%ds"\n" $(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) >> $1
   echo -e "##############################\n" >> $1
   mkdir -p /mnt/mnt /mnt/proc /mnt/tmp /mnt/lost+found
   cp -a /proc/mounts /proc/filesystems /mnt/proc
   umount $2
fi
}

Despite $RETURNCODE giving a value of 20 it does not trigger. Output is below.

Mounting backup Drive Backup Drive: /dev/hda4 mounted successfully Sync data to /dev/hda4

Backup completed Successfully with code 20 Sun May 4 11:04:48 EST 2014

Time taken: 0h:0m:3s

#

If anyone has any suggestions it would be appreciated :)

2
  • set -x is your friend. Trace the script's execution and see what it's actually doing. Commented May 4, 2014 at 1:50
  • Also, I strongly recommend running this through shellcheck.net -- it has a lot of minor bugs that probably aren't responsible for your question, but still ought to be fixed. Commented May 4, 2014 at 1:50

1 Answer 1

3

Are you sure that the function backup_failed is behaving as expected?

Also note that backup_failed will be triggered twice when RETURNCODE is not 0 and backup_failed doesn't terminate the script:

if [ $RETURNCODE -eq 20 ] ; then backup_failed ...
...
if [ $RETURNCODE -ne 0 ] ; then backup_failed ...

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks threadp :) You were right, backup_failed was not behaving as expected. There was an if statement which compared files that did not backup to a list of exclusions. This was from an older version of the script and was never going to trigger in the current one.
Actually, it's even worse than this, because if RETURNCODE is 20, it'll call backup_failed twice (as described), and also print "Backup completed Successfully with code 20 ..."
backup_failed terminates the script once it has completed, it is working as expected however I do plan to improve the code (I didn't make the script originally, I was asked to add some error handling and change from cpio to rsync) :)
Done, don't recall seeing the tick button there previously as I was looking for something to do just that. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.