I have the following bash shell script that I’m running periodically from cron in Ubuntu 16.04. I’m piping my cron output to cronlog.txt and cronerrlog.txt respectively. The output from the actual backup command I’m independently outputting to .log files so as not to jam up my cron output. I have plans of keeping each backup’s .log output separately so that I can refer to it if necessary.
#!/bin/bash now=$(date +"%Y%m%d%H%M") today=$(date +"%Y%m%d") rbkup="/home/nexargi/GI/bkup/" rrepo="/home/nexargi/GI/bkup/restic-repofile.txt" rhome="/home/nexargi/GI/restic/" result="UNKNOWN" echo '------ BACKUP START ------' $now ${rhome}restic backup -v -v --files-from ${rbkup}restic-includefiles.txt --repository-file $rrepo --password-file ${rbkup}rchuparustom >>./logs/bkup_$now.log &2>>./logs/bkuperr_$now.log result=$? echo $result if [[ $result == "0" ]]; then echo 'Backup Success' /usr/sbin/sendmail purvez@nexar.free-online.co.uk < successemail.txt fi if [[ $result == "1" ]]; then echo 'Backup Fail' /usr/sbin/sendmail purvez@nexar.free-online.co.uk < failureemail.txt fi if [[ $result == "3" ]]; then echo 'Backup Partial' /usr/sbin/sendmail purvez@nexar.free-online.co.uk < partialsuccessemail.txt fi if [[ $result == "UNKNOWN" ]]; then echo 'Unknown backup error' /usr/sbin/sendmail purvez@nexar.free-online.co.uk < unknownproblem.txt fi echo '------ BACKUP END ------' $now
When everything is running smoothly it appears to work. I wanted to test the script so I created a fatal error by changing the name of the repository in the restic-repofile.txt. In the bkup_datetime.log file I got a single line:
open repository
The bkuperr_datetime.log file is empty.
My cronerrlog.txt shows the following lines:
Fatal: unable to open config file: Stat: stat /home/nexargi/GI/bkup/restic-repox/config: no such file or directory
Is there a repository at the following location?
/home/nexargi/GI/bkup/restic-repox
and my cronlog.txt shows:
------ BACKUP START ------ 202104201133
0
Backup Success
------ BACKUP END ------ 202104201133
I was expecting the Fatal error to be reported in bkuperr_datetime.log file and the Exit code to be 1 so that I could catch the fatal error and send myself an email.
My questions are:
-
Am I trying to be too clever here?
-
Why is restic not reporting the Fatal error as part of the backup command in stderr?
-
Why is the Exit code always 0?
-
Is this more of a bash/shell script problem than a restic one? If it is I’ll happily search for the answer elsewhere but getting a ‘zero’ exit code immediately after the backup command that had a fatal error is what’s confusing me.
Thanks for all help in advance.